krasivotokak-simple-navigation 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/CHANGELOG +38 -0
- data/README +19 -0
- data/Rakefile +74 -0
- data/VERSION.yml +4 -0
- data/generators/navigation_config/USAGE +1 -0
- data/generators/navigation_config/navigation_config_generator.rb +8 -0
- data/generators/navigation_config/templates/config/navigation.rb +48 -0
- data/init.rb +1 -0
- data/install.rb +5 -0
- data/lib/simple_navigation.rb +40 -0
- data/lib/simple_navigation/configuration.rb +60 -0
- data/lib/simple_navigation/controller_methods.rb +75 -0
- data/lib/simple_navigation/helpers.rb +65 -0
- data/lib/simple_navigation/item.rb +48 -0
- data/lib/simple_navigation/item_container.rb +69 -0
- data/lib/simple_navigation/renderer/base.rb +44 -0
- data/lib/simple_navigation/renderer/list.rb +30 -0
- data/rails/init.rb +3 -0
- data/simple-navigation.gemspec +78 -0
- data/spec/lib/simple_navigation/configuration_spec.rb +101 -0
- data/spec/lib/simple_navigation/controller_methods_spec.rb +77 -0
- data/spec/lib/simple_navigation/helpers_spec.rb +125 -0
- data/spec/lib/simple_navigation/item_container_spec.rb +174 -0
- data/spec/lib/simple_navigation/item_spec.rb +148 -0
- data/spec/lib/simple_navigation/renderer/base_spec.rb +56 -0
- data/spec/lib/simple_navigation/renderer/list_spec.rb +118 -0
- data/spec/lib/simple_navigation_spec.rb +140 -0
- data/spec/spec_helper.rb +25 -0
- data/uninstall.rb +1 -0
- metadata +93 -0
data/.gitignore
ADDED
data/CHANGELOG
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
*master
|
2
|
+
|
3
|
+
*1.4.0
|
4
|
+
|
5
|
+
* added the capability to have several navigation-contexts
|
6
|
+
* doc-fix
|
7
|
+
|
8
|
+
*1.3.1
|
9
|
+
|
10
|
+
* now compliant with ruby 1.9.1 (thanks to Gernot Kogler for the feedback)
|
11
|
+
|
12
|
+
*1.3.0
|
13
|
+
|
14
|
+
* render_all_levels-option allows to render all subnavigation independent from the active primary navigation ('full open tree'). Userful for javascript menus. Thanks to Richard Hulse.
|
15
|
+
* ability to turn off automatic generation of dom_ids for the list items (autogenerate_item_ids). Credits again to Richard Hulse.
|
16
|
+
* ability to specify dom_class for primary and secondary lists. Thanks Richard!
|
17
|
+
|
18
|
+
*1.2.2
|
19
|
+
|
20
|
+
* renderers now have access to request_forgery_protection stuff (this allows delete-links as navigation-items)
|
21
|
+
|
22
|
+
*1.2.1
|
23
|
+
|
24
|
+
* changed way to include render_*-helper_methods into view (including them into Controller and declaring them as helper_methods instead of adding whole module as Helper). this seems to be more reliable under certain conditions. Credits to Gernot Kogler.
|
25
|
+
|
26
|
+
*1.2.0
|
27
|
+
|
28
|
+
* added capability to add conditions to navigation-items (primary.item key, name, url, :if => Proc.new {current_user.admin?})
|
29
|
+
|
30
|
+
*1.1.2
|
31
|
+
|
32
|
+
* Bugfix: config now gets evaluated on every render_navigation call. Credits to Joël Azémar.
|
33
|
+
* Config file gets reloaded on every render_navigation call in development mode. Only load config file on server start in production mode.
|
34
|
+
|
35
|
+
|
36
|
+
*1.1.1
|
37
|
+
|
38
|
+
* Change plugin into a GemPlugin
|
data/README
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
== Simple Navigation
|
3
|
+
|
4
|
+
Simple Navigation is a plugin for creating a navigation (optionally with sub navigation) for your rails app.
|
5
|
+
|
6
|
+
Source code:
|
7
|
+
git://github.com/andi/simple-navigation.git
|
8
|
+
|
9
|
+
Documentation:
|
10
|
+
http://wiki.github.com/andi/simple-navigation
|
11
|
+
|
12
|
+
Online Demo:
|
13
|
+
http://simple-navigation-demo.andischacke.com
|
14
|
+
|
15
|
+
Discussion Group for Feedback and Questions
|
16
|
+
http://groups.google.com/group/simple-navigation
|
17
|
+
|
18
|
+
Copyright (c) 2009 Andi Schacke, released under the MIT license
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'spec/rake/spectask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run specs.'
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
desc 'Run the specs'
|
9
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
10
|
+
t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
|
11
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
12
|
+
end
|
13
|
+
|
14
|
+
desc 'Generate documentation for the simple_navigation plugin.'
|
15
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'SimpleNavigation'
|
18
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
19
|
+
rdoc.rdoc_files.include('README')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
begin
|
24
|
+
require 'jeweler'
|
25
|
+
Jeweler::Tasks.new do |gemspec|
|
26
|
+
gemspec.name = "simple-navigation"
|
27
|
+
gemspec.summary = "Simple Navigation is a ruby library for creating a navigation (optionally with sub navigation) for your rails app."
|
28
|
+
gemspec.email = "andreas.schacke@gmail.com"
|
29
|
+
gemspec.homepage = "http://github.com/andi/simple-navigation"
|
30
|
+
gemspec.description = "Simple Navigation is a ruby library for creating a navigation (optionally with sub navigation) for your rails app."
|
31
|
+
gemspec.authors = ["Andi Schacke", 'Alexander Semyonov']
|
32
|
+
gemspec.rdoc_options = ["--inline-source", "--charset=UTF-8"]
|
33
|
+
gemspec.files += ["CHANGELOG"]
|
34
|
+
gemspec.rubyforge_project = 'andi'
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
38
|
+
end
|
39
|
+
|
40
|
+
begin
|
41
|
+
require 'rake/contrib/sshpublisher'
|
42
|
+
namespace :rubyforge do
|
43
|
+
|
44
|
+
desc "Release gem and RDoc documentation to RubyForge"
|
45
|
+
task :release => ["rubyforge:release:gem", "rubyforge:release:docs"]
|
46
|
+
|
47
|
+
namespace :release do
|
48
|
+
desc "Publish RDoc to RubyForge."
|
49
|
+
task :docs => [:rdoc] do
|
50
|
+
config = YAML.load(
|
51
|
+
File.read(File.expand_path('~/.rubyforge/user-config.yml'))
|
52
|
+
)
|
53
|
+
|
54
|
+
host = "#{config['username']}@rubyforge.org"
|
55
|
+
remote_dir = "/var/www/gforge-projects/andi/"
|
56
|
+
local_dir = 'rdoc'
|
57
|
+
|
58
|
+
Rake::SshDirPublisher.new(host, remote_dir, local_dir).upload
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
rescue LoadError
|
63
|
+
puts "Rake SshDirPublisher is unavailable or your rubyforge environment is not configured."
|
64
|
+
end
|
65
|
+
|
66
|
+
begin
|
67
|
+
require 'yard'
|
68
|
+
YARD::Rake::YardocTask.new
|
69
|
+
rescue LoadError
|
70
|
+
task :yardoc do
|
71
|
+
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
data/VERSION.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Creates a template config file for the simple-navigation plugin. You will find the generated file in config/navigation.rb.
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# Configures your navigation
|
2
|
+
SimpleNavigation::Configuration.run do |navigation|
|
3
|
+
# Specify a custom renderer if needed.
|
4
|
+
# The default renderer is SimpleNavigation::Renderer::List which renders HTML lists.
|
5
|
+
# navigation.renderer = Your::Custom::Renderer
|
6
|
+
|
7
|
+
# Specify the class that will be applied to active navigation items. Defaults to 'selected'
|
8
|
+
# navigation.selected_class = 'your_selected_class'
|
9
|
+
|
10
|
+
# Normally only the current sub menu is renderedwhen render_navigation is called
|
11
|
+
# setting this to true render all submenus which is useful for javascript
|
12
|
+
# driven hovering menus like the jquery superfish plugin
|
13
|
+
# navigation.render_all_levels = true
|
14
|
+
|
15
|
+
# Item keys are normally added to list items.
|
16
|
+
# this setting turns that off
|
17
|
+
# navigation.autogenerate_item_ids = false
|
18
|
+
|
19
|
+
# Define the primary navigation
|
20
|
+
navigation.items do |primary|
|
21
|
+
# Add an item to the primary navigation. The following params apply:
|
22
|
+
# key - a symbol which uniquely defines your navigation item in the scope of the primary_navigation
|
23
|
+
# name - will be displayed in the rendered navigation. This can also be a call to your I18n-framework.
|
24
|
+
# url - the address that the generated item links to. You can also use url_helpers (named routes, restful routes helper, url_for etc.)
|
25
|
+
# options - can be used to specify attributes that will be included in the rendered navigation item (e.g. id, class etc.)
|
26
|
+
#
|
27
|
+
primary.item :key_1, 'name', url, options
|
28
|
+
|
29
|
+
# Add an item which has a sub navigation (same params, but with block)
|
30
|
+
primary.item :key_2, 'name', url, options do |sub_nav|
|
31
|
+
# Add an item to the sub navigation (same params again)
|
32
|
+
sub_nav.item :key_2_1, 'name', url, options
|
33
|
+
end
|
34
|
+
|
35
|
+
# You can also specify a condition-proc that needs to be fullfilled to display an item.
|
36
|
+
# Conditions are part of the options. They are evaluated in the context of the views,
|
37
|
+
# thus you can use all the methods and vars you have available in the views.
|
38
|
+
primary.item :key_3, 'Admin', url, :class => 'special', :if => Proc.new { current_user.admin? }
|
39
|
+
primary.item :key_4, 'Account', url, :unless => Proc.new { logged_in? }
|
40
|
+
|
41
|
+
# you can also specify a css id or class to attach to this particular level
|
42
|
+
# works for all levels of the menu
|
43
|
+
# primary.dom_id = 'menu-id'
|
44
|
+
# primary.dom_class = 'menu-class'
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/rails/init"
|
data/install.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# A plugin for generating a simple navigation. See README for resources on usage instructions.
|
2
|
+
module SimpleNavigation
|
3
|
+
|
4
|
+
mattr_accessor :config_files
|
5
|
+
mattr_accessor :config_file_path
|
6
|
+
mattr_accessor :controller
|
7
|
+
self.config_files = {}
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
# Reads the config_file for the specified navigation_context and stores it for later evaluation.
|
12
|
+
def load_config(navigation_context = :default)
|
13
|
+
raise "config_file_path is not set!" unless self.config_file_path
|
14
|
+
raise "Config file '#{config_file_name(navigation_context)}' does not exists!" unless File.exists?(config_file_name(navigation_context))
|
15
|
+
if ::RAILS_ENV == 'production'
|
16
|
+
self.config_files[navigation_context] ||= IO.read(config_file_name(navigation_context))
|
17
|
+
else
|
18
|
+
self.config_files[navigation_context] = IO.read(config_file_name(navigation_context))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns the singleton instance of the SimpleNavigation::Configuration
|
23
|
+
def config
|
24
|
+
Configuration.instance
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns the ItemContainer that contains the items for the primary navigation
|
28
|
+
def primary_navigation
|
29
|
+
config.primary_navigation
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns the path to the config_file for the given navigation_context
|
33
|
+
def config_file_name(navigation_context = :default)
|
34
|
+
file_name = navigation_context == :default ? '' : "#{navigation_context.to_s.underscore}_"
|
35
|
+
File.join(config_file_path, "#{file_name}navigation.rb")
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module SimpleNavigation
|
4
|
+
|
5
|
+
# Responsible for evaluating and handling the config/navigation.rb file.
|
6
|
+
class Configuration
|
7
|
+
include Singleton
|
8
|
+
|
9
|
+
attr_accessor :renderer
|
10
|
+
attr_accessor :selected_class
|
11
|
+
attr_accessor :render_all_levels
|
12
|
+
attr_accessor :autogenerate_item_ids
|
13
|
+
attr_reader :primary_navigation
|
14
|
+
|
15
|
+
class << self
|
16
|
+
|
17
|
+
# Evals the config_file for the given navigation_context inside the specified context (usually a controller or view)
|
18
|
+
def eval_config(context, navigation_context = :default)
|
19
|
+
context.instance_eval(SimpleNavigation.config_files[navigation_context])
|
20
|
+
SimpleNavigation.controller = extract_controller_from context
|
21
|
+
end
|
22
|
+
|
23
|
+
# Starts processing the configuration
|
24
|
+
def run(&block)
|
25
|
+
block.call Configuration.instance
|
26
|
+
end
|
27
|
+
|
28
|
+
# Extracts a controller from the context.
|
29
|
+
def extract_controller_from(context)
|
30
|
+
if context.respond_to? :controller
|
31
|
+
context.controller
|
32
|
+
else
|
33
|
+
context
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
# Sets the config's default-settings
|
40
|
+
def initialize
|
41
|
+
@renderer = SimpleNavigation::Renderer::List
|
42
|
+
@selected_class = 'selected'
|
43
|
+
@render_all_levels = false
|
44
|
+
@autogenerate_item_ids = true
|
45
|
+
end
|
46
|
+
|
47
|
+
# Yields an SimpleNavigation::ItemContainer for adding navigation items
|
48
|
+
def items(&block)
|
49
|
+
@primary_navigation = ItemContainer.new
|
50
|
+
block.call @primary_navigation
|
51
|
+
end
|
52
|
+
|
53
|
+
# Returns true if the config_file has already been evaluated.
|
54
|
+
def loaded?
|
55
|
+
!@primary_navigation.nil?
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
#TODO: add :except and :only options to navigation method
|
2
|
+
module SimpleNavigation
|
3
|
+
|
4
|
+
# Adds methods for handling the current 'active' navigation to the controllers.
|
5
|
+
#
|
6
|
+
# On the controller class level, use the navigation method to set the active navigation for all actions in the controller.
|
7
|
+
#
|
8
|
+
# ==== Examples
|
9
|
+
# class AccountController << ActionController
|
10
|
+
# navigation :account
|
11
|
+
# ...
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# class AccountSettingsController << ActionController
|
15
|
+
# navigation :account, :settings
|
16
|
+
# ...
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# The first example sets the current_primary_navigation to :account for all actions. No active sub_navigation.
|
20
|
+
# The second example sets the current_primary_navigation to :account and the current_sub_navigation to :settings.
|
21
|
+
#
|
22
|
+
# On the controller instance level, use the current_navigation method to define the active navigation for a specific action.
|
23
|
+
# The navigation item that is set in current_navigation overrides the one defined on the controller class level (see navigation method).
|
24
|
+
#
|
25
|
+
# ==== Example
|
26
|
+
# class AccountController << ActionController
|
27
|
+
# navigation :account
|
28
|
+
#
|
29
|
+
# def your_special_action
|
30
|
+
# ...
|
31
|
+
# current_navigation :account, :special
|
32
|
+
# end
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# The code above still sets the active primary navigation to :account but also sets the sub_navigation to :special for 'your_special_action'.
|
36
|
+
#
|
37
|
+
# Note: The specified symbols must match the keys for your navigation items in your config/navigation.rb file.
|
38
|
+
module ControllerMethods
|
39
|
+
def self.included(base) #:nodoc:
|
40
|
+
base.class_eval do
|
41
|
+
extend ClassMethods
|
42
|
+
include InstanceMethods
|
43
|
+
include SimpleNavigation::Helpers
|
44
|
+
base.helper_method :render_navigation, :render_primary_navigation, :render_sub_navigation
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
module ClassMethods
|
49
|
+
# Sets the active navigation for all actions in this controller.
|
50
|
+
#
|
51
|
+
# The specified symbols must match the keys for your navigation items in your config/navigation.rb file.
|
52
|
+
def navigation(primary_navigation, sub_navigation=nil)
|
53
|
+
self.class_eval do
|
54
|
+
define_method :set_navigation do
|
55
|
+
current_navigation(primary_navigation, sub_navigation)
|
56
|
+
end
|
57
|
+
before_filter :set_navigation
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
module InstanceMethods
|
63
|
+
|
64
|
+
# Sets the active navigation. Call this method in any action to override the controller-wide active navigation
|
65
|
+
# specified by navigation.
|
66
|
+
#
|
67
|
+
# The specified symbols must match the keys for your navigation items in your config/navigation.rb file.
|
68
|
+
def current_navigation(primary_navigation, sub_navigation=nil)
|
69
|
+
@current_primary_navigation = primary_navigation
|
70
|
+
@current_secondary_navigation = sub_navigation
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module SimpleNavigation
|
2
|
+
|
3
|
+
# View helpers to render the navigation.
|
4
|
+
#
|
5
|
+
# Use render_primary_navigation to render your primary navigation with the configured renderer.
|
6
|
+
# Use render_sub_navigation to render the sub navigation belonging to the active primary navigation.
|
7
|
+
# Use render_navigation to render the primary navigation with the corresponding sub navigation rendered inside primary navigation item which is active.
|
8
|
+
#
|
9
|
+
# ==== Examples (using Haml)
|
10
|
+
# #primary_navigation= render_primary_navigation
|
11
|
+
#
|
12
|
+
# #sub_navigation= render_sub_navigation
|
13
|
+
#
|
14
|
+
# #main_navigation= render_navigation
|
15
|
+
#
|
16
|
+
module Helpers
|
17
|
+
|
18
|
+
# Renders the navigation according to the specified options-hash.
|
19
|
+
#
|
20
|
+
# The following options are supported:
|
21
|
+
# * <tt>level</tt> - defaults to :nested which renders the the sub_navigation for an active primary_navigation inside that active primary_navigation item.
|
22
|
+
# Other possible levels are :primary which only renders the primary_navigation (also see render_primary_navigation) and :secondary which only renders the sub_navigation (see render_sub_navigation).
|
23
|
+
# * <tt>context</tt> - specifies the context for which you would render the navigation. Defaults to :default which loads the default navigation.rb (i.e. config/navigation.rb)
|
24
|
+
# if you specify a context then the plugin tries to load the configuration file for that context, e.g. if you call <tt>render_navigation(:context => :admin)</tt> the file config/admin_navigation.rb
|
25
|
+
# will be loaded and used for rendering the navigation.
|
26
|
+
#
|
27
|
+
def render_navigation(*args)
|
28
|
+
args = [Hash.new] if args.empty?
|
29
|
+
default_options = {:context => :default, :level => :nested}
|
30
|
+
level, navigation_context = case args.first
|
31
|
+
when Hash
|
32
|
+
options = default_options.merge(args.first)
|
33
|
+
[options[:level], options[:context]]
|
34
|
+
when Symbol
|
35
|
+
[args[0], default_options.merge(args[1] || {})[:context]]
|
36
|
+
else
|
37
|
+
raise ArgumentError, "Invalid arguments"
|
38
|
+
end
|
39
|
+
SimpleNavigation.load_config(navigation_context)
|
40
|
+
SimpleNavigation::Configuration.eval_config(self, navigation_context)
|
41
|
+
case level
|
42
|
+
when :primary
|
43
|
+
SimpleNavigation.primary_navigation.render(@current_primary_navigation)
|
44
|
+
when :secondary
|
45
|
+
primary = SimpleNavigation.primary_navigation[@current_primary_navigation]
|
46
|
+
primary.sub_navigation.render(@current_secondary_navigation) if primary && primary.sub_navigation
|
47
|
+
when :nested
|
48
|
+
SimpleNavigation.primary_navigation.render(@current_primary_navigation, true, @current_secondary_navigation)
|
49
|
+
else
|
50
|
+
raise ArgumentError, "Invalid navigation level: #{level}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Renders the primary_navigation with the configured renderer. Calling render_navigation(:level => :primary) has the same effect.
|
55
|
+
def render_primary_navigation(options = {})
|
56
|
+
render_navigation(options.merge(:level => :primary))
|
57
|
+
end
|
58
|
+
|
59
|
+
# Renders the sub_navigation with the configured renderer. Calling render_navigation(:level => :secondary) has the same effect.
|
60
|
+
def render_sub_navigation(options = {})
|
61
|
+
render_navigation(options.merge(:level => :secondary))
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|