Files
nairobi/app/controllers/admin/settings_controller.rb
Javi Martín 11832cc07d Make it easier to customize allowed parameters
When customizing CONSUL, one of the most common actions is adding a new
field to a form.

This requires modifying the permitted/allowed parameters. However, in
most cases, the method returning these parameters returned an instance
of `ActionController::Parameters`, so adding more parameters to it
wasn't easy.

So customizing the code required copying the method returning those
parameters and adding the new ones. For example:

```
def something_params
  params.require(:something).permit(
    :one_consul_attribute,
    :another_consul_attribute,
    :my_custom_attribute
  )
end
```

This meant that, if the `something_params` method changed in CONSUL, the
customization of this method had to be updated as well.

So we're extracting the logic returning the parameters to a method which
returns an array. Now this code can be customized without copying the
original method:

```
alias_method :consul_allowed_params, :allowed_params

def allowed_params
  consul_allowed_params + [:my_custom_attribute]
end
```
2022-04-07 19:35:40 +02:00

63 lines
2.0 KiB
Ruby

class Admin::SettingsController < Admin::BaseController
def index
all_settings = Setting.all.group_by(&:type)
@configuration_settings = all_settings["configuration"]
@feature_settings = all_settings["feature"]
@participation_processes_settings = all_settings["process"]
@map_configuration_settings = all_settings["map"]
@proposals_settings = all_settings["proposals"]
@remote_census_general_settings = all_settings["remote_census.general"]
@remote_census_request_settings = all_settings["remote_census.request"]
@remote_census_response_settings = all_settings["remote_census.response"]
@uploads_settings = all_settings["uploads"]
@sdg_settings = all_settings["sdg"]
end
def update
@setting = Setting.find(params[:id])
@setting.update!(settings_params)
respond_to do |format|
format.html { redirect_to request_referer, notice: t("admin.settings.flash.updated") }
format.js
end
end
def update_map
Setting["map.latitude"] = params[:latitude].to_f
Setting["map.longitude"] = params[:longitude].to_f
Setting["map.zoom"] = params[:zoom].to_i
redirect_to admin_settings_path, notice: t("admin.settings.index.map.flash.update")
end
def update_content_types
setting = Setting.find(params[:id])
group = setting.content_type_group
mime_type_values = content_type_params.keys.map do |content_type|
Setting.mime_types[group][content_type]
end
setting.update! value: mime_type_values.join(" ")
redirect_to admin_settings_path, notice: t("admin.settings.flash.updated")
end
private
def settings_params
params.require(:setting).permit(allowed_params)
end
def allowed_params
[:value]
end
def content_type_params
params.permit(:jpg, :png, :gif, :pdf, :doc, :docx, :xls, :xlsx, :csv, :zip)
end
def request_referer
return request.referer + params[:setting][:tab] if params[:setting][:tab]
request.referer
end
end