ordered-tags 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +22 -0
- data/MIT-LICENSE +22 -0
- data/README.rdoc +164 -0
- data/Rakefile +25 -0
- data/VERSION +1 -0
- data/lib/acts-as-taggable-on.rb +7 -0
- data/lib/acts_as_taggable_on/acts_as_taggable_on.rb +365 -0
- data/lib/acts_as_taggable_on/acts_as_tagger.rb +52 -0
- data/lib/acts_as_taggable_on/tag.rb +23 -0
- data/lib/acts_as_taggable_on/tag_list.rb +95 -0
- data/lib/acts_as_taggable_on/tagging.rb +8 -0
- data/lib/acts_as_taggable_on/tags_helper.rb +11 -0
- data/rails/init.rb +6 -0
- data/spec/acts_as_taggable_on/acts_as_taggable_on_spec.rb +165 -0
- data/spec/acts_as_taggable_on/acts_as_tagger_spec.rb +72 -0
- data/spec/acts_as_taggable_on/tag_list_spec.rb +52 -0
- data/spec/acts_as_taggable_on/tag_spec.rb +27 -0
- data/spec/acts_as_taggable_on/taggable_spec.rb +204 -0
- data/spec/acts_as_taggable_on/tagger_spec.rb +23 -0
- data/spec/acts_as_taggable_on/tagging_spec.rb +7 -0
- data/spec/schema.rb +33 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +45 -0
- metadata +94 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
== 2009-11-27
|
2
|
+
|
3
|
+
Added position/order support
|
4
|
+
|
5
|
+
== 2008-07-17
|
6
|
+
|
7
|
+
* Can now use a named_scope to find tags!
|
8
|
+
|
9
|
+
== 2008-06-23
|
10
|
+
|
11
|
+
* Can now find related objects of another class (tristanzdunn)
|
12
|
+
* Removed extraneous down migration cruft (azabaj)
|
13
|
+
|
14
|
+
== 2008-06-09
|
15
|
+
|
16
|
+
* Added support for Single Table Inheritance
|
17
|
+
* Adding gemspec and rails/init.rb for gemified plugin
|
18
|
+
|
19
|
+
== 2007-12-12
|
20
|
+
|
21
|
+
* Added ability to use dynamic tag contexts
|
22
|
+
* Fixed missing migration generator
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2009 Benoit Bénézech for ordering changes
|
2
|
+
|
3
|
+
Copyright (c) 2007 Michael Bleigh and Intridea Inc. for ActsAsTaggableOn
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
= Ordered tags!
|
2
|
+
|
3
|
+
sudo gem install ordered-tags
|
4
|
+
|
5
|
+
Every thing below comes from ActsAsTaggableOn plugin and is still valid.
|
6
|
+
|
7
|
+
= (Based on) ActsAsTaggableOn
|
8
|
+
|
9
|
+
This plugin was originally based on Acts as Taggable on Steroids by Jonathan Viney.
|
10
|
+
It has evolved substantially since that point, but all credit goes to him for the
|
11
|
+
initial tagging functionality that so many people have used.
|
12
|
+
|
13
|
+
For instance, in a social network, a user might have tags that are called skills,
|
14
|
+
interests, sports, and more. There is no real way to differentiate between tags and
|
15
|
+
so an implementation of this type is not possible with acts as taggable on steroids.
|
16
|
+
|
17
|
+
Enter Acts as Taggable On. Rather than tying functionality to a specific keyword
|
18
|
+
(namely "tags"), acts as taggable on allows you to specify an arbitrary number of
|
19
|
+
tag "contexts" that can be used locally or in combination in the same way steroids
|
20
|
+
was used.
|
21
|
+
|
22
|
+
== Installation
|
23
|
+
|
24
|
+
=== Plugin
|
25
|
+
|
26
|
+
Acts As Taggable On is available both as a gem and as a traditional plugin. For the
|
27
|
+
traditional plugin you can install like so (Rails 2.1 or later):
|
28
|
+
|
29
|
+
script/plugin install git://github.com/mbleigh/acts-as-taggable-on.git
|
30
|
+
|
31
|
+
=== GemPlugin
|
32
|
+
|
33
|
+
Acts As Taggable On is also available as a gem plugin using Rails 2.1's gem dependencies.
|
34
|
+
To install the gem, add this to your config/environment.rb:
|
35
|
+
|
36
|
+
config.gem "acts-as-taggable-on", :source => "http://gemcutter.org"
|
37
|
+
|
38
|
+
After that, you can run "rake gems:install" to install the gem if you don't already have it.
|
39
|
+
|
40
|
+
=== Post Installation (Rails)
|
41
|
+
|
42
|
+
1. script/generate acts_as_taggable_on_migration
|
43
|
+
2. rake db:migrate
|
44
|
+
|
45
|
+
=== Testing
|
46
|
+
|
47
|
+
Acts As Taggable On uses RSpec for its test coverage. Inside the plugin
|
48
|
+
directory, you can run the specs with:
|
49
|
+
|
50
|
+
rake spec
|
51
|
+
|
52
|
+
If you already have RSpec on your application, the specs will run while using:
|
53
|
+
|
54
|
+
rake spec:plugins
|
55
|
+
|
56
|
+
|
57
|
+
== Usage
|
58
|
+
|
59
|
+
class User < ActiveRecord::Base
|
60
|
+
acts_as_taggable_on :tags, :skills, :interests
|
61
|
+
end
|
62
|
+
|
63
|
+
@user = User.new(:name => "Bobby")
|
64
|
+
@user.tag_list = "awesome, slick, hefty" # this should be familiar
|
65
|
+
@user.skill_list = "joking, clowning, boxing" # but you can do it for any context!
|
66
|
+
@user.skill_list # => ["joking","clowning","boxing"] as TagList
|
67
|
+
@user.save
|
68
|
+
|
69
|
+
@user.tags # => [<Tag name:"awesome">,<Tag name:"slick">,<Tag name:"hefty">]
|
70
|
+
@user.skills # => [<Tag name:"joking">,<Tag name:"clowning">,<Tag name:"boxing">]
|
71
|
+
|
72
|
+
# The old way
|
73
|
+
User.find_tagged_with("awesome", :on => :tags) # => [@user]
|
74
|
+
User.find_tagged_with("awesome", :on => :skills) # => []
|
75
|
+
|
76
|
+
# The better way (utilizes named_scope)
|
77
|
+
User.tagged_with("awesome", :on => :tags) # => [@user]
|
78
|
+
User.tagged_with("awesome", :on => :skills) # => []
|
79
|
+
|
80
|
+
@frankie = User.create(:name => "Frankie", :skill_list => "joking, flying, eating")
|
81
|
+
User.skill_counts # => [<Tag name="joking" count=2>,<Tag name="clowning" count=1>...]
|
82
|
+
@frankie.skill_counts
|
83
|
+
|
84
|
+
=== Finding Tagged Objects
|
85
|
+
|
86
|
+
Acts As Taggable On utilizes Rails 2.1's named_scope to create an association
|
87
|
+
for tags. This way you can mix and match to filter down your results, and it
|
88
|
+
also improves compatibility with the will_paginate gem:
|
89
|
+
|
90
|
+
class User < ActiveRecord::Base
|
91
|
+
acts_as_taggable_on :tags
|
92
|
+
named_scope :by_join_date, :order => "created_at DESC"
|
93
|
+
end
|
94
|
+
|
95
|
+
User.tagged_with("awesome").by_date
|
96
|
+
User.tagged_with("awesome").by_date.paginate(:page => params[:page], :per_page => 20)
|
97
|
+
|
98
|
+
=== Relationships
|
99
|
+
|
100
|
+
You can find objects of the same type based on similar tags on certain contexts.
|
101
|
+
Also, objects will be returned in descending order based on the total number of
|
102
|
+
matched tags.
|
103
|
+
|
104
|
+
@bobby = User.find_by_name("Bobby")
|
105
|
+
@bobby.skill_list # => ["jogging", "diving"]
|
106
|
+
|
107
|
+
@frankie = User.find_by_name("Frankie")
|
108
|
+
@frankie.skill_list # => ["hacking"]
|
109
|
+
|
110
|
+
@tom = User.find_by_name("Tom")
|
111
|
+
@tom.skill_list # => ["hacking", "jogging", "diving"]
|
112
|
+
|
113
|
+
@tom.find_related_skills # => [<User name="Bobby">,<User name="Frankie">]
|
114
|
+
@bobby.find_related_skills # => [<User name="Tom">]
|
115
|
+
@frankie.find_related_skills # => [<User name="Tom">]
|
116
|
+
|
117
|
+
=== Dynamic Tag Contexts
|
118
|
+
|
119
|
+
In addition to the generated tag contexts in the definition, it is also possible
|
120
|
+
to allow for dynamic tag contexts (this could be user generated tag contexts!)
|
121
|
+
|
122
|
+
@user = User.new(:name => "Bobby")
|
123
|
+
@user.set_tag_list_on(:customs, "same, as, tag, list")
|
124
|
+
@user.tag_list_on(:customs) # => ["same","as","tag","list"]
|
125
|
+
@user.save
|
126
|
+
@user.tags_on(:customs) # => [<Tag name='same'>,...]
|
127
|
+
@user.tag_counts_on(:customs)
|
128
|
+
User.find_tagged_with("same", :on => :customs) # => [@user]
|
129
|
+
|
130
|
+
=== Tag Ownership
|
131
|
+
|
132
|
+
Tags can have owners:
|
133
|
+
|
134
|
+
class User < ActiveRecord::Base
|
135
|
+
acts_as_tagger
|
136
|
+
end
|
137
|
+
|
138
|
+
class Photo < ActiveRecord::Base
|
139
|
+
acts_as_taggable_on :locations
|
140
|
+
end
|
141
|
+
|
142
|
+
@some_user.tag(@some_photo, :with => "paris, normandy", :on => :locations)
|
143
|
+
@some_user.owned_taggings
|
144
|
+
@some_user.owned_tags
|
145
|
+
@some_photo.locations_from(@some_user)
|
146
|
+
|
147
|
+
== Contributors
|
148
|
+
|
149
|
+
* TomEric (i76) - Maintainer
|
150
|
+
* Michael Bleigh - Original Author
|
151
|
+
* Brendan Lim - Related Objects
|
152
|
+
* Pradeep Elankumaran - Taggers
|
153
|
+
* Sinclair Bain - Patch King
|
154
|
+
|
155
|
+
== Patch Contributors
|
156
|
+
|
157
|
+
* tristanzdunn - Related objects of other classes
|
158
|
+
* azabaj - Fixed migrate down
|
159
|
+
* Peter Cooper - named_scope fix
|
160
|
+
* slainer68 - STI fix
|
161
|
+
* harrylove - migration instructions and fix-ups
|
162
|
+
* lawrencepit - cached tag work
|
163
|
+
|
164
|
+
Copyright (c) 2007-2009 Michael Bleigh (http://mbleigh.com/) and Intridea Inc. (http://intridea.com/), released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec/rake/spectask'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |gemspec|
|
6
|
+
gemspec.name = "ordered-tags"
|
7
|
+
gemspec.summary = "Based on ActsAsTaggableOn"
|
8
|
+
gemspec.description = "Based on ActsAsTaggableOn"
|
9
|
+
gemspec.email = "benoit.benezech@gmail.com"
|
10
|
+
gemspec.homepage = "http://github.com/bbenezech/acts-as-taggable-on"
|
11
|
+
gemspec.authors = ["Benoit Bénézech"]
|
12
|
+
gemspec.files = FileList["[A-Z]*", "{lib,spec,rails}/**/*"] - FileList["**/*.log"]
|
13
|
+
gemspec.add_dependency('acts_as_list')
|
14
|
+
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'Default: run specs'
|
22
|
+
task :default => :spec
|
23
|
+
Spec::Rake::SpecTask.new do |t|
|
24
|
+
t.spec_files = FileList["spec/**/*_spec.rb"]
|
25
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,7 @@
|
|
1
|
+
require 'acts_as_list'
|
2
|
+
require 'acts_as_taggable_on/acts_as_taggable_on'
|
3
|
+
require 'acts_as_taggable_on/acts_as_tagger'
|
4
|
+
require 'acts_as_taggable_on/tag'
|
5
|
+
require 'acts_as_taggable_on/tag_list'
|
6
|
+
require 'acts_as_taggable_on/tags_helper'
|
7
|
+
require 'acts_as_taggable_on/tagging'
|
@@ -0,0 +1,365 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Acts
|
3
|
+
module TaggableOn
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def taggable?
|
10
|
+
false
|
11
|
+
end
|
12
|
+
|
13
|
+
def acts_as_taggable
|
14
|
+
acts_as_taggable_on :tags
|
15
|
+
end
|
16
|
+
|
17
|
+
def acts_as_taggable_on(*args)
|
18
|
+
args.flatten! if args
|
19
|
+
args.compact! if args
|
20
|
+
for tag_type in args
|
21
|
+
tag_type = tag_type.to_s
|
22
|
+
# use aliased_join_table_name for context condition so that sphix can join multiple
|
23
|
+
# tag references from same model without getting an ambiguous column error
|
24
|
+
self.class_eval do
|
25
|
+
has_many "#{tag_type.singularize}_taggings".to_sym, :as => :taggable, :dependent => :destroy,
|
26
|
+
:include => [:tag, :tagger], :conditions => ['#{aliased_join_table_name rescue "taggings"}.context = ?',tag_type], :class_name => "Tagging"
|
27
|
+
has_many "#{tag_type}".to_sym, :through => "#{tag_type.singularize}_taggings".to_sym, :source => :tag
|
28
|
+
end
|
29
|
+
|
30
|
+
self.class_eval <<-RUBY
|
31
|
+
def #{tag_type.singularize}_taggers
|
32
|
+
#{tag_type.singularize}_taggings.map &:tagger
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.taggable?
|
36
|
+
true
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.caching_#{tag_type.singularize}_list?
|
40
|
+
caching_tag_list_on?("#{tag_type}")
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.#{tag_type.singularize}_counts(options={})
|
44
|
+
tag_counts_on('#{tag_type}',options)
|
45
|
+
end
|
46
|
+
|
47
|
+
def #{tag_type.singularize}_list
|
48
|
+
tag_list_on('#{tag_type}')
|
49
|
+
end
|
50
|
+
|
51
|
+
def #{tag_type.singularize}_list=(new_tags)
|
52
|
+
set_tag_list_on('#{tag_type}',new_tags)
|
53
|
+
end
|
54
|
+
|
55
|
+
def #{tag_type.singularize}_counts(options = {})
|
56
|
+
tag_counts_on('#{tag_type}',options)
|
57
|
+
end
|
58
|
+
|
59
|
+
def #{tag_type}_from(owner)
|
60
|
+
tag_list_on('#{tag_type}', owner)
|
61
|
+
end
|
62
|
+
|
63
|
+
def find_related_#{tag_type}(options = {})
|
64
|
+
related_tags_for('#{tag_type}', self.class, options)
|
65
|
+
end
|
66
|
+
alias_method :find_related_on_#{tag_type}, :find_related_#{tag_type}
|
67
|
+
|
68
|
+
def find_related_#{tag_type}_for(klass, options = {})
|
69
|
+
related_tags_for('#{tag_type}', klass, options)
|
70
|
+
end
|
71
|
+
|
72
|
+
def top_#{tag_type}(limit = 10)
|
73
|
+
tag_counts_on('#{tag_type}', :order => 'count desc', :limit => limit.to_i)
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.top_#{tag_type}(limit = 10)
|
77
|
+
tag_counts_on('#{tag_type}', :order => 'count desc', :limit => limit.to_i)
|
78
|
+
end
|
79
|
+
RUBY
|
80
|
+
end
|
81
|
+
|
82
|
+
if respond_to?(:tag_types)
|
83
|
+
write_inheritable_attribute( :tag_types, (tag_types + args).uniq )
|
84
|
+
else
|
85
|
+
self.class_eval do
|
86
|
+
write_inheritable_attribute(:tag_types, args.uniq)
|
87
|
+
class_inheritable_reader :tag_types
|
88
|
+
|
89
|
+
has_many :taggings, :as => :taggable, :dependent => :destroy, :include => :tag
|
90
|
+
has_many :base_tags, :class_name => "Tag", :through => :taggings, :source => :tag
|
91
|
+
|
92
|
+
attr_writer :custom_contexts
|
93
|
+
|
94
|
+
before_save :save_cached_tag_list
|
95
|
+
after_save :save_tags
|
96
|
+
|
97
|
+
if respond_to?(:named_scope)
|
98
|
+
named_scope :tagged_with, lambda{ |*args|
|
99
|
+
find_options_for_find_tagged_with(*args)
|
100
|
+
}
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
include ActiveRecord::Acts::TaggableOn::InstanceMethods
|
105
|
+
extend ActiveRecord::Acts::TaggableOn::SingletonMethods
|
106
|
+
alias_method_chain :reload, :tag_list
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def is_taggable?
|
111
|
+
false
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
module SingletonMethods
|
116
|
+
# Pass either a tag string, or an array of strings or tags
|
117
|
+
#
|
118
|
+
# Options:
|
119
|
+
# :exclude - Find models that are not tagged with the given tags
|
120
|
+
# :match_all - Find models that match all of the given tags, not just one
|
121
|
+
# :conditions - A piece of SQL conditions to add to the query
|
122
|
+
# :on - scopes the find to a context
|
123
|
+
def find_tagged_with(*args)
|
124
|
+
options = find_options_for_find_tagged_with(*args)
|
125
|
+
options.blank? ? [] : find(:all,options)
|
126
|
+
end
|
127
|
+
|
128
|
+
def caching_tag_list_on?(context)
|
129
|
+
column_names.include?("cached_#{context.to_s.singularize}_list")
|
130
|
+
end
|
131
|
+
|
132
|
+
def tag_counts_on(context, options = {})
|
133
|
+
Tag.find(:all, find_options_for_tag_counts(options.merge({:on => context.to_s})))
|
134
|
+
end
|
135
|
+
|
136
|
+
def all_tag_counts(options = {})
|
137
|
+
Tag.find(:all, find_options_for_tag_counts(options))
|
138
|
+
end
|
139
|
+
|
140
|
+
def find_options_for_find_tagged_with(tags, options = {})
|
141
|
+
tags = TagList.from(tags)
|
142
|
+
|
143
|
+
return {} if tags.empty?
|
144
|
+
|
145
|
+
joins = []
|
146
|
+
conditions = []
|
147
|
+
|
148
|
+
context = options.delete(:on)
|
149
|
+
|
150
|
+
|
151
|
+
if options.delete(:exclude)
|
152
|
+
tags_conditions = tags.map { |t| sanitize_sql(["#{Tag.table_name}.name LIKE ?", t]) }.join(" OR ")
|
153
|
+
conditions << "#{table_name}.#{primary_key} NOT IN (SELECT #{Tagging.table_name}.taggable_id FROM #{Tagging.table_name} JOIN #{Tag.table_name} ON #{Tagging.table_name}.tag_id = #{Tag.table_name}.id AND (#{tags_conditions}) WHERE #{Tagging.table_name}.taggable_type = #{quote_value(base_class.name)})"
|
154
|
+
|
155
|
+
else
|
156
|
+
tags.each do |tag|
|
157
|
+
safe_tag = tag.gsub(/[^a-zA-Z0-9]/, '')
|
158
|
+
prefix = "#{safe_tag}_#{rand(1024)}"
|
159
|
+
|
160
|
+
taggings_alias = "#{table_name}_taggings_#{prefix}"
|
161
|
+
tags_alias = "#{table_name}_tags_#{prefix}"
|
162
|
+
|
163
|
+
tagging_join = "JOIN #{Tagging.table_name} #{taggings_alias}" +
|
164
|
+
" ON #{taggings_alias}.taggable_id = #{table_name}.#{primary_key}" +
|
165
|
+
" AND #{taggings_alias}.taggable_type = #{quote_value(base_class.name)}"
|
166
|
+
tagging_join << " AND " + sanitize_sql(["#{taggings_alias}.context = ?", context.to_s]) if context
|
167
|
+
|
168
|
+
tag_join = "JOIN #{Tag.table_name} #{tags_alias}" +
|
169
|
+
" ON #{tags_alias}.id = #{taggings_alias}.tag_id" +
|
170
|
+
" AND " + sanitize_sql(["#{tags_alias}.name like ?", tag])
|
171
|
+
|
172
|
+
joins << tagging_join
|
173
|
+
joins << tag_join
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
taggings_alias, tags_alias = "#{table_name}_taggings_group", "#{table_name}_tags_group"
|
178
|
+
|
179
|
+
if options.delete(:match_all)
|
180
|
+
joins << "LEFT OUTER JOIN #{Tagging.table_name} #{taggings_alias}" +
|
181
|
+
" ON #{taggings_alias}.taggable_id = #{table_name}.#{primary_key}" +
|
182
|
+
" AND #{taggings_alias}.taggable_type = #{quote_value(base_class.name)}"
|
183
|
+
|
184
|
+
group = "#{table_name}.#{primary_key} HAVING COUNT(#{taggings_alias}.taggable_id) = #{tags.size}"
|
185
|
+
end
|
186
|
+
|
187
|
+
{ :joins => joins.join(" "),
|
188
|
+
:group => group,
|
189
|
+
:conditions => conditions.join(" AND ") }.update(options)
|
190
|
+
end
|
191
|
+
|
192
|
+
# Calculate the tag counts for all tags.
|
193
|
+
#
|
194
|
+
# Options:
|
195
|
+
# :start_at - Restrict the tags to those created after a certain time
|
196
|
+
# :end_at - Restrict the tags to those created before a certain time
|
197
|
+
# :conditions - A piece of SQL conditions to add to the query
|
198
|
+
# :limit - The maximum number of tags to return
|
199
|
+
# :order - A piece of SQL to order by. Eg 'tags.count desc' or 'taggings.created_at desc'
|
200
|
+
# :at_least - Exclude tags with a frequency less than the given value
|
201
|
+
# :at_most - Exclude tags with a frequency greater than the given value
|
202
|
+
# :on - Scope the find to only include a certain context
|
203
|
+
def find_options_for_tag_counts(options = {})
|
204
|
+
options.assert_valid_keys :start_at, :end_at, :conditions, :at_least, :at_most, :order, :limit, :on, :id
|
205
|
+
|
206
|
+
scope = scope(:find)
|
207
|
+
start_at = sanitize_sql(["#{Tagging.table_name}.created_at >= ?", options.delete(:start_at)]) if options[:start_at]
|
208
|
+
end_at = sanitize_sql(["#{Tagging.table_name}.created_at <= ?", options.delete(:end_at)]) if options[:end_at]
|
209
|
+
|
210
|
+
taggable_type = sanitize_sql(["#{Tagging.table_name}.taggable_type = ?", base_class.name])
|
211
|
+
taggable_id = sanitize_sql(["#{Tagging.table_name}.taggable_id = ?", options.delete(:id)]) if options[:id]
|
212
|
+
options[:conditions] = sanitize_sql(options[:conditions]) if options[:conditions]
|
213
|
+
|
214
|
+
conditions = [
|
215
|
+
taggable_type,
|
216
|
+
taggable_id,
|
217
|
+
options[:conditions],
|
218
|
+
start_at,
|
219
|
+
end_at
|
220
|
+
]
|
221
|
+
|
222
|
+
conditions = conditions.compact.join(' AND ')
|
223
|
+
conditions = merge_conditions(conditions, scope[:conditions]) if scope
|
224
|
+
|
225
|
+
joins = ["LEFT OUTER JOIN #{Tagging.table_name} ON #{Tag.table_name}.id = #{Tagging.table_name}.tag_id"]
|
226
|
+
joins << sanitize_sql(["AND #{Tagging.table_name}.context = ?",options.delete(:on).to_s]) unless options[:on].nil?
|
227
|
+
|
228
|
+
joins << " INNER JOIN #{table_name} ON #{table_name}.#{primary_key} = #{Tagging.table_name}.taggable_id"
|
229
|
+
unless self.descends_from_active_record?
|
230
|
+
# Current model is STI descendant, so add type checking to the join condition
|
231
|
+
joins << " AND #{table_name}.#{self.inheritance_column} = '#{self.name}'"
|
232
|
+
end
|
233
|
+
|
234
|
+
joins << scope[:joins] if scope && scope[:joins]
|
235
|
+
|
236
|
+
at_least = sanitize_sql(['COUNT(*) >= ?', options.delete(:at_least)]) if options[:at_least]
|
237
|
+
at_most = sanitize_sql(['COUNT(*) <= ?', options.delete(:at_most)]) if options[:at_most]
|
238
|
+
having = [at_least, at_most].compact.join(' AND ')
|
239
|
+
group_by = "#{Tag.table_name}.id HAVING COUNT(*) > 0"
|
240
|
+
group_by << " AND #{having}" unless having.blank?
|
241
|
+
|
242
|
+
{ :select => "#{Tag.table_name}.*, COUNT(*) AS count",
|
243
|
+
:joins => joins.join(" "),
|
244
|
+
:conditions => conditions,
|
245
|
+
:group => group_by,
|
246
|
+
:limit => options[:limit],
|
247
|
+
:order => options[:order]
|
248
|
+
}
|
249
|
+
end
|
250
|
+
|
251
|
+
def is_taggable?
|
252
|
+
true
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
module InstanceMethods
|
257
|
+
|
258
|
+
def tag_types
|
259
|
+
self.class.tag_types
|
260
|
+
end
|
261
|
+
|
262
|
+
def custom_contexts
|
263
|
+
@custom_contexts ||= []
|
264
|
+
end
|
265
|
+
|
266
|
+
def is_taggable?
|
267
|
+
self.class.is_taggable?
|
268
|
+
end
|
269
|
+
|
270
|
+
def add_custom_context(value)
|
271
|
+
custom_contexts << value.to_s unless custom_contexts.include?(value.to_s) or self.class.tag_types.map(&:to_s).include?(value.to_s)
|
272
|
+
end
|
273
|
+
|
274
|
+
def tag_list_on(context, owner=nil)
|
275
|
+
var_name = context.to_s.singularize + "_list"
|
276
|
+
add_custom_context(context)
|
277
|
+
return instance_variable_get("@#{var_name}") unless instance_variable_get("@#{var_name}").nil?
|
278
|
+
|
279
|
+
if !owner && self.class.caching_tag_list_on?(context) and !(cached_value = cached_tag_list_on(context)).nil?
|
280
|
+
instance_variable_set("@#{var_name}", TagList.from(self["cached_#{var_name}"]))
|
281
|
+
else
|
282
|
+
instance_variable_set("@#{var_name}", TagList.new(*tags_on(context, owner).map(&:name)))
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
def tags_on(context, owner=nil)
|
287
|
+
if owner
|
288
|
+
opts = {:conditions => ["#{Tagging.table_name}.context = ? AND #{Tagging.table_name}.tagger_id = ? AND #{Tagging.table_name}.tagger_type = ?",
|
289
|
+
context.to_s, owner.id, owner.class.to_s], :order => "#{Tagging.table_name}.position"}
|
290
|
+
else
|
291
|
+
opts = {:conditions => ["#{Tagging.table_name}.context = ?", context.to_s], :order => "#{Tagging.table_name}.position"}
|
292
|
+
end
|
293
|
+
base_tags.find(:all, opts)
|
294
|
+
end
|
295
|
+
|
296
|
+
def cached_tag_list_on(context)
|
297
|
+
self["cached_#{context.to_s.singularize}_list"]
|
298
|
+
end
|
299
|
+
|
300
|
+
def set_tag_list_on(context,new_list, tagger=nil)
|
301
|
+
instance_variable_set("@#{context.to_s.singularize}_list", TagList.from_owner(tagger, new_list))
|
302
|
+
add_custom_context(context)
|
303
|
+
end
|
304
|
+
|
305
|
+
def tag_counts_on(context, options={})
|
306
|
+
self.class.tag_counts_on(context, options.merge(:id => self.id))
|
307
|
+
end
|
308
|
+
|
309
|
+
def related_tags_for(context, klass, options = {})
|
310
|
+
search_conditions = related_search_options(context, klass, options)
|
311
|
+
|
312
|
+
klass.find(:all, search_conditions)
|
313
|
+
end
|
314
|
+
|
315
|
+
def related_search_options(context, klass, options = {})
|
316
|
+
tags_to_find = self.tags_on(context).collect { |t| t.name }
|
317
|
+
|
318
|
+
exclude_self = "#{klass.table_name}.id != #{self.id} AND" if self.class == klass
|
319
|
+
|
320
|
+
{ :select => "#{klass.table_name}.*, COUNT(#{Tag.table_name}.id) AS count",
|
321
|
+
:from => "#{klass.table_name}, #{Tag.table_name}, #{Tagging.table_name}",
|
322
|
+
:conditions => ["#{exclude_self} #{klass.table_name}.id = #{Tagging.table_name}.taggable_id AND #{Tagging.table_name}.taggable_type = '#{klass.to_s}' AND #{Tagging.table_name}.tag_id = #{Tag.table_name}.id AND #{Tag.table_name}.name IN (?)", tags_to_find],
|
323
|
+
:group => "#{klass.table_name}.id",
|
324
|
+
:order => "count DESC"
|
325
|
+
}.update(options)
|
326
|
+
end
|
327
|
+
|
328
|
+
def save_cached_tag_list
|
329
|
+
self.class.tag_types.map(&:to_s).each do |tag_type|
|
330
|
+
if self.class.send("caching_#{tag_type.singularize}_list?")
|
331
|
+
self["cached_#{tag_type.singularize}_list"] = send("#{tag_type.singularize}_list").to_s
|
332
|
+
end
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
def save_tags
|
337
|
+
(custom_contexts + self.class.tag_types.map(&:to_s)).each do |tag_type|
|
338
|
+
next unless instance_variable_get("@#{tag_type.singularize}_list")
|
339
|
+
owner = instance_variable_get("@#{tag_type.singularize}_list").owner
|
340
|
+
|
341
|
+
self.class.transaction do
|
342
|
+
base_tags.delete(*tags_on(tag_type, owner))
|
343
|
+
new_tag_names = instance_variable_get("@#{tag_type.singularize}_list")
|
344
|
+
new_tag_names.each do |new_tag_name|
|
345
|
+
new_tag = Tag.find_or_create_with_like_by_name(new_tag_name)
|
346
|
+
Tagging.create(:tag_id => new_tag.id, :context => tag_type,
|
347
|
+
:taggable => self, :tagger => owner)
|
348
|
+
end
|
349
|
+
end
|
350
|
+
end
|
351
|
+
|
352
|
+
true
|
353
|
+
end
|
354
|
+
|
355
|
+
def reload_with_tag_list(*args)
|
356
|
+
self.class.tag_types.each do |tag_type|
|
357
|
+
self.instance_variable_set("@#{tag_type.to_s.singularize}_list", nil)
|
358
|
+
end
|
359
|
+
|
360
|
+
reload_without_tag_list(*args)
|
361
|
+
end
|
362
|
+
end
|
363
|
+
end
|
364
|
+
end
|
365
|
+
end
|