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

Given a resource: get all tags by owner and get owner tags for multiple contexts #771

Merged
merged 3 commits into from
Aug 7, 2016
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
21 changes: 16 additions & 5 deletions lib/acts_as_taggable_on/taggable/ownership.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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