Skip to content

Commit

Permalink
Add render_menu helper
Browse files Browse the repository at this point in the history
It renders a menu's view templates.
  • Loading branch information
tvdeyen committed Nov 6, 2019
1 parent 4a2ebd8 commit a2171d4
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 25 deletions.
27 changes: 27 additions & 0 deletions app/helpers/alchemy/pages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,33 @@ def render_navigation(options = {}, html_options = {})
end
deprecate render_navigation: 'Create a menu and use render_menu instead', deprecator: Alchemy::Deprecation

# Renders a menu partial
#
# Menu partials are placed in the `app/views/alchemy/menus` folder
# Use the `rails g alchemy:menus` generator to create the partials
#
# @param [String] - Name of the menu
# @param [Hash] - A set of options available in your menu partials
# @param [Hash] - A set of HTML options available in your menu partials
def render_menu(name, options = {}, html_options = {})
root_node = Alchemy::Node.roots.find_by(name: name)
if root_node.nil?
warning("Menu with name #{name} not found!")
return
end

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

render root_node, node: root_node, options: options, html_options: html_options
rescue ActionView::MissingTemplate => e
warning <<~WARN
Menu partial not found for #{name}.
#{e}
WARN
end

# Renders navigation the children and all siblings of the given page (standard is the current page).
#
# Use this helper if you want to render the subnavigation independent from the mainnavigation. I.E. to place it in a different area on your website.
Expand Down
6 changes: 3 additions & 3 deletions spec/dummy/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
<%= csrf_meta_tags %>
</head>
<body>
<nav>
<%= render_menu('Main Navigation') %>
</nav>
<%= yield %>
<div id="navigation">
<%= render_navigation(:all_sub_menues => true) %>
</div>
<%= render "alchemy/edit_mode" %>
</body>
</html>
13 changes: 9 additions & 4 deletions spec/features/admin/page_editing_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,15 @@
expect(page).not_to have_selector('#alchemy_menubar')
end

it "navigation links are not clickable" do
visit alchemy.admin_page_path(a_page)
within('#navigation') do
expect(page).to have_selector('a[href="javascript: void(0)"]')
context 'with menu available' do
let!(:menu) { create(:alchemy_node, name: 'Main Navigation') }
let!(:node) { create(:alchemy_node, url: '/page-1', parent: menu) }

it "navigation links are not clickable" do
visit alchemy.admin_page_path(a_page)
within('nav') do
expect(page).to have_selector('a[href="javascript: void(0)"]')
end
end
end
end
Expand Down
29 changes: 11 additions & 18 deletions spec/features/page_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@
end
end

it "should show the navigation with all visible pages" do
create(:alchemy_page, :public, visible: true, name: 'Page 1')
create(:alchemy_page, :public, visible: true, name: 'Page 2')
visit '/'
within('div#navigation ul') do
expect(page).to have_selector('li a[href="/page-1"], li a[href="/page-2"]')
end
end

describe "Handling of non-existing pages" do
before do
# We need a admin user or the signup page will show up
Expand Down Expand Up @@ -146,15 +137,17 @@
end

describe 'navigation rendering' do
context 'with page having an external url without protocol' do
let!(:external_page) do
create(:alchemy_page, urlname: 'google.com', page_layout: 'external', visible: true)
end

it "adds an prefix to url" do
visit "/#{public_page.urlname}"
within '#navigation' do
expect(page.body).to match('http://google.com')
context 'with menu available' do
let(:menu) { create(:alchemy_node, name: 'Main Navigation') }
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) }
let!(:node2) { create(:alchemy_node, page: page2, parent: menu) }

it "should show the navigation with all visible pages" do
visit '/'
within('nav ul') do
expect(page).to have_selector('li a[href="/page-1"], li a[href="/page-2"]')
end
end
end
Expand Down
27 changes: 27 additions & 0 deletions spec/helpers/alchemy/pages_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,33 @@ module Alchemy
end
end

describe '#render_menu' do
subject { helper.render_menu(name) }

let(:name) { 'Main Navigation' }

context 'if menu exists' do
let(:menu) { create(:alchemy_node, name: name) }
let!(:node) { create(:alchemy_node, parent: menu, url: '/') }

context 'and the template exists' do
it 'renders the menu' do
is_expected.to have_selector('ul.nav > li.nav-item > a.nav-link')
end
end

context 'but the template does not exist' do
let(:name) { 'Unkown' }

it { is_expected.to be_nil }
end
end

context 'if menu does not exist' do
it { is_expected.to be_nil }
end
end

describe "#render_navigation" do
let(:user) { nil }

Expand Down

0 comments on commit a2171d4

Please sign in to comment.