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

Disallow deleting pages if still attached to menu node #2338

Merged
merged 3 commits into from
Jul 25, 2022
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
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 @@ -46,6 +46,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