Skip to content

Commit

Permalink
improve postgres escape sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
rsl committed Feb 8, 2018
1 parent 38fefff commit cc7a2c0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion lib/acts_as_taggable_on/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions spec/acts_as_taggable_on/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cc7a2c0

Please sign in to comment.