diff --git a/app/controllers/admin/site_customization/cards_controller.rb b/app/controllers/admin/site_customization/cards_controller.rb index d00176d18..86c229ffc 100644 --- a/app/controllers/admin/site_customization/cards_controller.rb +++ b/app/controllers/admin/site_customization/cards_controller.rb @@ -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 diff --git a/app/controllers/admin/widget/cards_controller.rb b/app/controllers/admin/widget/cards_controller.rb index 066ce2bfa..5d676e66a 100644 --- a/app/controllers/admin/widget/cards_controller.rb +++ b/app/controllers/admin/widget/cards_controller.rb @@ -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 diff --git a/app/controllers/concerns/admin/widget/cards_actions.rb b/app/controllers/concerns/admin/widget/cards_actions.rb new file mode 100644 index 000000000..b089ca1a0 --- /dev/null +++ b/app/controllers/concerns/admin/widget/cards_actions.rb @@ -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 diff --git a/app/views/admin/site_customization/cards/_card.html.erb b/app/views/admin/site_customization/cards/_card.html.erb index 578b3c01a..94025a7b1 100644 --- a/app/views/admin/site_customization/cards/_card.html.erb +++ b/app/views/admin/site_customization/cards/_card.html.erb @@ -17,8 +17,6 @@ <% end %> - <%= render Admin::TableActionsComponent.new(card, - edit_path: edit_admin_widget_card_path(card, page_id: params[:page_id]) - ) %> + <%= render Admin::TableActionsComponent.new(card) %> diff --git a/app/views/admin/site_customization/cards/index.html.erb b/app/views/admin/site_customization/cards/index.html.erb index 32ccf8fb3..ff94623f5 100644 --- a/app/views/admin/site_customization/cards/index.html.erb +++ b/app/views/admin/site_customization/cards/index.html.erb @@ -8,7 +8,7 @@
<%= 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" %>
<% if @cards.present? %> diff --git a/app/views/admin/site_customization/pages/index.html.erb b/app/views/admin/site_customization/pages/index.html.erb index 6d0d8f6e9..08a7956f8 100644 --- a/app/views/admin/site_customization/pages/index.html.erb +++ b/app/views/admin/site_customization/pages/index.html.erb @@ -30,7 +30,7 @@ <%= 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" %> diff --git a/app/views/admin/widget/cards/_form.html.erb b/app/views/admin/widget/cards/_form.html.erb index e0afe9e47..9b0ef8c34 100644 --- a/app/views/admin/widget/cards/_form.html.erb +++ b/app/views/admin/widget/cards/_form.html.erb @@ -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 %>
@@ -42,7 +42,6 @@
<%= f.hidden_field :header, value: @card.header? %> - <%= f.hidden_field :site_customization_page_id, value: @card.site_customization_page_id %>
diff --git a/app/views/admin/widget/cards/edit.html.erb b/app/views/admin/widget/cards/edit.html.erb index fd7260ba7..d7ab58339 100644 --- a/app/views/admin/widget/cards/edit.html.erb +++ b/app/views/admin/widget/cards/edit.html.erb @@ -6,4 +6,4 @@ <% end %> -<%= render "form" %> +<%= render "admin/widget/cards/form" %> diff --git a/app/views/admin/widget/cards/new.html.erb b/app/views/admin/widget/cards/new.html.erb index 9e7ee40b2..d12f8ab7c 100644 --- a/app/views/admin/widget/cards/new.html.erb +++ b/app/views/admin/widget/cards/new.html.erb @@ -6,4 +6,4 @@ <% end %> -<%= render "form" %> +<%= render "admin/widget/cards/form" %> diff --git a/config/routes/admin.rb b/config/routes/admin.rb index 632a91a64..4bc1f098f 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -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 diff --git a/spec/routing/polymorphic_routes_spec.rb b/spec/routing/polymorphic_routes_spec.rb index 8ab22512b..729e16d66 100644 --- a/spec/routing/polymorphic_routes_spec.rb +++ b/spec/routing/polymorphic_routes_spec.rb @@ -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) diff --git a/spec/system/admin/widgets/cards_spec.rb b/spec/system/admin/widgets/cards_spec.rb index a0315118a..039f7637d 100644 --- a/spec/system/admin/widgets/cards_spec.rb +++ b/spec/system/admin/widgets/cards_spec.rb @@ -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