class Nav

Attributes

active[RW]
block[RW]
controller[RW]
name[RW]
strategy[RW]
weights[RW]

Public Class Methods

build(*args, &block) click to toggle source

for use when the controller instance is available now

Nav.build do |nav|

nav.link 'Home', root_path

end

# File lib/rails_nav.rb, line 69
def Nav.build(*args, &block)
  if defined?(::ActionController::Base)
    controller = args.grep(::ActionController::Base).first
    args.delete(controller)
  end

  new(*args, &block).tap do |nav|
    nav.strategy = :call
    nav.controller = controller || Current.controller || Current.mock_controller
    nav.build!
    nav.compute_active!
  end
end
dependencies() click to toggle source
# File lib/rails_nav.rb, line 13
def Nav.dependencies
  {
    'rails_current' => [ 'rails_current' , ' >= 1.6'   ],
    'rails_helper'  => [ 'rails_helper'  , ' >= 1.2'   ],
    'map'           => [ 'map'           , ' >= 6.5'   ],
  }
end
description() click to toggle source
# File lib/rails_nav.rb, line 21
def Nav.description
  'declarative navigation for rails applications'
end
extend_action_controller!() click to toggle source

factored out mixin for controllers/views

# File lib/rails_nav.rb, line 381
def Nav.extend_action_controller!
  if defined?(::ActionController::Base)
    ::ActionController::Base.module_eval do
      class << self
        def nav_for(*args, &block)
          options = args.extract_options!.to_options!
          name = args.first || options[:name] || :main
          which_nav = [:nav, name].join('_')
          define_method(which_nav){ Nav.for(name, &block) }
          protected(which_nav)
        end
        alias_method(:nav, :nav_for)
      end

      def nav_for(*args, &block)
        options = args.extract_options!.to_options!
        name = args.first || options[:name] || :main
        Nav.for(name, &block).for(controller = self)
      end
      alias_method(:nav, :nav_for)

      helper do
        def nav_for(*args, &block)
          options = args.extract_options!.to_options!
          name = args.first || options[:name] || :main
          which_nav = [:nav, name].join('_')
          nav = controller.send(which_nav).for(controller)
        end
        alias_method(:nav, :nav_for)
      end
    end
  end
end
for(*args, &block) click to toggle source

for use in a controller

nav_for(:main) do |nav|

  nav.link 'Home', root_path

end
# File lib/rails_nav.rb, line 44
def Nav.for(*args, &block)
  new(*args, &block).tap do |nav|
    nav.strategy = :instance_exec
  end
end
nav(*args, &block)
Alias for: nav_for
nav_for(*args, &block) click to toggle source
Also aliased as: nav
new(name = :main, &block) click to toggle source
# File lib/rails_nav.rb, line 92
def initialize(name = :main, &block)
  @name = name.to_s
  @block = block
  @already_computed_active = false
  @controller = nil
  @strategy = :call
  @active = nil
end
version() click to toggle source
# File lib/rails_nav.rb, line 9
def Nav.version()
  '2.8.0'
end

Public Instance Methods

build!() click to toggle source
# File lib/rails_nav.rb, line 106
def build!
  evaluate(@block, nav = self)
end
compute_active() click to toggle source
# File lib/rails_nav.rb, line 179
def compute_active
  compute_active! unless @already_computed_active
  self
end
compute_active!() click to toggle source
# File lib/rails_nav.rb, line 117
def compute_active!
  nav = self

  unless empty?
    weights = []

    each_with_index do |link, index|
      link.controller = @controller
      active = link.compute_active!

      weight =
        begin
          case active
            when nil, false
              -1
            when true
              0
            else
              Integer(active)
          end
        rescue
          -1
        end

      link.weight = weight
      weights[index] = weight
    end

    nav.weights = weights

    each_with_index do |link, index|
      link.active = false
    end

    more_than_one_link =
      size > 1

    equivalently_active =
      weights.uniq.size < 2

    no_clear_winner =
      more_than_one_link && equivalently_active

    active_link =
      if no_clear_winner
        detect{|link| link.default}
      else
        max = weights.max
        longest_matching_link = select{|link| link.weight == max}.sort{|a,b| a.content.size <=> b.content.size}.last
      end

    if active_link
      active_link.active = true
      nav.active = active_link
    end

    @already_computed_active = true
  end

  nav
end
evaluate(block, *args) click to toggle source
# File lib/rails_nav.rb, line 101
def evaluate(block, *args)
  args = args.slice(0 .. (block.arity < 0 ? -1 : block.arity))
  @strategy == :instance_exec ? @controller.instance_exec(*args, &block) : block.call(*args)
end
for(controller) click to toggle source

for use in a view

<%= nav_for(:main) %>
# File lib/rails_nav.rb, line 54
def for(controller)
  @controller = controller
  build!
  compute_active!
  self
end
fullpath() click to toggle source
# File lib/rails_nav.rb, line 188
def fullpath
  request.fullpath
end
html_safe()
Alias for: to_html
html_safe?() click to toggle source
# File lib/rails_nav.rb, line 236
def html_safe?
  true
end
nav_for(*args, &block) click to toggle source
path_info() click to toggle source
# File lib/rails_nav.rb, line 192
def path_info
  path_info = fullpath.scan(%r{[^/]+})
end
request() click to toggle source
# File lib/rails_nav.rb, line 184
def request
  @controller.send(:request)
end
template() click to toggle source
# File lib/rails_nav.rb, line 208
    def template
      @template ||= Template.new do
        <<-__
          <nav class="nav-<%= name %>">
            <ul>
              <% each do |link| %>
                <li class="<%= link.active ? :active : :inactive %>">
                  <a href="<%= link.url %>" class="<%= link.active ? :active : :inactive %>"><%= link.content %></a>
                </li>
              <% end %>
            </ul>
          </nav>
        __
      end
    end
template=(template) click to toggle source
# File lib/rails_nav.rb, line 224
def template=(template)
  @template = Template.new(template.to_s){}
end
to_html() click to toggle source
# File lib/rails_nav.rb, line 228
def to_html
  template.render
end
Also aliased as: to_s, to_str, html_safe
to_s()
Alias for: to_html
to_str()
Alias for: to_html