Skip to content

Instantly share code, notes, and snippets.

@olly
Created December 8, 2014 12:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olly/8496d7d3600717b70fdd to your computer and use it in GitHub Desktop.
Save olly/8496d7d3600717b70fdd to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Troop</title>
<%= stylesheet_link_tag 'application' %>
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tags %>
</head>
<body>
<%= navbar 'Troop', root_path do %>
<%= navigation do |nav| %>
<%= nav.item 'Dashboard', dashboard_path, active: DashboardController === controller %>
<% if current_section %>
<%= nav.item current_section.naming.people, section_people_path(current_section), active: PeopleController === controller %>
<% end %>
<%= nav.item 'Help' %>
<% end %>
<%= navigation(:right) do |nav| %>
<%= nav.item 'Your Account', edit_account_path, active: controller.is_a?(AccountController) || controller.is_a?(AccountPasswordController) %>
<%= nav.divider %>
<% if current_section %>
<%= nav.dropdown_item(current_section.name) do |dropdown| %>
<%= dropdown.item "#{current_section.naming.short_section} Settings", section_settings_path(current_section) %>
<% if other_sections.any? %>
<%= dropdown.divider %>
<% other_sections.each do |section| %>
<%= dropdown.item(section.name, section_people_path(section)) %>
<% end %>
<% end %>
<%= dropdown.divider %>
<%= dropdown.item 'Add a New Section', new_section_path %>
<% end %>
<% else %>
<%= nav.item 'Add a New Section', new_section_path %>
<% end %>
<%= nav.divider %>
<%= nav.item('Logout', destroy_user_session_path) %>
<% end %>
<% end %>
<div class="container">
<div class="row">
<div class="span12">
<%= yield %>
</div>
</div>
</div>
</body>
</html>
module NavbarHelper
def navbar(brand_title, path, &content)
content_tag(:div, class: 'navbar navbar-static-top navbar-inverse') do
content_tag(:div, class: 'navbar-inner') do
content_tag(:div, class: 'container') do
output = link_to brand_title, path, class: 'brand'
output.concat(capture(&content)) if block_given?
output
end
end
end
end
def navigation(direction = nil, &navigation)
raise ArgumentError, "unknown direction :#{direction}" if direction && direction != :right
classes = ['nav']
classes << 'pull-right' if direction == :right
content = capture(Navigation.new(self), &navigation)
content_tag(:ul, content, class: classes)
end
private
class Navigation < ActionView::Base
def initialize(helper = nil)
@helper = helper || self
end
attr_reader :helper
def item(name, path = '', options = {})
options.assert_valid_keys(:active)
link = link_to(name, path)
classes = []
classes << 'active' if options[:active]
content_tag(:li, link, class: classes)
end
def dropdown_item(name, &dropdown)
content_tag(:li, class: 'dropdown') do
name = ERB::Util.html_escape(name)
link_text = name.concat(%Q{ <b class="caret"></b>}.html_safe)
link = link_to(name, '#', 'class' => 'dropdown-toggle', 'data-toggle' => 'dropdown')
content = helper.capture(DropdownItem.new, &dropdown)
dropdown = content_tag(:ul, content, class: 'dropdown-menu')
link + dropdown
end
end
def divider
content_tag(:li, '', class: 'divider-vertical')
end
end
class DropdownItem < ActionView::Base
def item(name, path = '')
link = link_to(name, path)
content_tag(:li, link)
end
def divider
content_tag(:li, '', class: 'divider')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment