Skip to content

Commit

Permalink
Use eq instead of matches for non-wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
flickgradley committed Mar 3, 2023
1 parent 5ce5260 commit 139b545
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def tag_match_type(tag)
if options[:wild].present?
matches_attribute.matches(wildcard_escaped_tag(tag), '!', ActsAsTaggableOn.strict_case_match)
else
matches_attribute.matches(escaped_tag(tag), '!', ActsAsTaggableOn.strict_case_match)
tag = tag.downcase unless ActsAsTaggableOn.strict_case_match
matches_attribute.eq(tag)
end
end

Expand All @@ -48,9 +49,9 @@ def tags_match_type
wildcard_escaped_tag(tag)
end, '!', ActsAsTaggableOn.strict_case_match)
else
matches_attribute.matches_any(tag_list.map do |tag|
escaped_tag(tag).to_s
end, '!', ActsAsTaggableOn.strict_case_match)
matches_attribute.eq_any(tag_list.map do |tag|
ActsAsTaggableOn.strict_case_match ? tag : tag.downcase
end)
end
end

Expand Down
28 changes: 28 additions & 0 deletions spec/acts_as_taggable_on/taggable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,21 @@
TaggableModel.create(name: 'Frank', tag_list: 'Ruby')

expect(ActsAsTaggableOn::Tag.all.size).to eq(1)
expect(TaggableModel.tagged_with('ruby').size).to eq(2)
expect(TaggableModel.tagged_with('ruby').to_a).to eq(TaggableModel.tagged_with('Ruby').to_a)
end

it 'should care about case when strict case matching is enabled' do
ActsAsTaggableOn.strict_case_match = true
bob = TaggableModel.create(name: 'Bob', tag_list: 'ruby')
frank = TaggableModel.create(name: 'Frank', tag_list: 'Ruby')

expect(ActsAsTaggableOn::Tag.all.size).to eq(2)
expect(TaggableModel.tagged_with('ruby').to_a).to eq([bob])
expect(TaggableModel.tagged_with('Ruby').to_a).to eq([frank])
expect(TaggableModel.tagged_with('RUBY').to_a.size).to eq(0)
end

it 'should be able to find by tags with other joins in the query' do
@taggable.skill_list = 'ruby, rails, css'
@taggable.tag_list = 'bob, charlie'
Expand Down Expand Up @@ -460,6 +472,22 @@
expect(TaggableModel.tagged_with(%w(depressed css), order: 'taggable_models.name', any: true).to_a).to eq([bob, frank])
end

it 'should be able to find tagged with any tag when strict case matching is enabled' do
ActsAsTaggableOn.strict_case_match = true
bob = TaggableModel.create(name: 'Bob', tag_list: 'fitter, happier, more productive', skill_list: 'ruby, rails, css')
frank = TaggableModel.create(name: 'Frank', tag_list: 'weaker, depressed, inefficient', skill_list: 'ruby, rails, css')
steve = TaggableModel.create(name: 'Steve', tag_list: 'fitter, happier, more productive', skill_list: 'c++, java, ruby')

expect(TaggableModel.tagged_with(%w(ruby java), order: 'taggable_models.name', any: true).to_a).to eq([bob, frank, steve])
expect(TaggableModel.tagged_with(%w(c++ fitter), order: 'taggable_models.name', any: true).to_a).to eq([bob, steve])
expect(TaggableModel.tagged_with(%w(depressed css), order: 'taggable_models.name', any: true).to_a).to eq([bob, frank])

expect(TaggableModel.tagged_with(%w(RUBY java), order: 'taggable_models.name', any: true).to_a).to eq([steve])
expect(TaggableModel.tagged_with(%w(c++ FITTER), order: 'taggable_models.name', any: true).to_a).to eq([steve])
expect(TaggableModel.tagged_with(%w(depressed CSS), order: 'taggable_models.name', any: true).to_a).to eq([frank])
expect(TaggableModel.tagged_with(%w(happier CSS), order: 'taggable_models.name', any: true).to_a).to eq([bob, steve])
end

it 'should be able to order by number of matching tags when matching any' do
bob = TaggableModel.create(name: 'Bob', tag_list: 'fitter, happier, more productive', skill_list: 'ruby, rails, css')
frank = TaggableModel.create(name: 'Frank', tag_list: 'weaker, depressed, inefficient', skill_list: 'ruby, rails, css')
Expand Down

0 comments on commit 139b545

Please sign in to comment.