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 011ae51e8..4408c6772 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 @@ -48,7 +48,7 @@ def tags_match_type def escaped_tag(tag) tag = tag.downcase unless ActsAsTaggableOn.strict_case_match - tag.gsub(/[!%_]/) { |x| '!' + x } + ActsAsTaggableOn::Utils.escape_like(tag) end def adjust_taggings_alias(taggings_alias) diff --git a/lib/acts_as_taggable_on/utils.rb b/lib/acts_as_taggable_on/utils.rb index 38d68eb06..df423cb74 100644 --- a/lib/acts_as_taggable_on/utils.rb +++ b/lib/acts_as_taggable_on/utils.rb @@ -30,7 +30,11 @@ def like_operator # escape _ and % characters in strings, since these are wildcards in SQL. def escape_like(str) - str.gsub(/[!%_]/) { |x| '!' + x } + str.gsub(/[!%_]/) { |x| escape_replacement + x } + end + + def escape_replacement + using_postgresql? ? '\\' : '!' end end end diff --git a/spec/acts_as_taggable_on/utils_spec.rb b/spec/acts_as_taggable_on/utils_spec.rb index fa23f897e..ecd95b03c 100644 --- a/spec/acts_as_taggable_on/utils_spec.rb +++ b/spec/acts_as_taggable_on/utils_spec.rb @@ -20,4 +20,16 @@ expect(ActsAsTaggableOn::Utils.sha_prefix('puppies')).not_to eq(ActsAsTaggableOn::Utils.sha_prefix('kittens')) end end + + describe '#escape_replacement' do + it 'should return ! when the adapter is not PostgreSQL' do + allow(ActsAsTaggableOn::Utils.connection).to receive(:adapter_name) { 'MySQL' } + expect(ActsAsTaggableOn::Utils.escape_replacement).to eq('!') + end + + it 'should return \\ when the adapter is PostgreSQL' do + allow(ActsAsTaggableOn::Utils.connection).to receive(:adapter_name) { 'PostgreSQL' } + expect(ActsAsTaggableOn::Utils.escape_replacement).to eq('\\') + end + end end