diff --git a/app/helpers/alchemy/pages_helper.rb b/app/helpers/alchemy/pages_helper.rb index e0db828dea..7cb2e3e136 100644 --- a/app/helpers/alchemy/pages_helper.rb +++ b/app/helpers/alchemy/pages_helper.rb @@ -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. diff --git a/spec/dummy/app/views/layouts/application.html.erb b/spec/dummy/app/views/layouts/application.html.erb index 3179502411..17d080b35d 100644 --- a/spec/dummy/app/views/layouts/application.html.erb +++ b/spec/dummy/app/views/layouts/application.html.erb @@ -7,10 +7,10 @@ <%= csrf_meta_tags %> + <%= yield %> - <%= render "alchemy/edit_mode" %> diff --git a/spec/features/admin/page_editing_feature_spec.rb b/spec/features/admin/page_editing_feature_spec.rb index cc56d20aa1..d77a4f21cc 100644 --- a/spec/features/admin/page_editing_feature_spec.rb +++ b/spec/features/admin/page_editing_feature_spec.rb @@ -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 diff --git a/spec/features/page_feature_spec.rb b/spec/features/page_feature_spec.rb index 23aa37fa55..11333afa91 100644 --- a/spec/features/page_feature_spec.rb +++ b/spec/features/page_feature_spec.rb @@ -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 @@ -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 diff --git a/spec/helpers/alchemy/pages_helper_spec.rb b/spec/helpers/alchemy/pages_helper_spec.rb index 3f8f5c77eb..9a6d79f619 100644 --- a/spec/helpers/alchemy/pages_helper_spec.rb +++ b/spec/helpers/alchemy/pages_helper_spec.rb @@ -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 }