Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.1-stable] Fix re-render of layoutpages form if validation fails #2953

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -50,7 +50,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
Loading