Skip to content

Commit

Permalink
Add context constraint to find_related_* methods. Fixes #628
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rky-pl committed Feb 11, 2015
1 parent 4d24051 commit 3f9632a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/acts_as_taggable_on/taggable/related.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/acts_as_taggable_on/related_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3f9632a

Please sign in to comment.