Skip to content

Commit

Permalink
Disallow deleting pages if still attached to menu node (AlchemyCMS#2338)
Browse files Browse the repository at this point in the history
Add foreign key on nodes to restrict on delete page_id
and add a warning on delete if a page is still linked to nodes.

Closes AlchemyCMS#2301
  • Loading branch information
afdev82 authored and dbwinger committed Mar 20, 2023
1 parent 57ec979 commit 9129305
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/controllers/alchemy/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ def destroy
# Remove page from clipboard
clipboard = get_clipboard("pages")
clipboard.delete_if { |item| item["id"] == @page.id.to_s }
else
flash[:warning] = @page.errors.full_messages.to_sentence
end

respond_to do |format|
Expand Down
5 changes: 5 additions & 0 deletions app/models/alchemy/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ class Page < BaseRecord
before_create -> { versions.build },
if: -> { versions.none? }

before_destroy if: -> { nodes.any? } do
errors.add(:nodes, :still_present)
throw(:abort)
end

before_save :set_language_code,
if: -> { language.present? }

Expand Down
4 changes: 4 additions & 0 deletions config/locales/alchemy.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,10 @@ en:
attributes:
pages:
still_present: "are still attached to this language. Please remove them first."
alchemy/page:
attributes:
nodes:
still_present: "are still attached to this page. Please remove them first."
models:
gutentag/tag:
one: Tag
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class RestrictOnDeletePageIdForeignKeyFromAlchemyNodes < ActiveRecord::Migration[6.0]
def up
remove_foreign_key :alchemy_nodes, :alchemy_pages
add_foreign_key :alchemy_nodes, :alchemy_pages, column: :page_id, on_delete: :restrict
end

def down
remove_foreign_key :alchemy_nodes, :alchemy_pages
add_foreign_key :alchemy_nodes, :alchemy_pages, column: :page_id, on_delete: :cascade
end
end
23 changes: 23 additions & 0 deletions spec/controllers/alchemy/admin/pages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,29 @@
end
end

describe "#destroy" do
let(:page) { create(:alchemy_page) }

context "with nodes attached" do
let!(:node) { create(:alchemy_node, page: page) }

it "returns with error message" do
delete :destroy, params: { id: page.id, format: :js }
expect(response).to redirect_to admin_page_path(page.id)
expect(flash[:warning]).to \
eq("Nodes are still attached to this page. Please remove them first.")
end
end

context "without nodes" do
it "removes the page" do
delete :destroy, params: { id: page.id, format: :js }
expect(response).to redirect_to admin_page_path(page.id)
expect(flash[:notice]).to eq("A Page 61 deleted")
end
end
end

describe "#publish" do
let(:page) { create(:alchemy_page) }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class RestrictOnDeletePageIdForeignKeyFromAlchemyNodes < ActiveRecord::Migration[6.0]
def up
remove_foreign_key :alchemy_nodes, :alchemy_pages
add_foreign_key :alchemy_nodes, :alchemy_pages, column: :page_id, on_delete: :restrict
end

def down
remove_foreign_key :alchemy_nodes, :alchemy_pages
add_foreign_key :alchemy_nodes, :alchemy_pages, column: :page_id, on_delete: :cascade
end
end
4 changes: 2 additions & 2 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2021_11_05_175532) do
ActiveRecord::Schema[7.0].define(version: 2022_05_14_072456) do
create_table "alchemy_attachments", force: :cascade do |t|
t.string "name"
t.string "file_name"
Expand Down Expand Up @@ -402,7 +402,7 @@
add_foreign_key "alchemy_essence_pages", "alchemy_pages", column: "page_id"
add_foreign_key "alchemy_ingredients", "alchemy_elements", column: "element_id", on_delete: :cascade
add_foreign_key "alchemy_nodes", "alchemy_languages", column: "language_id"
add_foreign_key "alchemy_nodes", "alchemy_pages", column: "page_id", on_delete: :cascade
add_foreign_key "alchemy_nodes", "alchemy_pages", column: "page_id", on_delete: :restrict
add_foreign_key "alchemy_page_versions", "alchemy_pages", column: "page_id", on_delete: :cascade
add_foreign_key "alchemy_pages", "alchemy_languages", column: "language_id"
add_foreign_key "alchemy_picture_thumbs", "alchemy_pictures", column: "picture_id"
Expand Down

0 comments on commit 9129305

Please sign in to comment.