Skip to content

Commit

Permalink
Merge pull request #3064 from AlchemyCMS/backport/7.3-stable/pr-3060
Browse files Browse the repository at this point in the history
[7.3-stable] fix new page form
  • Loading branch information
tvdeyen authored Oct 4, 2024
2 parents 195e0a1 + dc11e58 commit af895f9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/models/alchemy/page/page_naming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module PageNaming
unless: -> { name.blank? }

validates :name,
presence: true
presence: true, uniqueness: {scope: [:parent_id], case_sensitive: false, unless: -> { parent_id.nil? }}
validates :urlname,
uniqueness: {scope: [:language_id, :layoutpage], if: -> { urlname.present? }, case_sensitive: false},
exclusion: {in: RESERVED_URLNAMES},
Expand Down
2 changes: 1 addition & 1 deletion app/views/alchemy/admin/pages/_new_page_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
label: Alchemy.t(:page_type),
include_blank: @page_layouts.length == 1 ? nil : Alchemy.t('Please choose'),
required: true,
selected: @page_layouts.length == 1 ? @page_layouts.first : nil,
selected: @page_layouts.length == 1 ? @page_layouts.first : @page.page_layout,
input_html: {is: 'alchemy-select'} %>
<%= f.input :name %>
<%= f.submit Alchemy.t(:create) %>
Expand Down
13 changes: 13 additions & 0 deletions spec/features/admin/page_creation_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@
expect(page).to_not have_css("#s2id_page_parent_id")
end
end
context "with same name " do
let!(:existing_page) { create(:alchemy_page, parent: homepage, name: "Unique Name") }
it "doesn't create a page with the same name" do
visit admin_pages_path

find(%(a.icon_button[href="/admin/pages/new?parent_id=#{homepage.id}"]), visible: true, match: :first).click
select2 "Standard", from: "Type"
fill_in "Name", with: "Unique Name"
click_button "create"

expect(page).to have_css("div.alchemy-dialog-body form small.error", text: "has already been taken")
end
end
end

describe "overlay GUI" do
Expand Down
14 changes: 14 additions & 0 deletions spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ module Alchemy

it { expect(page).to_not be_valid }
end
context "a page must have a unique name within the same parent" do
let!(:homepage) { create(:alchemy_page, :language_root) }
let!(:existing_page) { create(:alchemy_page, parent: homepage, name: "Unique Name") }
it {
expect {
create(:alchemy_page, name: existing_page.name, parent: homepage)
}.to raise_error(ActiveRecord::RecordInvalid, /has already been taken/)
}
it {
expect {
create(:alchemy_page, name: existing_page.name.upcase, parent: homepage)
}.to raise_error(ActiveRecord::RecordInvalid, /has already been taken/)
}
end
end

# Callbacks
Expand Down

0 comments on commit af895f9

Please sign in to comment.