Skip to content

Commit

Permalink
Fix re-render of layoutpages form if validation fails
Browse files Browse the repository at this point in the history
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 6ce2f64)
  • Loading branch information
tvdeyen committed Jul 1, 2024
1 parent 2f9a721 commit b39b054
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 3 deletions.
19 changes: 19 additions & 0 deletions app/controllers/alchemy/admin/layoutpages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/views/alchemy/admin/layoutpages/edit.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
<div class="input string">
<%= f.label :tag_list %>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
end
end

resources :layoutpages, only: [:index, :edit]
resources :layoutpages, only: [:index, :edit, :update]

resources :pictures, except: [:new] do
collection do
Expand Down
2 changes: 1 addition & 1 deletion lib/alchemy/permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions spec/controllers/alchemy/admin/layoutpages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions spec/libraries/permissions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down

0 comments on commit b39b054

Please sign in to comment.