I have the following function, and I have a hunch it can be written more concisely in Ruby.
def template_params
filtered_params = params[:template].permit!
filtered_params[:published] ||= false
filtered_params
end
The end result is that filtered_params
has published: false
if the params hash doesn't have published
as a key.
I'm wondering if there is some kind of conditional merge w/ Ruby to turn that code into a 1-liner.
Essentially, the params hash includes "what has been changed" and when :published
becomes false (it's a checkbox that gets unchecked), it naturally doesn't get included in the params hash. I wanted to pass in the fact that :published
became false when it got unchecked.
I do not need to pass in published: false
by default, because whatever the default is, is fine. It is the changed state that I need, but from false to true. The alternative works: if the checkbox is checked, it becomes true and gets passed through without issue.
This is more an exercise of how can I turn this bit of code into something more "elegant" and maybe a 1-liner. As it stands, it does exactly what I need it to do.