From 231a2989e70a68e87e0182ed3ae2be837ab79c54 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Mon, 1 Jul 2024 15:50:01 +0200 Subject: [PATCH] Fix re-render of layoutpages form if validation fails We need to re-render the layoutpages form instead of the page form. Therefore we need to introduce an update action in the layoutpages controller, otherwise it uses the pages controller update action which renders the full page form. (cherry picked from commit 6ce2f64662d06c75126e1886b14fde93e49b8b93) --- .../alchemy/admin/layoutpages_controller.rb | 19 +++++++++++++++ .../alchemy/admin/layoutpages/edit.html.erb | 2 +- config/routes.rb | 2 +- lib/alchemy/permissions.rb | 2 +- .../admin/layoutpages_controller_spec.rb | 24 +++++++++++++++++++ spec/libraries/permissions_spec.rb | 4 ++++ 6 files changed, 50 insertions(+), 3 deletions(-) diff --git a/app/controllers/alchemy/admin/layoutpages_controller.rb b/app/controllers/alchemy/admin/layoutpages_controller.rb index c53a5a94e4..2bf2995593 100644 --- a/app/controllers/alchemy/admin/layoutpages_controller.rb +++ b/app/controllers/alchemy/admin/layoutpages_controller.rb @@ -17,6 +17,25 @@ def index def edit @page = Page.find(params[:id]) end + + def update + @page = Page.find(params[:id]) + if @page.update(page_params) + @notice = Alchemy.t("Page saved", name: @page.name) + render "alchemy/admin/pages/update" + else + render :edit, status: :unprocessable_entity + end + end + + private + + def page_params + params.require(:page).permit( + :name, + :tag_list + ) + end end end end diff --git a/app/views/alchemy/admin/layoutpages/edit.html.erb b/app/views/alchemy/admin/layoutpages/edit.html.erb index 8e07640b16..200a6578da 100644 --- a/app/views/alchemy/admin/layoutpages/edit.html.erb +++ b/app/views/alchemy/admin/layoutpages/edit.html.erb @@ -1,4 +1,4 @@ -<%= alchemy_form_for [:admin, @page], class: 'edit_page' do |f| %> +<%= alchemy_form_for [:admin, @page], url: alchemy.admin_layoutpage_path(@page), class: 'edit_page' do |f| %> <%= f.input :name, autofocus: true %>
<%= f.label :tag_list %> diff --git a/config/routes.rb b/config/routes.rb index 8a9f220589..16bcf9031c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -50,7 +50,7 @@ end end - resources :layoutpages, only: [:index, :edit] + resources :layoutpages, only: [:index, :edit, :update] resources :pictures, except: [:new] do collection do diff --git a/lib/alchemy/permissions.rb b/lib/alchemy/permissions.rb index 9eec7cd772..585ce0b458 100644 --- a/lib/alchemy/permissions.rb +++ b/lib/alchemy/permissions.rb @@ -96,7 +96,7 @@ def alchemy_author_rules can :leave, :alchemy_admin can [:info, :help], :alchemy_admin_dashboard can :manage, :alchemy_admin_clipboard - can :edit, :alchemy_admin_layoutpages + can :update, :alchemy_admin_layoutpages can :tree, :alchemy_admin_pages # Resources diff --git a/spec/controllers/alchemy/admin/layoutpages_controller_spec.rb b/spec/controllers/alchemy/admin/layoutpages_controller_spec.rb index a39b5322b9..f344edb188 100644 --- a/spec/controllers/alchemy/admin/layoutpages_controller_spec.rb +++ b/spec/controllers/alchemy/admin/layoutpages_controller_spec.rb @@ -62,5 +62,29 @@ module Alchemy end end end + + describe "#update" do + let(:page) { create(:alchemy_page, :layoutpage) } + + context "with passing validations" do + subject { put :update, params: {id: page.id, page: {name: "New Name"}}, format: :js } + + it "renders update template" do + is_expected.to render_template("alchemy/admin/pages/update") + end + end + + context "with failing validations" do + subject { put :update, params: {id: page.id, page: {name: ""}} } + + it "renders edit form" do + is_expected.to render_template(:edit) + end + + it "sets 422 status" do + expect(subject.status).to eq 422 + end + end + end end end diff --git a/spec/libraries/permissions_spec.rb b/spec/libraries/permissions_spec.rb index 693fac00c2..fb1e85d352 100644 --- a/spec/libraries/permissions_spec.rb +++ b/spec/libraries/permissions_spec.rb @@ -148,6 +148,10 @@ is_expected.to be_able_to(:switch, Alchemy::Language) end + it "can update layoutpages" do + is_expected.to be_able_to(:update, :alchemy_admin_layoutpages) + end + context "if page language is public" do let(:language) { create(:alchemy_language, :german, public: true) } let(:page) { create(:alchemy_page, language: language) }