Search.new and the ransack scope no longer modify the params hash

Closes #378.
This commit is contained in:
Jon Atack 2014-08-26 23:12:49 +02:00
parent 64827eeffe
commit 11398617a1
4 changed files with 21 additions and 6 deletions

View File

@ -12,9 +12,7 @@ module Ransack
end
def ransack(params = {}, options = {})
params = params.presence || {}
Search.new(self, params ? params.delete_if {
|k, v| v.blank? && v != false } : params, options)
Search.new(self, params, options)
end
def ransacker(name, opts = {}, &block)

View File

@ -14,9 +14,12 @@ module Ransack
:translate, :to => :base
def initialize(object, params = {}, options = {})
params = {} unless params.is_a?(Hash)
(params ||= {})
.delete_if { |k, v| [*v].all? { |i| i.blank? && i != false } }
if params.is_a? Hash
params = params.dup
params.delete_if { |k, v| [*v].all?{|i| i.blank? && i != false } }
else
params = {}
end
@context = options[:context] || Context.for(object, options)
@context.auth_object = options[:auth_object]
@base = Nodes::Grouping.new(@context, options[:grouping] || 'and')

View File

@ -50,6 +50,14 @@ module Ransack
end
end
it 'does not raise exception for string :params argument' do
lambda { Person.search('') }.should_not raise_error
end
it 'does not modify the parameters' do
params = { :name_eq => '' }
expect { Person.search(params)}.not_to change { params }
end
end
describe '#ransacker' do

View File

@ -183,6 +183,12 @@ module Ransack
specify { expect { subject }.not_to raise_error }
end
end
it 'does not modify the parameters' do
params = { :name_eq => '' }
expect { Search.new(Person, params)}.not_to change { params }
end
end
describe '#result' do