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:
@@ -1,7 +1,14 @@
|
||||
class Admin::SiteCustomization::CardsController < Admin::SiteCustomization::BaseController
|
||||
include Admin::Widget::CardsActions
|
||||
load_and_authorize_resource :page, class: "::SiteCustomization::Page"
|
||||
load_and_authorize_resource :card, through: :page, class: "Widget::Card"
|
||||
|
||||
def index
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def index_path
|
||||
admin_site_customization_page_widget_cards_path(@page)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,70 +1,10 @@
|
||||
class Admin::Widget::CardsController < Admin::BaseController
|
||||
include Translatable
|
||||
include ImageAttributes
|
||||
|
||||
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
|
||||
include Admin::Widget::CardsActions
|
||||
load_and_authorize_resource :card, class: "Widget::Card"
|
||||
|
||||
private
|
||||
|
||||
def card_params
|
||||
params.require(:widget_card).permit(
|
||||
:link_url, :button_text, :button_url, :alignment, :header, :site_customization_page_id,
|
||||
:columns,
|
||||
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_path, notice: notice
|
||||
end
|
||||
end
|
||||
|
||||
def page
|
||||
::SiteCustomization::Page.find(@card.site_customization_page_id)
|
||||
def index_path
|
||||
admin_homepage_path
|
||||
end
|
||||
end
|
||||
|
||||
59
app/controllers/concerns/admin/widget/cards_actions.rb
Normal file
59
app/controllers/concerns/admin/widget/cards_actions.rb
Normal 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
|
||||
@@ -17,8 +17,6 @@
|
||||
<% end %>
|
||||
</td>
|
||||
<td>
|
||||
<%= render Admin::TableActionsComponent.new(card,
|
||||
edit_path: edit_admin_widget_card_path(card, page_id: params[:page_id])
|
||||
) %>
|
||||
<%= render Admin::TableActionsComponent.new(card) %>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
<div class="float-right">
|
||||
<%= link_to t("admin.site_customization.pages.cards.create_card"),
|
||||
new_admin_widget_card_path(page_id: params[:page_id]), class: "button" %>
|
||||
new_admin_site_customization_page_widget_card_path(@page), class: "button" %>
|
||||
</div>
|
||||
|
||||
<% if @cards.present? %>
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
<td>
|
||||
<%= render Admin::TableActionsComponent.new(page) do |actions| %>
|
||||
<%= actions.link_to t("admin.site_customization.pages.page.see_cards"),
|
||||
admin_site_customization_page_cards_path(page),
|
||||
admin_site_customization_page_widget_cards_path(page),
|
||||
class: "cards-link" %>
|
||||
|
||||
<% if page.status == "published" %>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<%= render "shared/globalize_locales", resource: @card %>
|
||||
|
||||
<%= translatable_form_for [:admin, @card] do |f| %>
|
||||
<%= translatable_form_for [:admin, @page, @card] do |f| %>
|
||||
<%= render "shared/errors", resource: @card %>
|
||||
|
||||
<div class="row">
|
||||
@@ -42,7 +42,6 @@
|
||||
</div>
|
||||
|
||||
<%= f.hidden_field :header, value: @card.header? %>
|
||||
<%= f.hidden_field :site_customization_page_id, value: @card.site_customization_page_id %>
|
||||
<div class="row">
|
||||
<div class="image-form">
|
||||
<div class="image small-12 column">
|
||||
|
||||
@@ -6,4 +6,4 @@
|
||||
<% end %>
|
||||
</h2>
|
||||
|
||||
<%= render "form" %>
|
||||
<%= render "admin/widget/cards/form" %>
|
||||
|
||||
@@ -6,4 +6,4 @@
|
||||
<% end %>
|
||||
</h2>
|
||||
|
||||
<%= render "form" %>
|
||||
<%= render "admin/widget/cards/form" %>
|
||||
|
||||
@@ -227,7 +227,7 @@ namespace :admin do
|
||||
|
||||
namespace :site_customization do
|
||||
resources :pages, except: [:show] do
|
||||
resources :cards, only: [:index]
|
||||
resources :cards, except: [:show], as: :widget_cards
|
||||
end
|
||||
resources :images, only: [:index, :update, :destroy]
|
||||
resources :content_blocks, except: [:show]
|
||||
@@ -270,6 +270,10 @@ resolve "Audit" do |audit|
|
||||
[*resource_hierarchy_for(audit.associated || audit.auditable), audit]
|
||||
end
|
||||
|
||||
resolve "Widget::Card" do |card, options|
|
||||
[*resource_hierarchy_for(card.page), card]
|
||||
end
|
||||
|
||||
resolve "Budget::Group" do |group, options|
|
||||
[group.budget, :group, options.merge(id: group)]
|
||||
end
|
||||
|
||||
@@ -164,6 +164,19 @@ describe "Polymorphic routes" do
|
||||
expect(admin_polymorphic_path(shift)).to eq(admin_booth_shift_path(booth, shift))
|
||||
end
|
||||
|
||||
it "routes widget cards" do
|
||||
card = create(:widget_card)
|
||||
|
||||
expect(admin_polymorphic_path(card)).to eq(admin_widget_card_path(card))
|
||||
end
|
||||
|
||||
it "routes site customization page widget cards" do
|
||||
page = create(:site_customization_page)
|
||||
card = create(:widget_card, page: page)
|
||||
|
||||
expect(admin_polymorphic_path(card)).to eq admin_site_customization_page_widget_card_path(page, card)
|
||||
end
|
||||
|
||||
it "supports routes for actions like edit" do
|
||||
proposal = create(:proposal)
|
||||
milestone = create(:milestone, milestoneable: proposal)
|
||||
|
||||
@@ -163,7 +163,7 @@ describe "Cards", :admin do
|
||||
fill_in "Title", with: "Card for a custom page"
|
||||
click_button "Create card"
|
||||
|
||||
expect(page).to have_current_path admin_site_customization_page_cards_path(custom_page)
|
||||
expect(page).to have_current_path admin_site_customization_page_widget_cards_path(custom_page)
|
||||
expect(page).to have_content "Card for a custom page"
|
||||
end
|
||||
|
||||
@@ -199,7 +199,7 @@ describe "Cards", :admin do
|
||||
scenario "Edit", :js do
|
||||
create(:widget_card, page: custom_page, title: "Original title")
|
||||
|
||||
visit admin_site_customization_page_cards_path(custom_page)
|
||||
visit admin_site_customization_page_widget_cards_path(custom_page)
|
||||
|
||||
expect(page).to have_content("Original title")
|
||||
|
||||
@@ -211,7 +211,7 @@ describe "Cards", :admin do
|
||||
|
||||
click_button "Save card"
|
||||
|
||||
expect(page).to have_current_path admin_site_customization_page_cards_path(custom_page)
|
||||
expect(page).to have_current_path admin_site_customization_page_widget_cards_path(custom_page)
|
||||
expect(page).to have_content "Updated title"
|
||||
expect(page).not_to have_content "Original title"
|
||||
end
|
||||
@@ -219,7 +219,7 @@ describe "Cards", :admin do
|
||||
scenario "Destroy", :js do
|
||||
create(:widget_card, page: custom_page, title: "Card title")
|
||||
|
||||
visit admin_site_customization_page_cards_path(custom_page)
|
||||
visit admin_site_customization_page_widget_cards_path(custom_page)
|
||||
|
||||
expect(page).to have_content("Card title")
|
||||
|
||||
@@ -227,7 +227,7 @@ describe "Cards", :admin do
|
||||
click_link "Delete"
|
||||
end
|
||||
|
||||
expect(page).to have_current_path admin_site_customization_page_cards_path(custom_page)
|
||||
expect(page).to have_current_path admin_site_customization_page_widget_cards_path(custom_page)
|
||||
expect(page).not_to have_content "Card title"
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user