Commit afd75dcd by Vlad Vanca Committed by ifool

Fix issue with boolean params using a default with block that returns false

parent e31bc384
......@@ -19,8 +19,20 @@ module RailsParam
begin
params[name] = coerce(params[name], type, options)
params[name] = (options[:default].call if options[:default].respond_to?(:call)) || options[:default] if params[name].nil? and check_param_presence?(options[:default])
params[name] = options[:transform].to_proc.call(params[name]) if params[name] and options[:transform]
# set default
if options[:default].respond_to?(:call)
params[name] = options[:default].call
elsif params[name].nil? && check_param_presence?(options[:default])
params[name] = options[:default]
end
# apply tranformation
if params[name] && options[:transform]
params[name] = options[:transform].to_proc.call(params[name])
end
# validate
validate!(params[name], options)
if block_given?
......
......@@ -46,8 +46,8 @@ describe RailsParam::Param do
context "with a block" do
it "defaults to the block value" do
allow(controller).to receive(:params).and_return({})
controller.param! :word, String, default: lambda { "foo" }
expect(controller.params["word"]).to eql("foo")
controller.param! :foo, :boolean, default: lambda { false }
expect(controller.params["foo"]).to eql(false)
end
end
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment