Skip to content

Commit

Permalink
Support any :any => true option to find_tagged_with() to match record…
Browse files Browse the repository at this point in the history
…s tagged with tags in a list
  • Loading branch information
kjvarga committed Jan 21, 2010
1 parent a880c31 commit bb49cf0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/acts_as_taggable_on/acts_as_taggable_on.rb
Expand Up @@ -117,6 +117,7 @@ module SingletonMethods
# Pass either a tag string, or an array of strings or tags
#
# Options:
# :any - find models that match any of the given tags
# :exclude - Find models that are not tagged with the given tags
# :match_all - Find models that match all of the given tags, not just one
# :conditions - A piece of SQL conditions to add to the query
Expand Down Expand Up @@ -153,6 +154,10 @@ def find_options_for_find_tagged_with(tags, options = {})
tags_conditions = tag_list.map { |t| sanitize_sql(["#{Tag.table_name}.name LIKE ?", t]) }.join(" OR ")
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)})"

elsif options.delete(:any)
tags_conditions = tag_list.map { |t| sanitize_sql(["#{Tag.table_name}.name LIKE ?", t]) }.join(" OR ")
conditions << "#{table_name}.#{primary_key} 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)})"

else
tags = Tag.named_like_any(tag_list)
return { :conditions => "1 = 0" } unless tags.length == tag_list.length
Expand Down
12 changes: 11 additions & 1 deletion spec/acts_as_taggable_on/taggable_spec.rb
Expand Up @@ -149,7 +149,17 @@
TaggableModel.find_tagged_with("ruby, rails", :order => 'taggable_models.name').should == [bob, frank]
TaggableModel.find_tagged_with(["ruby", "rails"], :order => 'taggable_models.name').should == [bob, frank]
end


it "should be able to find tagged with any tag" do
bob = TaggableModel.create(:name => "Bob", :tag_list => "fitter, happier, more productive", :skill_list => "ruby, rails, css")
frank = TaggableModel.create(:name => "Frank", :tag_list => "weaker, depressed, inefficient", :skill_list => "ruby, rails, css")
steve = TaggableModel.create(:name => 'Steve', :tag_list => 'fitter, happier, more productive', :skill_list => 'c++, java, ruby')

TaggableModel.find_tagged_with(["ruby", "java"], :order => 'taggable_models.name', :any => true).should == [bob, frank, steve]
TaggableModel.find_tagged_with(["c++", "fitter"], :order => 'taggable_models.name', :any => true).should == [bob, steve]
TaggableModel.find_tagged_with(["depressed", "css"], :order => 'taggable_models.name', :any => true).should == [bob, frank]
end

it "should be able to find tagged on a custom tag context" do
bob = TaggableModel.create(:name => "Bob")
bob.set_tag_list_on(:rotors, "spinning, jumping")
Expand Down

0 comments on commit bb49cf0

Please sign in to comment.