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

Use eq instead of matches for non-wildcard searches #1101

Open
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ As such, _Breaking Changes_ are major. _Features_ would map to either major or m
* Features
* [@glampr Add support for prefix and suffix searches alongside previously supported containment (wildcard) searches](https://github.com/mbleigh/acts-as-taggable-on/pull/1082)
* [@donquxiote Add support for horizontally sharded databases](https://github.com/mbleigh/acts-as-taggable-on/pull/1079)
* Fixes
* [@flickgradley Use eq instead of matches for non-wildcard searches](https://github.com/mbleigh/acts-as-taggable-on/pull/1101)

### [v9.0.1) / 2022-01-07](https://github.com/mbleigh/acts-as-taggable-on/compare/v9.0.0..v9.0.1)
* Fixes
Expand Down
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