Module: SimpleNavigation::Helpers

Defined in:
lib/simple_navigation/helpers.rb

Overview

View helpers to render the navigation.

Use render_primary_navigation to render your primary navigation with the configured renderer. Use render_sub_navigation to render the sub navigation belonging to the active primary navigation. Use render_navigation to render the primary navigation with the corresponding sub navigation rendered inside primary navigation item which is active.

Examples (using Haml)

#primary_navigation= render_primary_navigation

#sub_navigation= render_sub_navigation

#main_navigation= render_navigation

Instance Method Summary collapse

Instance Method Details

#render_navigation(*args) ⇒ Object

Renders the navigation according to the specified options-hash.

The following options are supported:

  • level - defaults to :nested which renders the the sub_navigation for an active primary_navigation inside that active primary_navigation item. 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).

  • context - specifies the context for which you would render the navigation. Defaults to :default which loads the default navigation.rb (i.e. config/navigation.rb) if you specify a context then the plugin tries to load the configuration file for that context, e.g. if you call render_navigation(:context => :admin) the file config/admin_navigation.rb will be loaded and used for rendering the navigation.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/simple_navigation/helpers.rb', line 27

def render_navigation(*args)
  args = [Hash.new] if args.empty?
  default_options = {:context => :default, :level => :nested}
  level, navigation_context = case args.first 
  when Hash
    options = default_options.merge(args.first)
    [options[:level], options[:context]]
  when Symbol
    [args[0], default_options.merge(args[1] || {})[:context]]
  else
    raise ArgumentError, "Invalid arguments"
  end
  SimpleNavigation.load_config(navigation_context)
  SimpleNavigation::Configuration.eval_config(self, navigation_context)
  case level
  when :primary
    SimpleNavigation.primary_navigation.render(@current_primary_navigation)
  when :secondary
    primary = SimpleNavigation.primary_navigation[@current_primary_navigation]
    primary.sub_navigation.render(@current_secondary_navigation) if primary && primary.sub_navigation
  when :nested
    SimpleNavigation.primary_navigation.render(@current_primary_navigation, true, @current_secondary_navigation)
  else
    raise ArgumentError, "Invalid navigation level: #{level}"
  end
end

#render_primary_navigation(options = {}) ⇒ Object

Renders the primary_navigation with the configured renderer. Calling render_navigation(:level => :primary) has the same effect.



55
56
57
# File 'lib/simple_navigation/helpers.rb', line 55

def render_primary_navigation(options = {})
  render_navigation(options.merge(:level => :primary))
end

#render_sub_navigation(options = {}) ⇒ Object

Renders the sub_navigation with the configured renderer. Calling render_navigation(:level => :secondary) has the same effect.



60
61
62
# File 'lib/simple_navigation/helpers.rb', line 60

def render_sub_navigation(options = {})
  render_navigation(options.merge(:level => :secondary))
end