Skip to content

Commit

Permalink
Render nodes (AlchemyCMS#1831)
Browse files Browse the repository at this point in the history
* Allow directly rendering nodes

We can use Rails' model rendering mechanism to render nodes by changing
`to_partial_path` to not point to the wrapper, but to the node itself.
This yields a few simplifications.

* Adapt generator partials to not use node_partial_path

This is now `node.to_partial_path`.

* Actually render node when rendering an EssenceNode
  • Loading branch information
mamhoff authored May 14, 2020
1 parent f58f4f9 commit f25b8e5
Show file tree
Hide file tree
Showing 15 changed files with 21 additions and 34 deletions.
6 changes: 1 addition & 5 deletions app/helpers/alchemy/pages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,7 @@ def render_menu(name, options = {})
return
end

options = {
node_partial_name: "#{root_node.view_folder_name}/node",
}.merge(options)

render(root_node.to_partial_path, menu: root_node, node: root_node, options: options)
render("alchemy/menus/#{name}/wrapper", menu: root_node, options: options)
rescue ActionView::MissingTemplate => e
warning <<~WARN
Menu partial not found for #{name}.
Expand Down
6 changes: 3 additions & 3 deletions app/models/alchemy/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ def url
end

def to_partial_path
"#{view_folder_name}/wrapper"
"alchemy/menus/#{menu_type}/node"
end

def view_folder_name
"alchemy/menus/#{name.parameterize.underscore}"
def menu_type
root.name.parameterize.underscore
end

def check_if_related_essence_nodes_present
Expand Down
3 changes: 1 addition & 2 deletions app/views/alchemy/essences/_essence_node_view.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<%# The content object holds the essence %>
<%= content.ingredient %>
<%= render content.ingredient if content.ingredient %>
2 changes: 1 addition & 1 deletion lib/generators/alchemy/menus/templates/wrapper.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%% cache menu do %>
<ul class="nav">
<%%= render partial: options[:node_partial_name],
<%%= render partial: menu.to_partial_path,
collection: menu.children.includes(:page, :children),
locals: { options: options },
as: 'node' %>
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/alchemy/menus/templates/wrapper.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
- cache menu do
%ul.nav
= render partial: options[:node_partial_name],
= render partial: menu.to_partial_path,
collection: menu.children.includes(:page, :children),
locals: { options: options },
as: 'node'
2 changes: 1 addition & 1 deletion lib/generators/alchemy/menus/templates/wrapper.html.slim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
- cache menu do
ul.nav
= render partial: options[:node_partial_name],
= render partial: menu.to_partial_path,
collection: menu.children.includes(:page, :children),
locals: { options: options },
as: 'node'
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<ul class="nav">
<%= render partial: options[:node_partial_name],
collection: node.children.includes(:page, :children),
<%= render partial: menu.to_partial_path,
collection: menu.children.includes(:page, :children),
locals: { options: options },
as: 'node' %>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<ul class="nav">
<%= render partial: menu.to_partial_path,
collection: menu.children.includes(:page, :children),
locals: { options: options },
as: "node" %>
</ul>

This file was deleted.

2 changes: 1 addition & 1 deletion spec/dummy/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</head>
<body>
<nav>
<%= render_menu('Main Navigation') %>
<%= render_menu('main_menu') %>
</nav>
<%= yield %>
<%= render "alchemy/edit_mode" %>
Expand Down
2 changes: 1 addition & 1 deletion spec/features/admin/page_editing_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
end

context "with menu available" do
let!(:menu) { create(:alchemy_node, name: "Main Navigation") }
let!(:menu) { create(:alchemy_node, name: "main_menu") }
let!(:node) { create(:alchemy_node, url: "/page-1", parent: menu) }

it "navigation links are not clickable" do
Expand Down
2 changes: 1 addition & 1 deletion spec/features/page_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@

describe "navigation rendering" do
context "with menu available" do
let(:menu) { create(:alchemy_node, name: "Main Navigation") }
let(:menu) { create(:alchemy_node, name: "main_menu") }
let(:page1) { create(:alchemy_page, :public, visible: true, name: "Page 1") }
let(:page2) { create(:alchemy_page, :public, visible: true, name: "Page 2") }
let!(:node1) { create(:alchemy_node, page: page1, parent: menu) }
Expand Down
2 changes: 1 addition & 1 deletion spec/helpers/alchemy/pages_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module Alchemy
describe "#render_menu" do
subject { helper.render_menu(name) }

let(:name) { "Main Navigation" }
let(:name) { "main_menu" }

context "if menu exists" do
let(:menu) { create(:alchemy_node, name: name) }
Expand Down
10 changes: 1 addition & 9 deletions spec/models/alchemy/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,7 @@ module Alchemy
let(:node) { build(:alchemy_node, name: "Main Menu") }

it "returns the path to the menu wrapper partial" do
expect(node.to_partial_path).to eq("alchemy/menus/main_menu/wrapper")
end
end

describe "#view_folder_name" do
let(:node) { build(:alchemy_node, name: "Main Menu") }

it "returns the path to the menu view folder" do
expect(node.view_folder_name).to eq("alchemy/menus/main_menu")
expect(node.to_partial_path).to eq("alchemy/menus/main_menu/node")
end
end
end
Expand Down

0 comments on commit f25b8e5

Please sign in to comment.