Use polymorphic routes to manage cards

We use a different logic to load the card depending on the controller
we're using, and then share the rest of the code. This way we simplify
the code a bit, since we don't have to check for the page_id parameter.
This commit is contained in:
Javi Martín
2021-01-06 20:09:36 +01:00
parent 04e817e696
commit 4c0bb894eb
12 changed files with 99 additions and 79 deletions

View File

@@ -0,0 +1,59 @@
module Admin::Widget::CardsActions
extend ActiveSupport::Concern
include Translatable
include ImageAttributes
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(
: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
end