diff --git a/lib/acts_as_taggable_on/taggable/ownership.rb b/lib/acts_as_taggable_on/taggable/ownership.rb index 43bddbaaa..7b7ed3dc6 100644 --- a/lib/acts_as_taggable_on/taggable/ownership.rb +++ b/lib/acts_as_taggable_on/taggable/ownership.rb @@ -27,13 +27,16 @@ def #{tag_type}_from(owner) end end - def owner_tags_on(owner, context) + def owner_tags(owner) if owner.nil? - scope = base_tags.where([%(#{ActsAsTaggableOn::Tagging.table_name}.context = ?), context.to_s]) + scope = base_tags else - scope = base_tags.where([%(#{ActsAsTaggableOn::Tagging.table_name}.context = ? AND - #{ActsAsTaggableOn::Tagging.table_name}.tagger_id = ? AND - #{ActsAsTaggableOn::Tagging.table_name}.tagger_type = ?), context.to_s, owner.id, owner.class.base_class.to_s]) + scope = base_tags.where( + "#{ActsAsTaggableOn::Tagging.table_name}" => { + tagger_id: owner.id, + tagger_type: owner.class.base_class.to_s + } + ) end # when preserving tag order, return tags in created order @@ -45,6 +48,14 @@ def owner_tags_on(owner, context) end end + def owner_tags_on(owner, context) + scope = owner_tags(owner).where( + "#{ActsAsTaggableOn::Tagging.table_name}" => { + context: context + } + ) + end + def cached_owned_tag_list_on(context) variable_name = "@owned_#{context}_list" (instance_variable_defined?(variable_name) && instance_variable_get(variable_name)) || instance_variable_set(variable_name, {}) diff --git a/spec/acts_as_taggable_on/single_table_inheritance_spec.rb b/spec/acts_as_taggable_on/single_table_inheritance_spec.rb index 2fbd7101c..c0fbd9095 100644 --- a/spec/acts_as_taggable_on/single_table_inheritance_spec.rb +++ b/spec/acts_as_taggable_on/single_table_inheritance_spec.rb @@ -170,11 +170,27 @@ expect(taggable.tags_from(student)).to eq(%w(ruby scheme)) end + it 'returns all owner tags on the taggable' do + student.tag(taggable, with: 'ruby, scheme', on: :tags) + student.tag(taggable, with: 'skill_one', on: :skills) + student.tag(taggable, with: 'english, spanish', on: :language) + expect(taggable.owner_tags(student).count).to eq(5) + expect(taggable.owner_tags(student).sort == %w(english ruby scheme skill_one spanish)) + end + + it 'returns owner tags on the tagger' do student.tag(taggable, with: 'ruby, scheme', on: :tags) expect(taggable.owner_tags_on(student, :tags).count).to eq(2) end + it 'returns owner tags on the taggable for an array of contexts' do + student.tag(taggable, with: 'ruby, scheme', on: :tags) + student.tag(taggable, with: 'skill_one, skill_two', on: :skills) + expect(taggable.owner_tags_on(student, [:tags, :skills]).count).to eq(4) + expect(taggable.owner_tags_on(student, [:tags, :skills]).sort == %w(ruby scheme skill_one skill_two)) + end + it 'should scope objects returned by tagged_with by owners' do student.tag(taggable, with: 'ruby, scheme', on: :tags) expect(TaggableModel.tagged_with(%w(ruby scheme), owned_by: student).count).to eq(1) @@ -208,4 +224,3 @@ end end end -