diff --git a/CHANGELOG.md b/CHANGELOG.md index a5a373cb9..77bfcb5ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ As such, a _Feature_ would map to either major or minor. A _bug fix_ to a patch. * Breaking Changes * Features * Fixes + * [@mikehale #487 Match_all respects context](https://github.com/mbleigh/acts-as-taggable-on/pull/487) * Misc ### [3.1.0.rc1 / 2014-02-26](https://github.com/mbleigh/acts-as-taggable-on/compare/v3.0.1...v3.1.0.rc1) diff --git a/lib/acts_as_taggable_on/acts_as_taggable_on/core.rb b/lib/acts_as_taggable_on/acts_as_taggable_on/core.rb index ef4046b01..fde06cd90 100644 --- a/lib/acts_as_taggable_on/acts_as_taggable_on/core.rb +++ b/lib/acts_as_taggable_on/acts_as_taggable_on/core.rb @@ -193,6 +193,8 @@ def tagged_with(tags, options = {}) " ON #{taggings_alias}.taggable_id = #{quote}#{table_name}#{quote}.#{primary_key}" + " AND #{taggings_alias}.taggable_type = #{quote_value(base_class.name, nil)}" + joins << " AND " + sanitize_sql(["#{taggings_alias}.context = ?", context.to_s]) if context + group_columns = ActsAsTaggableOn::Tag.using_postgresql? ? grouped_column_names_for(self) : "#{table_name}.#{primary_key}" group = group_columns having = "COUNT(#{taggings_alias}.taggable_id) = #{tags.size}" diff --git a/spec/acts_as_taggable_on/taggable_spec.rb b/spec/acts_as_taggable_on/taggable_spec.rb index 33e93e0ae..01300b131 100644 --- a/spec/acts_as_taggable_on/taggable_spec.rb +++ b/spec/acts_as_taggable_on/taggable_spec.rb @@ -430,6 +430,14 @@ TaggableModel.tagged_with("fitter, happier", :match_all => true).to_a.should == [steve] end + it "should be able to find tagged with only the matching tags for a context" do + bob = TaggableModel.create(:name => "Bob", :tag_list => "lazy, happier", :skill_list => "ruby, rails, css") + frank = TaggableModel.create(:name => "Frank", :tag_list => "fitter, happier, inefficient", :skill_list => "css") + steve = TaggableModel.create(:name => 'Steve', :tag_list => "fitter, happier", :skill_list => "ruby, rails, css") + + TaggableModel.tagged_with("css", :on => :skills, :match_all => true).to_a.should == [frank] + end + it "should be able to find tagged with some excluded tags" do bob = TaggableModel.create(:name => "Bob", :tag_list => "happier, lazy") frank = TaggableModel.create(:name => "Frank", :tag_list => "happier")