feat(controller): add read_param_value helper

This commit is contained in:
Paul Chavard 2022-05-03 10:09:58 +02:00
parent cfeef3bf5f
commit edba12705b

View file

@ -364,4 +364,20 @@ class ApplicationController < ActionController::Base
def set_customizable_view_path
prepend_view_path "app/custom_views"
end
# Extract a value from params based on the "path"
#
# params: { dossiers: { champs_attributes: { 1234 => { value: "hello" } } } }
#
# Usage: read_param_value("dossiers[champs_attributes][1234]", "value")
def read_param_value(path, name)
parts = path.split(/\[|\]\[|\]/) + [name]
parts.reduce(params) do |value, part|
if part.to_i != 0
value[part.to_i] || value[part]
else
value[part]
end
end
end
end