class NavLinks::LinkGenerator

Public Class Methods

new(request, title, path, html_options = {}, options = {}) click to toggle source
# File lib/nav_links/link_generator.rb, line 5
def initialize(request, title, path, html_options = {}, options = {})
  @request      = request
  @title        = title
  @path         = path
  @html_options = html_options
  @options      = options
end

Public Instance Methods

to_html() click to toggle source
# File lib/nav_links/link_generator.rb, line 13
def to_html
  html = link

  if @options[:wrapper]
    html = content_tag(@options[:wrapper], html, :class => wrapper_classes)
  end

  html.html_safe
end

Private Instance Methods

comparable_path_for(path) click to toggle source
# File lib/nav_links/link_generator.rb, line 109
def comparable_path_for(path)
  if @options[:ignore_params]
    path.gsub(/\?.*/, '')
  else
    path
  end
end
controller_for(path) click to toggle source
# File lib/nav_links/link_generator.rb, line 105
def controller_for(path)
  Rails.application.routes.recognize_path(path)[:controller]
end
current_controller() click to toggle source
# File lib/nav_links/link_generator.rb, line 45
def current_controller
  controller_for(@request.path)
end
current_path() click to toggle source
# File lib/nav_links/link_generator.rb, line 25
def current_path
  comparable_path_for(@request.fullpath)
end
current_segment() click to toggle source
# File lib/nav_links/link_generator.rb, line 61
def current_segment
  segment_for(current_controller, link_path)
end
html_options() click to toggle source
# File lib/nav_links/link_generator.rb, line 89
def html_options
  selected? ? @html_options.merge(:class => link_classes) : @html_options
end
path_controller() click to toggle source
# File lib/nav_links/link_generator.rb, line 41
def path_controller
  controller_for(@path)
end
path_segment() click to toggle source
# File lib/nav_links/link_generator.rb, line 57
def path_segment
  segment_for(path_controller, current_path)
end
paths_match?() click to toggle source
# File lib/nav_links/link_generator.rb, line 65
def paths_match?
  current_path == link_path
end
segment_for(controller, path) click to toggle source
# File lib/nav_links/link_generator.rb, line 49
def segment_for(controller, path)
  if @options[:controller_segment]
    controller.split('/')[segment_position]
  elsif @options[:url_segment]
    path.split('/')[segment_position]
  end
end
segment_position() click to toggle source
# File lib/nav_links/link_generator.rb, line 33
def segment_position
  if @options[:controller_segment]
    @options[:controller_segment] - 1
  elsif @options[:url_segment]
    @options[:url_segment]
  end
end
segments_match?() click to toggle source
# File lib/nav_links/link_generator.rb, line 69
def segments_match?
  path_segment && path_segment == current_segment
end
selected?() click to toggle source
# File lib/nav_links/link_generator.rb, line 73
def selected?
  paths_match? || segments_match?
end
selected_class() click to toggle source
# File lib/nav_links/link_generator.rb, line 77
def selected_class
  @options[:selected_class] || 'selected'
end
wrapper_classes() click to toggle source
# File lib/nav_links/link_generator.rb, line 97
def wrapper_classes
  if selected?
    "#{selected_class} #{@options[:wrapper_class]}"
  else
    @options[:wrapper_class]
  end
end