module SimpleNavigation

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

Constants

VERSION

Public Class Methods

active_item_container_for(level) click to toggle source

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.

# File lib/simple_navigation.rb, line 162
def active_item_container_for(level)
  case level
  when :all then primary_navigation
  when :leaves then primary_navigation.active_leaf_container
  when Integer then primary_navigation.active_item_container_for(level)
  when Range then primary_navigation.active_item_container_for(level.min)
  else
    fail ArgumentError, "Invalid navigation level: #{level}"
  end
end
config() click to toggle source

Returns the singleton instance of the SimpleNavigation::Configuration

# File lib/simple_navigation.rb, line 142
def config
  SimpleNavigation::Configuration.instance
end
config_file(navigation_context = :default) click to toggle source

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.

# File lib/simple_navigation.rb, line 100
def config_file(navigation_context = :default)
  config_file_paths
    .map { |path| File.join(path, config_file_name(navigation_context)) }
    .find { |full_path| File.exist?(full_path) }
end
config_file?(navigation_context = :default) click to toggle source

Returns true if the ::config_file for specified context does exist.

# File lib/simple_navigation.rb, line 93
def config_file?(navigation_context = :default)
  !!config_file(navigation_context)
end
config_file_name(navigation_context = :default) click to toggle source

Returns the name of the config file for the given navigation_context

# File lib/simple_navigation.rb, line 107
def config_file_name(navigation_context = :default)
  prefix = if navigation_context == :default
             ''
           else
             "#{navigation_context.to_s.underscore}_"
           end
  "#{prefix}navigation.rb"
end
config_file_path=(path) click to toggle source

Resets the list of config_file_paths to the specified path

# File lib/simple_navigation.rb, line 117
def config_file_path=(path)
  self.config_file_paths = [path]
end
current_navigation_for(level) click to toggle source

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

# File lib/simple_navigation/rails_controller_methods.rb, line 8
def self.current_navigation_for(level)
  adapter.controller.instance_variable_get(:"@sn_current_navigation_#{level}")
end
default_config_file_path() click to toggle source
# File lib/simple_navigation.rb, line 88
def default_config_file_path
  File.join(root, 'config')
end
explicit_navigation_args() click to toggle source
# File lib/simple_navigation/rails_controller_methods.rb, line 2
def self.explicit_navigation_args
  adapter.controller.instance_variable_get(:'@sn_current_navigation_args')
end
framework() click to toggle source

Returns the current framework in which the plugin is running.

# File lib/simple_navigation.rb, line 62
def framework
  return :rails if defined?(Rails)
  return :padrino if defined?(Padrino)
  return :sinatra if defined?(Sinatra)
  return :nanoc if defined?(Nanoc3)
  fail 'simple_navigation currently only works for Rails, Sinatra and '             'Padrino apps'
end
handle_explicit_navigation() click to toggle source

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.

# File lib/simple_navigation/rails_controller_methods.rb, line 15
def self.handle_explicit_navigation
  return unless explicit_navigation_args
  level, navigation = parse_explicit_navigation_args
  navigation_level = :"@sn_current_navigation_#{level}"
  adapter.controller.instance_variable_set(navigation_level, navigation)
end
init_adapter_from(context) click to toggle source

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

# File lib/simple_navigation.rb, line 84
def init_adapter_from(context)
  self.adapter = adapter_class.new(context)
end
load_adapter() click to toggle source

Loads the adapter for the current framework

# File lib/simple_navigation.rb, line 72
def load_adapter
  self.adapter_class =
    case framework
    when :rails then SimpleNavigation::Adapters::Rails
    when :sinatra then SimpleNavigation::Adapters::Sinatra
    when :padrino then SimpleNavigation::Adapters::Padrino
    when :nanoc then SimpleNavigation::Adapters::Nanoc
    end
end
load_config(navigation_context = :default) click to toggle source

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

# File lib/simple_navigation.rb, line 123
def load_config(navigation_context = :default)
  unless config_file?(navigation_context)
    fail "Config file '#{config_file_name(navigation_context)}' not "               "found in path(s) #{config_file_paths.join(', ')}!"
  end

  # FIXME: what about update_config and update_config! methods ?
  if environment == 'production'
    config_files[navigation_context] ||= begin
      IO.read(config_file(navigation_context))
    end
  else
    config_files[navigation_context] = begin
      IO.read(config_file(navigation_context))
    end
  end
end
parse_explicit_navigation_args() click to toggle source
# File lib/simple_navigation/rails_controller_methods.rb, line 22
def self.parse_explicit_navigation_args
  args = if explicit_navigation_args.empty?
           [{}]
         else
           explicit_navigation_args
         end
  indexed_args = args_indexed_by_level(args)
  deepest = deepest_level_and_item(indexed_args)

  if deepest.first.zero?
    fail ArgumentError, 'Invalid level specified or item key not found'
  end

  deepest
end
primary_navigation() click to toggle source

Returns the ItemContainer that contains the items for the primary navigation

# File lib/simple_navigation.rb, line 148
def primary_navigation
  config.primary_navigation
end
register_renderer(renderer_hash) click to toggle source

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)
# File lib/simple_navigation.rb, line 183
def register_renderer(renderer_hash)
  registered_renderers.merge!(renderer_hash)
end
set_env(root, environment) click to toggle source

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

# File lib/simple_navigation.rb, line 55
def set_env(root, environment)
  self.root = root
  self.environment = environment
  config_file_paths << SimpleNavigation.default_config_file_path
end

Private Class Methods

apply_defaults(options) click to toggle source
# File lib/simple_navigation.rb, line 189
def apply_defaults(options)
  options[:level] = options.delete(:levels) if options[:levels]
  { context: :default, level: :all }.merge(options)
end
args_indexed_by_level(navigation_args) click to toggle source
# File lib/simple_navigation/rails_controller_methods.rb, line 45
def self.args_indexed_by_level(navigation_args)
  if navigation_args.first.is_a?(Hash)
    navigation_args.first
  elsif navigation_args.size == 1
    level = primary_navigation.level_for_item(navigation_args.first)
    level ? { :"level_#{level}" => navigation_args.first } : {}
  else
    navigation_args.each_with_index
                   .with_object({}) do |(arg, i), h|
                     h[:"level_#{i + 1}"] = arg
                   end
  end
end
deepest_level_and_item(navigation_args) click to toggle source
# File lib/simple_navigation/rails_controller_methods.rb, line 40
def self.deepest_level_and_item(navigation_args)
  navigation_args.map { |k, v| [k.to_s[/level_(\d)/, 1].to_i, v] }
                 .max
end