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
```
72 lines
1.3 KiB
Ruby
72 lines
1.3 KiB
Ruby
module Admin::Widget::CardsActions
|
|
extend ActiveSupport::Concern
|
|
include Translatable
|
|
include ImageAttributes
|
|
|
|
included do
|
|
helper_method :form_path
|
|
end
|
|
|
|
def new
|
|
@card.header = header_card?
|
|
render template: "#{cards_view_path}/new"
|
|
end
|
|
|
|
def create
|
|
if @card.save
|
|
redirect_to_index
|
|
else
|
|
render template: "#{cards_view_path}/new"
|
|
end
|
|
end
|
|
|
|
def edit
|
|
render template: "#{cards_view_path}/edit"
|
|
end
|
|
|
|
def update
|
|
if @card.update(card_params)
|
|
redirect_to_index
|
|
else
|
|
render template: "#{cards_view_path}/edit"
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
@card.destroy!
|
|
redirect_to_index
|
|
end
|
|
|
|
private
|
|
|
|
def card_params
|
|
params.require(:widget_card).permit(allowed_params)
|
|
end
|
|
|
|
def allowed_params
|
|
[
|
|
:link_url, :button_text, :button_url, :alignment, :header, :columns,
|
|
translation_params(Widget::Card),
|
|
image_attributes: image_attributes
|
|
]
|
|
end
|
|
|
|
def header_card?
|
|
params[:header_card].present?
|
|
end
|
|
|
|
def redirect_to_index
|
|
notice = t("admin.site_customization.pages.cards.#{params[:action]}.notice")
|
|
|
|
redirect_to index_path, notice: notice
|
|
end
|
|
|
|
def cards_view_path
|
|
"admin/widget/cards"
|
|
end
|
|
|
|
def form_path
|
|
nil
|
|
end
|
|
end
|