From 1c0645b1775cdd9105b7e3b30f8d90ad6854db76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Matyas?= Date: Tue, 10 Feb 2015 15:45:33 +0100 Subject: [PATCH] Add context constraint to find_related_* methods. Fixes mbleigh/acts-as-taggable-on#628 --- lib/acts_as_taggable_on/taggable/related.rb | 2 +- spec/acts_as_taggable_on/related_spec.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/acts_as_taggable_on/taggable/related.rb b/lib/acts_as_taggable_on/taggable/related.rb index 9346fd6f6..8fb02d35a 100644 --- a/lib/acts_as_taggable_on/taggable/related.rb +++ b/lib/acts_as_taggable_on/taggable/related.rb @@ -43,7 +43,7 @@ def matching_contexts_for(search_context, result_context, klass, options = {}) def related_tags_for(context, klass, options = {}) tags_to_ignore = Array.wrap(options[:ignore]).map(&:to_s) || [] tags_to_find = tags_on(context).map { |t| t.name }.reject { |t| tags_to_ignore.include? t } - related_where(klass, ["#{exclude_self(klass, id)} #{klass.table_name}.#{klass.primary_key} = #{ActsAsTaggableOn::Tagging.table_name}.taggable_id AND #{ActsAsTaggableOn::Tagging.table_name}.taggable_type = '#{klass.base_class}' AND #{ActsAsTaggableOn::Tagging.table_name}.tag_id = #{ActsAsTaggableOn::Tag.table_name}.#{ActsAsTaggableOn::Tag.primary_key} AND #{ActsAsTaggableOn::Tag.table_name}.name IN (?)", tags_to_find]) + related_where(klass, ["#{exclude_self(klass, id)} #{klass.table_name}.#{klass.primary_key} = #{ActsAsTaggableOn::Tagging.table_name}.taggable_id AND #{ActsAsTaggableOn::Tagging.table_name}.taggable_type = '#{klass.base_class}' AND #{ActsAsTaggableOn::Tagging.table_name}.tag_id = #{ActsAsTaggableOn::Tag.table_name}.#{ActsAsTaggableOn::Tag.primary_key} AND #{ActsAsTaggableOn::Tag.table_name}.name IN (?) AND #{ActsAsTaggableOn::Tagging.table_name}.context = ?", tags_to_find, context]) end private diff --git a/spec/acts_as_taggable_on/related_spec.rb b/spec/acts_as_taggable_on/related_spec.rb index dafe843a5..0a3fc0332 100644 --- a/spec/acts_as_taggable_on/related_spec.rb +++ b/spec/acts_as_taggable_on/related_spec.rb @@ -42,6 +42,15 @@ expect(taggable1.find_related_tags_for(OtherTaggableModel)).to_not include(taggable2) end + it 'should find other related objects based on tags only from particular context' do + taggable1 = TaggableModel.create!(name: 'Taggable 1',tag_list: 'one, two') + taggable2 = TaggableModel.create!(name: 'Taggable 2',tag_list: 'three, four', skill_list: 'one, two') + taggable3 = TaggableModel.create!(name: 'Taggable 3',tag_list: 'one, four') + + expect(taggable1.find_related_tags).to include(taggable3) + expect(taggable1.find_related_tags).to_not include(taggable2) + end + shared_examples "a collection" do it do