Files
nairobi/app/controllers/admin/widget/cards_controller.rb
Javi Martín 2926e4e375 Fix managing widget cards for homepage
The condition `params[:page_id] != 0` didn't work properly when editing
the homepage because in that case the parameter was `nil`, and the line
`SiteCustomization::Page.find(@card.site_customization_page_id)` raised
an exception because it couldn't find a page with a `nil` ID.

Fixing the issue while maintaining the check against `0` lead to complex
code, and so allowing `nil` in the database and assuming cards with no
`site_customization_page_id` belonged in the homepage seemed to be the
easiest solution.
2019-01-21 10:40:14 -05:00

75 lines
1.7 KiB
Ruby

class Admin::Widget::CardsController < Admin::BaseController
include Translatable
def new
if header_card?
@card = ::Widget::Card.new(header: header_card?)
else
@card = ::Widget::Card.new(site_customization_page_id: params[:page_id])
end
end
def create
@card = ::Widget::Card.new(card_params)
if @card.save
redirect_to_customization_page_cards_or_homepage
else
render :new
end
end
def edit
@card = ::Widget::Card.find(params[:id])
end
def update
@card = ::Widget::Card.find(params[:id])
if @card.update(card_params)
redirect_to_customization_page_cards_or_homepage
else
render :edit
end
end
def destroy
@card = ::Widget::Card.find(params[:id])
@card.destroy
redirect_to_customization_page_cards_or_homepage
end
private
def card_params
image_attributes = [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy]
params.require(:widget_card).permit(
:link_url, :button_text, :button_url, :alignment, :header, :site_customization_page_id,
translation_params(Widget::Card),
image_attributes: image_attributes
)
end
def header_card?
params[:header_card].present?
end
def redirect_to_customization_page_cards_or_homepage
notice = t("admin.site_customization.pages.cards.#{params[:action]}.notice")
if @card.site_customization_page_id
redirect_to admin_site_customization_page_cards_path(page), notice: notice
else
redirect_to admin_homepage_url, notice: notice
end
end
def page
::SiteCustomization::Page.find(@card.site_customization_page_id)
end
def resource
Widget::Card.find(params[:id])
end
end