Skip to content

Commit

Permalink
Add a menus generator
Browse files Browse the repository at this point in the history
Generates view partials for menus

    rails g alchemy:menus
  • Loading branch information
tvdeyen committed Nov 8, 2019
1 parent 1cf3fc2 commit 8419df3
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/rails/generators/alchemy/menus/menus_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require_relative '../base'

module Alchemy
module Generators
class MenusGenerator < Base
desc "This generator generates Alchemy menu partials."
source_root File.expand_path('templates', __dir__)

def create_partials
menus = Alchemy::Node.roots
return unless menus

menus.each do |menu|
conditional_template "wrapper.html.#{template_engine}",
"app/views/#{menu.view_folder_name}/_wrapper.html.#{template_engine}"
conditional_template "node.html.#{template_engine}",
"app/views/#{menu.view_folder_name}/_node.html.#{template_engine}"
end
end
end
end
end
17 changes: 17 additions & 0 deletions lib/rails/generators/alchemy/menus/templates/node.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%%= content_tag :li, class: ['nav-item', node.children.any? ? 'dropdown' : nil].compact do %>
<%%= link_to_if node.url,
node.name,
@preview_mode ? 'javascript: void(0)' : node.url,
class: ['nav-link', current_page?(node.url) ? 'active' : nil].compact,
title: node.title,
target: node.external? ? '_blank' : nil,
rel: node.nofollow? ? 'nofollow' : nil %>
<%% if node.children.any? %>
<ul class="dropdown-menu">
<%%= render partial: options[:node_partial_name],
collection: node.children.includes(:page, :children),
locals: { options: options },
as: 'node' %>
</ul>
<%% end %>
<%% end %>
15 changes: 15 additions & 0 deletions lib/rails/generators/alchemy/menus/templates/node.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
= content_tag :li,
class: ['nav-item', node.children.any? ? 'dropdown' : nil].compact do
= link_to_if node.url,
node.name,
@preview_mode ? 'javascript: void(0)' : node.url,
class: ['nav-link', current_page?(node.url) ? 'active' : nil].compact,
title: node.title,
target: node.external? ? '_blank' : nil,
rel: node.nofollow? ? 'nofollow' : nil
- if node.children.any?
%ul.dropdown-menu
= render partial: options[:node_partial_name],
collection: node.children.includes(:page, :children),
locals: { options: options },
as: 'node'
15 changes: 15 additions & 0 deletions lib/rails/generators/alchemy/menus/templates/node.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
= content_tag :li,
class: ['nav-item', node.children.any? ? 'dropdown' : nil].compact do
= link_to_if node.url,
node.name,
@preview_mode ? 'javascript: void(0)' : node.url,
class: ['nav-link', current_page?(node.url) ? 'active' : nil].compact,
title: node.title,
target: node.external? ? '_blank' : nil,
rel: node.nofollow? ? 'nofollow' : nil
- if node.children.any?
ul.dropdown-menu
= render partial: options[:node_partial_name],
collection: node.children.includes(:page, :children),
locals: { options: options },
as: 'node'
6 changes: 6 additions & 0 deletions lib/rails/generators/alchemy/menus/templates/wrapper.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<ul class="nav">
<%%= render partial: options[:node_partial_name],
collection: node.children.includes(:page, :children),
locals: { options: options },
as: 'node' %>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%ul.nav
= render partial: options[:node_partial_name],
collection: node.children.includes(:page, :children),
locals: { options: options },
as: 'node' %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ul.nav
= render partial: options[:node_partial_name],
collection: node.children.includes(:page, :children),
locals: { options: options },
as: 'node' %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%= content_tag :li, class: ['nav-item', node.children.any? ? 'dropdown' : nil].compact do %>
<%= link_to_if node.url,
node.name,
@preview_mode ? 'javascript: void(0)' : node.url,
class: ['nav-link', current_page?(node.url) ? 'active' : nil].compact,
title: node.title,
target: node.external? ? '_blank' : nil,
rel: node.nofollow? ? 'nofollow' : nil %>
<% if node.children.any? %>
<ul class="dropdown-menu">
<%= render partial: options[:node_partial_name],
collection: node.children.includes(:page, :children),
locals: { options: options },
as: 'node' %>
</ul>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<ul class="nav">
<%= render partial: options[:node_partial_name],
collection: node.children.includes(:page, :children),
locals: { options: options },
as: 'node' %>
</ul>
17 changes: 17 additions & 0 deletions spec/dummy/app/views/alchemy/menus/main_navigation/_node.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%= content_tag :li, class: ['nav-item', node.children.any? ? 'dropdown' : nil].compact do %>
<%= link_to_if node.url,
node.name,
@preview_mode ? 'javascript: void(0)' : node.url,
class: ['nav-link', current_page?(node.url) ? 'active' : nil].compact,
title: node.title,
target: node.external? ? '_blank' : nil,
rel: node.nofollow? ? 'nofollow' : nil %>
<% if node.children.any? %>
<ul class="dropdown-menu">
<%= render partial: options[:node_partial_name],
collection: node.children.includes(:page, :children),
locals: { options: options },
as: 'node' %>
</ul>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<ul class="nav">
<%= render partial: options[:node_partial_name],
collection: node.children.includes(:page, :children),
locals: { options: options },
as: 'node' %>
</ul>

0 comments on commit 8419df3

Please sign in to comment.