mbleigh-acts-as-taggable-on 1.0.3 → 1.0.5
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/README +1 -1
- data/lib/acts_as_taggable_on/acts_as_taggable_on.rb +17 -9
- data/spec/acts_as_taggable_on/taggable_spec.rb +12 -1
- metadata +1 -1
data/README
CHANGED
@@ -19,9 +19,11 @@ module ActiveRecord
|
|
19
19
|
args.compact! if args
|
20
20
|
for tag_type in args
|
21
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
|
22
24
|
self.class_eval do
|
23
25
|
has_many "#{tag_type.singularize}_taggings".to_sym, :as => :taggable, :dependent => :destroy,
|
24
|
-
:include => :tag, :conditions => ["context = ?
|
26
|
+
:include => :tag, :conditions => ['#{aliased_join_table_name rescue "taggings"}.context = ?',tag_type], :class_name => "Tagging"
|
25
27
|
has_many "#{tag_type}".to_sym, :through => "#{tag_type.singularize}_taggings".to_sym, :source => :tag
|
26
28
|
end
|
27
29
|
|
@@ -62,6 +64,10 @@ module ActiveRecord
|
|
62
64
|
def find_related_#{tag_type}_for(klass, options = {})
|
63
65
|
related_tags_for('#{tag_type}', klass, options)
|
64
66
|
end
|
67
|
+
|
68
|
+
def top_#{tag_type}(limit = 10)
|
69
|
+
tag_counts_on('#{tag_type}', :order => 'count desc', :limit => limit.to_i)
|
70
|
+
end
|
65
71
|
RUBY
|
66
72
|
end
|
67
73
|
|
@@ -164,16 +170,18 @@ module ActiveRecord
|
|
164
170
|
# :at_most - Exclude tags with a frequency greater than the given value
|
165
171
|
# :on - Scope the find to only include a certain context
|
166
172
|
def find_options_for_tag_counts(options = {})
|
167
|
-
options.assert_valid_keys :start_at, :end_at, :conditions, :at_least, :at_most, :order, :limit, :on
|
173
|
+
options.assert_valid_keys :start_at, :end_at, :conditions, :at_least, :at_most, :order, :limit, :on, :id
|
168
174
|
|
169
175
|
scope = scope(:find)
|
170
176
|
start_at = sanitize_sql(["#{Tagging.table_name}.created_at >= ?", options.delete(:start_at)]) if options[:start_at]
|
171
177
|
end_at = sanitize_sql(["#{Tagging.table_name}.created_at <= ?", options.delete(:end_at)]) if options[:end_at]
|
172
178
|
|
173
|
-
|
179
|
+
taggable_type = sanitize_sql(["#{Tagging.table_name}.taggable_type = ?", base_class.name])
|
180
|
+
taggable_id = sanitize_sql(["#{Tagging.table_name}.taggable_id = ?", options.delete(:id)]) if options[:id]
|
174
181
|
|
175
182
|
conditions = [
|
176
|
-
|
183
|
+
taggable_type,
|
184
|
+
taggable_id,
|
177
185
|
options[:conditions],
|
178
186
|
start_at,
|
179
187
|
end_at
|
@@ -197,7 +205,7 @@ module ActiveRecord
|
|
197
205
|
:joins => joins.join(" "),
|
198
206
|
:conditions => conditions,
|
199
207
|
:group => group_by
|
200
|
-
}
|
208
|
+
}
|
201
209
|
end
|
202
210
|
|
203
211
|
def is_taggable?
|
@@ -254,8 +262,8 @@ module ActiveRecord
|
|
254
262
|
add_custom_context(context)
|
255
263
|
end
|
256
264
|
|
257
|
-
def tag_counts_on(context,options={})
|
258
|
-
self.class.tag_counts_on(context,
|
265
|
+
def tag_counts_on(context, options={})
|
266
|
+
self.class.tag_counts_on(context, options.merge(:id => self.id))
|
259
267
|
end
|
260
268
|
|
261
269
|
def related_tags_for(context, klass, options = {})
|
@@ -290,7 +298,7 @@ module ActiveRecord
|
|
290
298
|
next unless instance_variable_get("@#{tag_type.singularize}_list")
|
291
299
|
owner = instance_variable_get("@#{tag_type.singularize}_list").owner
|
292
300
|
new_tag_names = instance_variable_get("@#{tag_type.singularize}_list") - tags_on(tag_type).map(&:name)
|
293
|
-
old_tags = tags_on(tag_type).reject { |tag| instance_variable_get("@#{tag_type.singularize}_list").include?(tag.name) }
|
301
|
+
old_tags = tags_on(tag_type, owner).reject { |tag| instance_variable_get("@#{tag_type.singularize}_list").include?(tag.name) }
|
294
302
|
|
295
303
|
self.class.transaction do
|
296
304
|
base_tags.delete(*old_tags) if old_tags.any?
|
@@ -315,4 +323,4 @@ module ActiveRecord
|
|
315
323
|
end
|
316
324
|
end
|
317
325
|
end
|
318
|
-
end
|
326
|
+
end
|
@@ -105,6 +105,17 @@ describe "Taggable" do
|
|
105
105
|
bob.save
|
106
106
|
TaggableModel.find_tagged_with("spinning", :on => :rotors).should_not be_empty
|
107
107
|
end
|
108
|
+
|
109
|
+
it "should be able to use named scopes to chain tag finds" do
|
110
|
+
bob = TaggableModel.create(:name => "Bob", :tag_list => "fitter, happier, more productive", :skill_list => "ruby, rails, css")
|
111
|
+
frank = TaggableModel.create(:name => "Frank", :tag_list => "weaker, depressed, inefficient", :skill_list => "ruby, rails, css")
|
112
|
+
steve = TaggableModel.create(:name => 'Steve', :tag_list => 'fitter, happier, more productive', :skill_list => 'c++, java, python')
|
113
|
+
|
114
|
+
# Let's only find those productive Rails developers
|
115
|
+
TaggableModel.tagged_with('rails', :on => :skills).all(:order => 'taggable_models.name').should == [bob, frank]
|
116
|
+
TaggableModel.tagged_with('happier', :on => :tags).all(:order => 'taggable_models.name').should == [bob, steve]
|
117
|
+
TaggableModel.tagged_with('rails', :on => :skills).tagged_with('happier', :on => :tags).should == [bob]
|
118
|
+
end
|
108
119
|
|
109
120
|
describe "Single Table Inheritance" do
|
110
121
|
before do
|
@@ -133,4 +144,4 @@ describe "Taggable" do
|
|
133
144
|
AlteredInheritingTaggableModel.find_tagged_with("fork", :on => :parts).first.should == @inherited_different
|
134
145
|
end
|
135
146
|
end
|
136
|
-
end
|
147
|
+
end
|