Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request mbleigh#697 from zapnap/for_context
Browse files Browse the repository at this point in the history
scope tags to specific tagging context
  • Loading branch information
seuros committed Nov 11, 2015
2 parents 1dfd5ec + 0d0805d commit c788afc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/acts_as_taggable_on/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def self.named_like_any(list)
where(clause)
end

def self.for_context(context)
joins(:taggings).
where(["taggings.context = ?", context]).
select("DISTINCT tags.*")
end

### CLASS METHODS:

def self.find_or_create_with_like_by_name(name)
Expand Down
15 changes: 15 additions & 0 deletions spec/acts_as_taggable_on/tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@
end
end

describe 'for context' do
before(:each) do
@user.skill_list.add('ruby')
@user.save
end

it 'should return tags that have been used in the given context' do
expect(ActsAsTaggableOn::Tag.for_context('skills').all.map(&:name)).to include('ruby')
end

it 'should not return tags that have been used in other contexts' do
expect(ActsAsTaggableOn::Tag.for_context('needs').all.map(&:name)).to_not include('ruby')
end
end

describe 'find or create by name' do
before(:each) do
@tag.name = 'awesome'
Expand Down

0 comments on commit c788afc

Please sign in to comment.