mirror of
https://github.com/activerecord-hackery/ransack.git
synced 2022-11-09 13:47:45 -05:00
Bullet proof: don’t crash if blank string is passed to SomeModel.search
method
This commit is contained in:
parent
e9b568d927
commit
b20e303d42
4 changed files with 19 additions and 1 deletions
|
@ -12,6 +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)
|
||||
end
|
||||
|
|
|
@ -45,7 +45,7 @@ module Ransack
|
|||
raise TypeError, "First argument must be a Ransack::Search!" unless
|
||||
Search === search
|
||||
|
||||
search_params = params[search.context.search_key] ||
|
||||
search_params = params[search.context.search_key].presence ||
|
||||
{}.with_indifferent_access
|
||||
|
||||
attr_name = attribute.to_s
|
||||
|
|
|
@ -79,6 +79,10 @@ module Ransack
|
|||
s = Person.search(nil)
|
||||
end
|
||||
|
||||
it "should function correctly when a blank string is passed in" do
|
||||
s = Person.search("")
|
||||
end
|
||||
|
||||
it "should function correctly when using fields with dots in them" do
|
||||
s = Person.search(:email_cont => "example.com")
|
||||
s.result.exists?.should be_true
|
||||
|
|
|
@ -71,6 +71,19 @@ module Ransack
|
|||
}
|
||||
end
|
||||
|
||||
describe '#sort_link works even if search params are really blank string' do
|
||||
before { @controller.view_context.params[:q] = "" }
|
||||
specify {
|
||||
expect {
|
||||
@controller.view_context.sort_link(
|
||||
Person.search(@controller.view_context.params[:q]),
|
||||
:name,
|
||||
:controller => 'people'
|
||||
)
|
||||
}.not_to raise_error
|
||||
}
|
||||
end
|
||||
|
||||
describe '#sort_link with default search_key defined as string' do
|
||||
subject {
|
||||
@controller.view_context.sort_link(
|
||||
|
|
Loading…
Reference in a new issue