Files
nairobi/app/controllers/admin/dashboard/actions_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

81 lines
1.9 KiB
Ruby

class Admin::Dashboard::ActionsController < Admin::Dashboard::BaseController
include DocumentAttributes
helper_method :dashboard_action, :resource
def index
@dashboard_actions = proposed_actions + resources
end
def new
@dashboard_action = ::Dashboard::Action.new(
active: true,
day_offset: 0,
required_supports: 0,
request_to_administrators: false,
action_type: "proposed_action"
)
end
def create
@dashboard_action = ::Dashboard::Action.new(dashboard_action_params)
if @dashboard_action.save
redirect_to admin_dashboard_actions_path, notice: t("admin.dashboard.actions.create.notice")
else
render :new
end
end
def edit
dashboard_action
end
def update
if dashboard_action.update(dashboard_action_params)
redirect_to admin_dashboard_actions_path
else
render :edit
end
end
def destroy
if dashboard_action.destroy
flash[:notice] = t("admin.dashboard.actions.delete.success")
else
flash[:error] = dashboard_action.errors.full_messages.join(",")
end
redirect_to admin_dashboard_actions_path
end
private
def resource
@dashboard_action
end
def dashboard_action_params
params.require(:dashboard_action).permit(allowed_params)
end
def allowed_params
[
:title, :description, :short_description, :request_to_administrators, :day_offset,
:required_supports, :order, :active, :action_type, :published_proposal,
documents_attributes: document_attributes,
links_attributes: [:id, :label, :url, :_destroy]
]
end
def dashboard_action
@dashboard_action ||= ::Dashboard::Action.find(params[:id])
end
def proposed_actions
::Dashboard::Action.proposed_actions.order(order: :asc)
end
def resources
::Dashboard::Action.resources.order(required_supports: :asc, day_offset: :asc)
end
end