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

Add errors when node cant be deleted #1828

Merged
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
10 changes: 10 additions & 0 deletions app/models/alchemy/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Alchemy
class Node < BaseRecord
VALID_URL_REGEX = /\A(\/|\D[a-z\+\d\.\-]+:)/

before_destroy :check_if_related_essence_nodes_present

acts_as_nested_set scope: "language_id", touch: true
stampable stamper_class_name: Alchemy.user_class_name

Expand Down Expand Up @@ -75,5 +77,13 @@ def to_partial_path
def view_folder_name
"alchemy/menus/#{name.parameterize.underscore}"
end

def check_if_related_essence_nodes_present
dependent_essence_nodes = self_and_descendants.flat_map(&:essence_nodes)
if dependent_essence_nodes.any?
errors.add(:base, :essence_nodes_present, page_names: dependent_essence_nodes.map(&:page).map(&:name).to_sentence)
throw(:abort)
end
end
end
end
4 changes: 4 additions & 0 deletions config/locales/alchemy.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,10 @@ en:
activerecord:
errors:
models:
alchemy/node:
attributes:
base:
essence_nodes_present: "This menu item is in use inside an Alchemy element on the following pages: %{page_names}."
alchemy/site:
attributes:
languages:
Expand Down
29 changes: 29 additions & 0 deletions spec/models/alchemy/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,33 @@ module Alchemy
end
end
end

describe "#destroy" do
context "if there are essence nodes present" do
let(:node) { create(:alchemy_node) }
let(:page) { create(:alchemy_page, :layoutpage, page_layout: :footer) }
let(:element) { create(:alchemy_element, name: "menu", page: page) }
let(:content) { create(:alchemy_content, name: "menu", element: element) }

before do
node.essence_nodes.create(content: content)
end

it "does not destroy the node but adds an error" do
node.destroy
expect(node).not_to be_destroyed
expect(node.errors.full_messages).to eq(["This menu item is in use inside an Alchemy element on the following pages: #{page.name}."])
end

context "if there are essence nodes present on a child node" do
let!(:parent_node) { create(:alchemy_node, children: [node]) }

it "does not destroy the node and children either but adds an error" do
parent_node.destroy
expect(parent_node).not_to be_destroyed
expect(parent_node.errors.full_messages).to eq(["This menu item is in use inside an Alchemy element on the following pages: #{page.name}."])
end
end
end
end
end