Skip to content
Browse files

Updated to handle repeated reload

  • Loading branch information...
0 parents commit 78a6ef987d21ba8bdde84b491e0bbd61bffae078 @thetron committed Feb 28, 2011
3 .gitignore
@@ -0,0 +1,3 @@
+pkg/*
+*.gem
+.bundle
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in menumatic.gemspec
+gemspec
13 README.md
@@ -0,0 +1,13 @@
+# Menumatic
+
+Menumatic is a _Rails 3 exclusive_ gem which aids in developing HTML
+navigation menus.
+
+# Getting Started
+
+Start by generating a new navigation
+
+ $ rails g navigation main
+
+This will create the file `app/navigation/main.rb` which you can use to
+define the structure of your navigation items
2 Rakefile
@@ -0,0 +1,2 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
3 lib/generators/menumatic/USAGE
@@ -0,0 +1,3 @@
+To create a new MenuMatic navigation in your Rails app, simply do:
+
+ rails generate menumatic:navigation main
23 lib/generators/menumatic/navigation_generator.rb
@@ -0,0 +1,23 @@
+module Menumatic
+ module Generators
+ class NavigationGenerator < Rails::Generators::Base
+ desc "Creates a new navigation file"
+ source_root File.expand_path('../templates', __FILE__)
+ argument :navigation_name, :type => :string, :default => "application"
+ class_option :stylesheet, :type => :boolean, :default => true, :description => "Include stylesheet file"
+
+ def generate_navigation
+ template "navigation.rb", "app/navigation/#{file_name}_navigation.rb"
+ end
+
+ def generate_stylesheet
+ copy_file "stylesheet.css", "public/stylesheets/#{file_name}_navigation.css" unless options.skip_stylesheet?
+ end
+
+ private
+ def file_name
+ navigation_name.underscore
+ end
+ end
+ end
+end
3 lib/generators/menumatic/templates/navigation.rb
@@ -0,0 +1,3 @@
+class <%= file_name.camelize %>Navigation < Menumatic::Navigation::Base
+ navigate_to "Home", root_path
+end
26 lib/generators/menumatic/templates/stylesheet.css
@@ -0,0 +1,26 @@
+ul.navigation{
+ padding: 0;
+ margin: 0;
+ display:block;
+ clear:both:
+ float:left;
+}
+
+ul.navigation li{
+ display: inline;
+ background: #CCC;
+ float: left;
+}
+
+ul.navigation li a{
+ display: block;
+ padding: 15px 5px;
+}
+
+ul.navigation.level_1 li.active{
+ background: #777
+}
+
+ul.navigation.level_2 li{
+ background: #777;
+}
7 lib/menumatic.rb
@@ -0,0 +1,7 @@
+require 'menumatic/helpers/navigation_helper'
+require 'menumatic/navigation'
+require 'menumatic/navigation/item'
+
+module Menumatic
+ ActionController::Base.helper(Menumatic::Helpers::NavigationHelper)
+end
47 lib/menumatic/helpers/navigation_helper.rb
@@ -0,0 +1,47 @@
+module Menumatic
+ module Helpers
+ module NavigationHelper
+ @levels = [:primary, :secondary, :tertiary, :quarternary, :quinary]
+
+ def navigation(ids, options = {})
+ options[:start_level] ||= :primary # level to start building from
+ options[:end_level] ||= nil # last level to render
+ options[:level] ||= nil # single level to render, overrides any other level settings
+
+ ids = Array(ids)
+ navigation_id = ids.first
+
+ # this needs to be made development-only
+ Menumatic::Navigation::Base.destroy_all
+ load "app/navigation/#{navigation_id}_navigation.rb"
+
+ navigation = Menumatic::Navigation::Base.get(navigation_id)
+ render_list(navigation.items, 1, true)
+ end
+
+ private
+ def render_list(items, level, render_children = false)
+ has_active = render_children
+ html_class = "level_#{level.to_s}"
+ html_class += " application navigation" if level == 1
+ if items && items.count > 0
+ list = content_tag(:ul, items.map do |item|
+ has_active = true if item.is_active?
+ content_tag(:li, render_item(item, level))
+ end.join("").html_safe, :class => html_class)
+ end
+ has_active ? list : nil
+ end
+
+ def render_item(item, level)
+ children = render_list(item.items, level + 1)
+ if children
+ item.render(:active => true) + children
+ else
+ item.render
+ end
+ end
+
+ end
+ end
+end
47 lib/menumatic/navigation.rb
@@ -0,0 +1,47 @@
+module Menumatic
+ module Navigation
+ class Base
+ include ActionView::Helpers
+ @@navigations = {}
+ attr_accessor :id, :root
+
+ class << self
+ def navigate_to(label, destination, options = {}, html_options = {})
+ if block_given?
+ item = self.get_instance.root.navigate_to(label, destination, options, html_options, &Proc.new)
+ else
+ item = self.get_instance.root.navigate_to(label, destination, options, html_options)
+ end
+ end
+
+ def get_instance
+ id = self.name.split("Navigation").first.underscore.to_sym
+ @@navigations[id] = self.new(id) unless @@navigations.has_key?(id)
+ @@navigations[id]
+ end
+
+ def get(id)
+ unless @@navigations.has_key?(id)
+ Module.const_get("#{id.to_s.camelize}Navigation").get_instance
+ end
+ @@navigations[id]
+ end
+
+ def destroy_all
+ @@navigations = {}
+ end
+ end
+
+ def initialize(id)
+ self.id = id
+ self.root = Menumatic::Navigation::Item.new("Root", "javascript:void(0)", {:root => true})
+ end
+
+ def items
+ self.root.items
+ end
+
+ end
+ end
+end
+
45 lib/menumatic/navigation/item.rb
@@ -0,0 +1,45 @@
+module Menumatic
+ module Navigation
+ class Item
+ attr_accessor :is_root, :items, :label, :destination, :options, :html_options
+
+ include ActionView::Helpers::UrlHelper
+ def initialize(label, destination, options = {}, html_options = {})
+ self.label = label
+ self.destination = destination
+ self.options = options
+ self.html_options = html_options
+ self.is_root = options[:root]
+ self.items = []
+ end
+
+ def render(options = {})
+ options[:active] ||= false
+
+ html_options = self.html_options
+ html_options[:class] = "active" if options[:active] || self.is_active?
+
+ if self.is_root
+ self.label
+ else
+ link_to(self.label, self.destination, html_options).html_safe
+ end
+ end
+
+ def navigate_to(label, destination, options = {}, html_options = {})
+ item = add_item(Menumatic::Navigation::Item.new(label, destination, options, html_options))
+ yield item if block_given?
+ end
+
+ def is_active?
+ false
+ end
+
+ private
+ def add_item(item)
+ self.items << item
+ item
+ end
+ end
+ end
+end
3 lib/menumatic/version.rb
@@ -0,0 +1,3 @@
+module Menumatic
+ VERSION = "0.0.1"
+end
21 menumatic.gemspec
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "menumatic/version"
+
+Gem::Specification.new do |s|
+ s.name = "menumatic"
+ s.version = Menumatic::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["TODO: Write your name"]
+ s.email = ["TODO: Write your email address"]
+ s.homepage = ""
+ s.summary = %q{TODO: Write a gem summary}
+ s.description = %q{TODO: Write a gem description}
+
+ s.rubyforge_project = "menumatic"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end

0 comments on commit 78a6ef9

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