Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context constraint to find_related_* methods. #629

Merged
merged 1 commit into from
Feb 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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