Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Updated to handle repeated reload

  • Loading branch information...
commit 78a6ef987d21ba8bdde84b491e0bbd61bffae078 0 parents
Nicholas Bruning authored
3  .gitignore
... ... @@ -0,0 +1,3 @@
  1 +pkg/*
  2 +*.gem
  3 +.bundle
4 Gemfile
... ... @@ -0,0 +1,4 @@
  1 +source "http://rubygems.org"
  2 +
  3 +# Specify your gem's dependencies in menumatic.gemspec
  4 +gemspec
13 README.md
Source Rendered
... ... @@ -0,0 +1,13 @@
  1 +# Menumatic
  2 +
  3 +Menumatic is a _Rails 3 exclusive_ gem which aids in developing HTML
  4 +navigation menus.
  5 +
  6 +# Getting Started
  7 +
  8 +Start by generating a new navigation
  9 +
  10 + $ rails g navigation main
  11 +
  12 +This will create the file `app/navigation/main.rb` which you can use to
  13 +define the structure of your navigation items
2  Rakefile
... ... @@ -0,0 +1,2 @@
  1 +require 'bundler'
  2 +Bundler::GemHelper.install_tasks
3  lib/generators/menumatic/USAGE
... ... @@ -0,0 +1,3 @@
  1 +To create a new MenuMatic navigation in your Rails app, simply do:
  2 +
  3 + rails generate menumatic:navigation main
23 lib/generators/menumatic/navigation_generator.rb
... ... @@ -0,0 +1,23 @@
  1 +module Menumatic
  2 + module Generators
  3 + class NavigationGenerator < Rails::Generators::Base
  4 + desc "Creates a new navigation file"
  5 + source_root File.expand_path('../templates', __FILE__)
  6 + argument :navigation_name, :type => :string, :default => "application"
  7 + class_option :stylesheet, :type => :boolean, :default => true, :description => "Include stylesheet file"
  8 +
  9 + def generate_navigation
  10 + template "navigation.rb", "app/navigation/#{file_name}_navigation.rb"
  11 + end
  12 +
  13 + def generate_stylesheet
  14 + copy_file "stylesheet.css", "public/stylesheets/#{file_name}_navigation.css" unless options.skip_stylesheet?
  15 + end
  16 +
  17 + private
  18 + def file_name
  19 + navigation_name.underscore
  20 + end
  21 + end
  22 + end
  23 +end
3  lib/generators/menumatic/templates/navigation.rb
... ... @@ -0,0 +1,3 @@
  1 +class <%= file_name.camelize %>Navigation < Menumatic::Navigation::Base
  2 + navigate_to "Home", root_path
  3 +end
26 lib/generators/menumatic/templates/stylesheet.css
... ... @@ -0,0 +1,26 @@
  1 +ul.navigation{
  2 + padding: 0;
  3 + margin: 0;
  4 + display:block;
  5 + clear:both:
  6 + float:left;
  7 +}
  8 +
  9 +ul.navigation li{
  10 + display: inline;
  11 + background: #CCC;
  12 + float: left;
  13 +}
  14 +
  15 +ul.navigation li a{
  16 + display: block;
  17 + padding: 15px 5px;
  18 +}
  19 +
  20 +ul.navigation.level_1 li.active{
  21 + background: #777
  22 +}
  23 +
  24 +ul.navigation.level_2 li{
  25 + background: #777;
  26 +}
7 lib/menumatic.rb
... ... @@ -0,0 +1,7 @@
  1 +require 'menumatic/helpers/navigation_helper'
  2 +require 'menumatic/navigation'
  3 +require 'menumatic/navigation/item'
  4 +
  5 +module Menumatic
  6 + ActionController::Base.helper(Menumatic::Helpers::NavigationHelper)
  7 +end
47 lib/menumatic/helpers/navigation_helper.rb
... ... @@ -0,0 +1,47 @@
  1 +module Menumatic
  2 + module Helpers
  3 + module NavigationHelper
  4 + @levels = [:primary, :secondary, :tertiary, :quarternary, :quinary]
  5 +
  6 + def navigation(ids, options = {})
  7 + options[:start_level] ||= :primary # level to start building from
  8 + options[:end_level] ||= nil # last level to render
  9 + options[:level] ||= nil # single level to render, overrides any other level settings
  10 +
  11 + ids = Array(ids)
  12 + navigation_id = ids.first
  13 +
  14 + # this needs to be made development-only
  15 + Menumatic::Navigation::Base.destroy_all
  16 + load "app/navigation/#{navigation_id}_navigation.rb"
  17 +
  18 + navigation = Menumatic::Navigation::Base.get(navigation_id)
  19 + render_list(navigation.items, 1, true)
  20 + end
  21 +
  22 + private
  23 + def render_list(items, level, render_children = false)
  24 + has_active = render_children
  25 + html_class = "level_#{level.to_s}"
  26 + html_class += " application navigation" if level == 1
  27 + if items && items.count > 0
  28 + list = content_tag(:ul, items.map do |item|
  29 + has_active = true if item.is_active?
  30 + content_tag(:li, render_item(item, level))
  31 + end.join("").html_safe, :class => html_class)
  32 + end
  33 + has_active ? list : nil
  34 + end
  35 +
  36 + def render_item(item, level)
  37 + children = render_list(item.items, level + 1)
  38 + if children
  39 + item.render(:active => true) + children
  40 + else
  41 + item.render
  42 + end
  43 + end
  44 +
  45 + end
  46 + end
  47 +end
47 lib/menumatic/navigation.rb
... ... @@ -0,0 +1,47 @@
  1 +module Menumatic
  2 + module Navigation
  3 + class Base
  4 + include ActionView::Helpers
  5 + @@navigations = {}
  6 + attr_accessor :id, :root
  7 +
  8 + class << self
  9 + def navigate_to(label, destination, options = {}, html_options = {})
  10 + if block_given?
  11 + item = self.get_instance.root.navigate_to(label, destination, options, html_options, &Proc.new)
  12 + else
  13 + item = self.get_instance.root.navigate_to(label, destination, options, html_options)
  14 + end
  15 + end
  16 +
  17 + def get_instance
  18 + id = self.name.split("Navigation").first.underscore.to_sym
  19 + @@navigations[id] = self.new(id) unless @@navigations.has_key?(id)
  20 + @@navigations[id]
  21 + end
  22 +
  23 + def get(id)
  24 + unless @@navigations.has_key?(id)
  25 + Module.const_get("#{id.to_s.camelize}Navigation").get_instance
  26 + end
  27 + @@navigations[id]
  28 + end
  29 +
  30 + def destroy_all
  31 + @@navigations = {}
  32 + end
  33 + end
  34 +
  35 + def initialize(id)
  36 + self.id = id
  37 + self.root = Menumatic::Navigation::Item.new("Root", "javascript:void(0)", {:root => true})
  38 + end
  39 +
  40 + def items
  41 + self.root.items
  42 + end
  43 +
  44 + end
  45 + end
  46 +end
  47 +
45 lib/menumatic/navigation/item.rb
... ... @@ -0,0 +1,45 @@
  1 +module Menumatic
  2 + module Navigation
  3 + class Item
  4 + attr_accessor :is_root, :items, :label, :destination, :options, :html_options
  5 +
  6 + include ActionView::Helpers::UrlHelper
  7 + def initialize(label, destination, options = {}, html_options = {})
  8 + self.label = label
  9 + self.destination = destination
  10 + self.options = options
  11 + self.html_options = html_options
  12 + self.is_root = options[:root]
  13 + self.items = []
  14 + end
  15 +
  16 + def render(options = {})
  17 + options[:active] ||= false
  18 +
  19 + html_options = self.html_options
  20 + html_options[:class] = "active" if options[:active] || self.is_active?
  21 +
  22 + if self.is_root
  23 + self.label
  24 + else
  25 + link_to(self.label, self.destination, html_options).html_safe
  26 + end
  27 + end
  28 +
  29 + def navigate_to(label, destination, options = {}, html_options = {})
  30 + item = add_item(Menumatic::Navigation::Item.new(label, destination, options, html_options))
  31 + yield item if block_given?
  32 + end
  33 +
  34 + def is_active?
  35 + false
  36 + end
  37 +
  38 + private
  39 + def add_item(item)
  40 + self.items << item
  41 + item
  42 + end
  43 + end
  44 + end
  45 +end
3  lib/menumatic/version.rb
... ... @@ -0,0 +1,3 @@
  1 +module Menumatic
  2 + VERSION = "0.0.1"
  3 +end
21 menumatic.gemspec
... ... @@ -0,0 +1,21 @@

0 comments on commit 78a6ef9

Please sign in to comment.
Something went wrong with that request. Please try again.