Skip to content

Commit

Permalink
Merge pull request #771 from brilyuhns/add_tags_by_owner
Browse files Browse the repository at this point in the history
Given a resource: get all tags by owner and get owner tags for multiple contexts
  • Loading branch information
seuros committed Aug 7, 2016
2 parents a489556 + 65669df commit 67cb90a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
21 changes: 16 additions & 5 deletions lib/acts_as_taggable_on/taggable/ownership.rb
Expand Up @@ -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
Expand All @@ -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, {})
Expand Down
17 changes: 16 additions & 1 deletion spec/acts_as_taggable_on/single_table_inheritance_spec.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -208,4 +224,3 @@
end
end
end

0 comments on commit 67cb90a

Please sign in to comment.