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

Scope tags for taggable type #923

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions lib/acts_as_taggable_on/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def validates_name_uniqueness?
### SCOPES:
scope :most_used, ->(limit = 20) { order('taggings_count desc').limit(limit) }
scope :least_used, ->(limit = 20) { order('taggings_count asc').limit(limit) }
scope :for_taggable_type, -> (type = nil) { joins(:taggings).where('taggings.taggable_type' => Array.wrap(type)) }

def self.named(name)
if ActsAsTaggableOn.strict_case_match
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_taggable_on/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ActsAsTaggableOn
VERSION = '6.0.1'
VERSION = '6.0.2'
end
24 changes: 23 additions & 1 deletion spec/acts_as_taggable_on/tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,29 @@
@tag = ActsAsTaggableOn::Tag.new
@user = TaggableModel.create(name: 'Pablo')
end



# scopes
describe 'for_taggable_type' do
before(:each) do
@tag_1 = ActsAsTaggableOn::Tag.create(name: 'Awesome')
@tag_2 = ActsAsTaggableOn::Tag.create(name: 'notSoCool')
@tagging_1 = ActsAsTaggableOn::Tagging.create(taggable: @user, tag: @tag_1, context: 'tags')
@tagging_2 = ActsAsTaggableOn::Tagging.create(taggable_type: 'AnyClass', taggable_id: 1, tag: @tag_2, context: 'tags')
# this one is not to be found...
@tagging_3 = ActsAsTaggableOn::Tagging.create(taggable_type: 'DontCare', taggable_id: 1, tag: @tag_2, context: 'tags')
end

specify "finds one given taggable type" do
expect(ActsAsTaggableOn::Tag.for_taggable_type(TaggableModel.to_s)).to match_array([@tag_1])
end

specify "finds multiple give taggable type" do
expect(ActsAsTaggableOn::Tag.for_taggable_type([TaggableModel.to_s, @tagging_2.taggable_type])).to match_array([@tag_1, @tag_2])
end


end

describe 'named like any' do
context 'case insensitive collation and unique index on tag name', if: using_case_insensitive_collation? do
Expand Down