Fix issue #190 and #195

This resolves issues where an integer column would raise an exception
when handled by Ransack::Constants.escape_wildcards.
This commit is contained in:
Kevin Sjöberg 2013-02-06 11:26:06 +01:00
parent 7232269a17
commit 53ef01b9e6
3 changed files with 22 additions and 5 deletions

View File

@ -23,7 +23,7 @@ module Ransack
module_function
# replace % \ to \% \\
def escape_wildcards(unescaped)
unescaped.gsub(/([\\|\%|.])/, '\\\\\\1')
unescaped.to_s.gsub(/([\\|\%|.])/, '\\\\\\1')
end
end
end

View File

@ -7,6 +7,17 @@ module Ransack
@s = Search.new(Person)
end
shared_examples 'wildcard escaping' do |method, regexp|
it 'automatically converts integers to strings' do
subject.parent_id_cont = 1
expect { subject.result }.to_not raise_error
end
it "escapes '%', '.' and '\\\\' in value" do
subject.public_send(:"#{method}=", '%._\\')
subject.result.to_sql.should match(regexp)
end
end
describe 'eq' do
it 'generates an equality condition for boolean true' do
@s.awesome_eq = true
@ -25,17 +36,21 @@ module Ransack
end
describe 'cont' do
it_has_behavior 'wildcard escaping', :name_cont, /"people"."name" LIKE '%\\%\\._\\\\%'/ do
subject { @s }
end
it 'generates a LIKE query with value surrounded by %' do
@s.name_cont = 'ric'
@s.result.to_sql.should match /"people"."name" LIKE '%ric%'/
end
it 'escapes %, . and \\ in value' do
@s.name_cont = '%._\\'
@s.result.to_sql.should match /"people"."name" LIKE '%\\%\\._\\\\%'/
end
end
describe 'not_cont' do
it_has_behavior 'wildcard escaping', :name_not_cont, /"people"."name" NOT LIKE '%\\%\\._\\\\%'/ do
subject { @s }
end
it 'generates a NOT LIKE query with value surrounded by %' do
@s.name_not_cont = 'ric'
@s.result.to_sql.should match /"people"."name" NOT LIKE '%ric%'/

View File

@ -20,6 +20,8 @@ Sham.define do
end
RSpec.configure do |config|
config.alias_it_behaves_like_to :it_has_behavior, 'has behavior'
config.before(:suite) do
puts '=' * 80
puts "Running specs against ActiveRecord #{ActiveRecord::VERSION::STRING} and ARel #{Arel::VERSION}..."