diff --git a/CHANGELOG.md b/CHANGELOG.md index 468c60f7..f856e8c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/acts_as_taggable_on/taggable/tagged_with_query/query_base.rb b/lib/acts_as_taggable_on/taggable/tagged_with_query/query_base.rb index 77614f0d..f2834d94 100644 --- a/lib/acts_as_taggable_on/taggable/tagged_with_query/query_base.rb +++ b/lib/acts_as_taggable_on/taggable/tagged_with_query/query_base.rb @@ -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 @@ -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 diff --git a/spec/acts_as_taggable_on/taggable_spec.rb b/spec/acts_as_taggable_on/taggable_spec.rb index efa3bd6f..df5ce341 100644 --- a/spec/acts_as_taggable_on/taggable_spec.rb +++ b/spec/acts_as_taggable_on/taggable_spec.rb @@ -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' @@ -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')