Module: Mariner::Helper

Defined in:
lib/mariner/helper.rb

Overview

Public: Gets included in controllers (see the Railtie)

Instance Method Summary collapse

Instance Method Details

#render_navigation(config_path = nil, renderer = nil) ⇒ Object

Public: A shortcut for rendering navigation. Made available to both controllers and views.

config_path - A symbol or slash-separated string path to the configuration group you want to render.

renderer - The rendering strategy to use. Can be a symbol or actual rendering stragey. When a symbol, looks in the ‘Mariner.rendering_strategies` hash using the given symbol as the key. Raises an error if not found. When a rendering strategy, passes the strategy on to the target group’s #render method.

Examples:

Mariner.configure do
  a_group do
    root_path "Home"

    a_sub_group do
      users_path "Manage Users"
    end
  end
end

render_navigation
#=> renders the entire nav tree

render_navigation :a_group
#=> renders the `a_group` nav tree

render_navigation "a_group/a_sub_group"
#=> renders the nav tree of `a_sub_group` under `a_group`

render_navigation :a_group, FakeRenderingStrategy.new
#=> renders `a_group` with a FakeRenderingStrategy instance

render_navigation :a_group, :other_strategy
#=> renders `a_group` with Mariner.rendering_strategies[:other_strategy]


49
50
51
52
53
54
# File 'lib/mariner/helper.rb', line 49

def render_navigation(config_path=nil, renderer=nil)
  target   = target_from_path(config_path)
  strategy = rendering_strategy_from(renderer)

  strategy ? target.render(strategy) : target.render
end

#render_navigations(*args) ⇒ Object

Public: For when you want to render multiple specific nav trees

*args - A comma separated list of groups to render. The last argument may be an options hash. Options:

  • :rendering_strategy - A symbol, class, or object to use for rendering.

See #render_navigation for details.

Examples:

Mariner.configure do
  group_a do
    root_path "Home"

    sub_group do
      products_path "Products"
    end
  end

  group_b do
    users_path "Manage Users"
  end
end

render_navigations 'group_a/sub_group', :group_b
render_navigations :group_a, :group_b, :rendering_strategy => :different_strategy


111
112
113
114
# File 'lib/mariner/helper.rb', line 111

def render_navigations(*args)
  renderer = args.extract_options![:rendering_strategy]
  args.map { |target_path| render_navigation(*[target_path, renderer].compact) }.join
end

#render_sub_navigations(config_path = nil, renderer = nil) ⇒ Object

Public: For when you want to render all the configurations under a given group but you don’t want to render the group itself.

Examples:

Mariner.configure do
  group_a do
    root_path "Home"
  end

  group_b do
    users_path "Manage Users"
  end
end

render_sub_navigations
#=> renders the group_a and group_b trees and joins the result


74
75
76
77
78
79
80
81
82
# File 'lib/mariner/helper.rb', line 74

def render_sub_navigations(config_path=nil, renderer=nil)
  target = target_from_path(config_path)
  strategy = rendering_strategy_from(renderer)

  target.configurations.map do |c|
    _, entity = c
    strategy ? entity.render(strategy) : entity.render
  end.join
end