From 11398617a14b58cecb4d37c75f3df7b8a1054895 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Tue, 26 Aug 2014 23:12:49 +0200 Subject: [PATCH] Search.new and the ransack scope no longer modify the params hash Closes #378. --- lib/ransack/adapters/active_record/base.rb | 4 +--- lib/ransack/search.rb | 9 ++++++--- spec/ransack/adapters/active_record/base_spec.rb | 8 ++++++++ spec/ransack/search_spec.rb | 6 ++++++ 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/ransack/adapters/active_record/base.rb b/lib/ransack/adapters/active_record/base.rb index 75383f1..6bdb8fb 100644 --- a/lib/ransack/adapters/active_record/base.rb +++ b/lib/ransack/adapters/active_record/base.rb @@ -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) diff --git a/lib/ransack/search.rb b/lib/ransack/search.rb index e8e5a76..bd99fce 100644 --- a/lib/ransack/search.rb +++ b/lib/ransack/search.rb @@ -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') diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index 551cf3c..789e5a8 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -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 diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index 2a810a0..c443a1f 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -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