From ee3b4966546f800c5893a9a448a845268d1c1a72 Mon Sep 17 00:00:00 2001 From: Bonias <“piotr@galdomedia.pl”> Date: Thu, 27 Feb 2014 10:22:36 +0100 Subject: [PATCH] Model.search("") should not raise exception Consider query like: "users?q=some-string" or "users?q=" and controller content User.search(params[:q]) --- lib/ransack/search.rb | 3 ++- spec/ransack/search_spec.rb | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/ransack/search.rb b/lib/ransack/search.rb index a9edb95..f514d46 100644 --- a/lib/ransack/search.rb +++ b/lib/ransack/search.rb @@ -14,7 +14,8 @@ module Ransack :translate, :to => :base def initialize(object, params = {}, options = {}) - (params ||= {}).delete_if { |k, v| v.blank? && v != false } + params = {} unless params.is_a?(Hash) + params.delete_if { |k, v| v.blank? && v != false } @context = Context.for(object, options) @context.auth_object = options[:auth_object] @base = Nodes::Grouping.new(@context, 'and') diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index 08c9892..d346cb8 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -3,6 +3,12 @@ require 'spec_helper' module Ransack describe Search do + describe "#initialize" do + it "do not raise exception for string :params argument" do + lambda { Search.new(Person, "") }.should_not raise_error + end + end + describe '#build' do it 'creates Conditions for top-level attributes' do search = Search.new(Person, :name_eq => 'Ernie')