Module: SimpleNavigation

Extended by:
Forwardable
Defined in:
lib/simple_navigation.rb,
lib/simple_navigation/adapters.rb,
lib/simple_navigation/core/item.rb,
lib/simple_navigation/rendering.rb,
lib/simple_navigation/adapters/base.rb,
lib/simple_navigation/adapters/rails.rb,
lib/simple_navigation/adapters/rails.rb,
lib/simple_navigation/adapters/padrino.rb,
lib/simple_navigation/adapters/sinatra.rb,
lib/simple_navigation/core/item_adapter.rb,
lib/simple_navigation/rendering/helpers.rb,
lib/simple_navigation/core/configuration.rb,
lib/simple_navigation/core/item_container.rb,
lib/simple_navigation/core/items_provider.rb,
lib/simple_navigation/rendering/renderer/base.rb,
lib/simple_navigation/rendering/renderer/list.rb,
lib/simple_navigation/rails_controller_methods.rb,
lib/simple_navigation/rendering/renderer/links.rb,
lib/simple_navigation/rendering/renderer/breadcrumbs.rb

Overview

A plugin for generating a simple navigation. See README for resources on usage instructions.

Defined Under Namespace

Modules: Adapters, ControllerMethods, Helpers, Renderer Classes: Configuration, Item, ItemAdapter, ItemContainer, ItemsProvider, Railtie

Class Method Summary collapse

Class Method Details

.active_item_container_for(level) ⇒ Object

Returns the active item container for the specified level. Valid levels are

  • :all - in this case the primary_navigation is returned.

  • :leaves - the ‘deepest’ active item_container will be returned

  • a specific level - the active item_container for the specified level will be returned

  • a range of levels - the active item_container for the range’s minimum will be returned

Returns nil if there is no active item_container for the specified level.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/simple_navigation.rb', line 121

def active_item_container_for(level)
  case level
  when :all
    self.primary_navigation
  when :leaves
    self.primary_navigation.active_leaf_container
  when Integer
    self.primary_navigation.active_item_container_for(level)
  when Range
    self.primary_navigation.active_item_container_for(level.min)
  else
    raise ArgumentError, "Invalid navigation level: #{level}"
  end
end

.configObject

Returns the singleton instance of the SimpleNavigation::Configuration



105
106
107
# File 'lib/simple_navigation.rb', line 105

def config
  SimpleNavigation::Configuration.instance
end

.config_file(navigation_context = :default) ⇒ Object

Returns the path to the config file for the given navigation context or nil if no matching config file can be found. If multiple config_paths are set, it returns the first matching path.



79
80
81
# File 'lib/simple_navigation.rb', line 79

def config_file(navigation_context = :default)
  config_file_paths.collect { |path| File.join(path, config_file_name(navigation_context)) }.detect {|full_path| File.exists?(full_path)}
end

.config_file?(navigation_context = :default) ⇒ Boolean

Returns true if the config_file for specified context does exist.

Returns:

  • (Boolean)


73
74
75
# File 'lib/simple_navigation.rb', line 73

def config_file?(navigation_context = :default)
  !!config_file(navigation_context)
end

.config_file_name(navigation_context = :default) ⇒ Object

Returns the name of the config file for the given navigation_context



84
85
86
87
# File 'lib/simple_navigation.rb', line 84

def config_file_name(navigation_context = :default)
  prefix = navigation_context == :default ? '' : "#{navigation_context.to_s.underscore}_"
  "#{prefix}navigation.rb"      
end

.config_file_path=(path) ⇒ Object

Resets the list of config_file_paths to the specified path



90
91
92
# File 'lib/simple_navigation.rb', line 90

def config_file_path=(path)
  self.config_file_paths = [path]
end

.current_navigation_for(level) ⇒ Object

Reads the current navigation for the specified level from the controller. Returns nil if there is no current navigation set for level.



11
12
13
# File 'lib/simple_navigation/rails_controller_methods.rb', line 11

def current_navigation_for(level)
  self.adapter.controller.instance_variable_get(:"@sn_current_navigation_#{level}")
end

.default_config_file_pathObject



68
69
70
# File 'lib/simple_navigation.rb', line 68

def default_config_file_path
  File.join(SimpleNavigation.root, 'config')
end

.explicit_navigation_argsObject



5
6
7
# File 'lib/simple_navigation/rails_controller_methods.rb', line 5

def explicit_navigation_args
  self.adapter.controller.instance_variable_get(:"@sn_current_navigation_args")
end

.frameworkObject

Returns the current framework in which the plugin is running.



44
45
46
47
48
49
# File 'lib/simple_navigation.rb', line 44

def framework
  return :rails if defined?(Rails)
  return :padrino if defined?(Padrino)
  return :sinatra if defined?(Sinatra)
  raise 'simple_navigation currently only works for Rails, Sinatra and Padrino apps'
end

.handle_explicit_navigationObject

If any navigation has been explicitely set in the controller this method evaluates the specified args set in the controller and sets the correct instance variable in the controller.



17
18
19
20
21
22
# File 'lib/simple_navigation/rails_controller_methods.rb', line 17

def handle_explicit_navigation
  if SimpleNavigation.explicit_navigation_args
    level, navigation = parse_explicit_navigation_args
    self.adapter.controller.instance_variable_set(:"@sn_current_navigation_#{level}", navigation)
  end
end

.init_adapter_from(context) ⇒ Object

Creates a new adapter instance based on the context in which render_navigation has been called.



64
65
66
# File 'lib/simple_navigation.rb', line 64

def init_adapter_from(context)
  self.adapter = self.adapter_class.new(context)
end

.load_adapterObject

Loads the adapter for the current framework



52
53
54
55
56
57
58
59
60
61
# File 'lib/simple_navigation.rb', line 52

def load_adapter
  self.adapter_class = case framework
  when :rails
    SimpleNavigation::Adapters::Rails
  when :sinatra
    SimpleNavigation::Adapters::Sinatra
  when :padrino
    SimpleNavigation::Adapters::Padrino
  end
end

.load_config(navigation_context = :default) ⇒ Object

Reads the config_file for the specified navigation_context and stores it for later evaluation.



95
96
97
98
99
100
101
102
# File 'lib/simple_navigation.rb', line 95

def load_config(navigation_context = :default)
  raise "Config file '#{config_file_name(navigation_context)}' not found in path(s) #{config_file_paths.join(', ')}!" unless config_file?(navigation_context)      
  if self.environment == 'production'
    self.config_files[navigation_context] ||= IO.read(config_file(navigation_context))
  else
    self.config_files[navigation_context] = IO.read(config_file(navigation_context))
  end
end

.parse_explicit_navigation_argsObject

TODO: refactor this ugly thing to make it nice and short

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/simple_navigation/rails_controller_methods.rb', line 25

def parse_explicit_navigation_args
  args = SimpleNavigation.explicit_navigation_args
  args = [Hash.new] if args.empty?
  if args.first.kind_of? Hash
    options = args.first
  else # args is a list of current navigation for several levels
    options = {}
    if args.size == 1 #only one navi-key has been specified, try to find out level
      level = SimpleNavigation.primary_navigation.level_for_item(args.first)
      options[:"level_#{level}"] = args.first if level
    else
      args.each_with_index {|arg, i| options[:"level_#{i + 1}"] = arg}
    end
  end
  #only the deepest level is relevant
  level = options.inject(0) do |max, kv|
    kv.first.to_s =~ /level_(\d)/
    max = $1.to_i if $1.to_i > max
    max
  end
  raise ArgumentError, "Invalid level specified or item key not found" if level == 0
  [level, options[:"level_#{level}"]]
end

.primary_navigationObject

Returns the ItemContainer that contains the items for the primary navigation



110
111
112
# File 'lib/simple_navigation.rb', line 110

def primary_navigation
  config.primary_navigation
end

.register_renderer(renderer_hash) ⇒ Object

Registers a renderer.

Example

To register your own renderer:

SimpleNavigation.register_renderer :my_renderer => My::RendererClass

Then in the view you can call:

render_navigation(:renderer => :my_renderer)


146
147
148
# File 'lib/simple_navigation.rb', line 146

def register_renderer(renderer_hash)
  self.registered_renderers.merge!(renderer_hash)
end

.set_env(root, environment) ⇒ Object

Sets the root path and current environment as specified. Also sets the default config_file_path.



37
38
39
40
41
# File 'lib/simple_navigation.rb', line 37

def set_env(root, environment)
  self.root = root
  self.environment = environment
  self.config_file_paths << SimpleNavigation.default_config_file_path
end