Class: NavigationPresenter

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper
Defined in:
lib/happy-nav/navigation_presenter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items = [], options = {}, current_items = []) ⇒ NavigationPresenter

Returns a new instance of NavigationPresenter.



7
8
9
10
11
# File 'lib/happy-nav/navigation_presenter.rb', line 7

def initialize(items = [], options = {}, current_items = [])
  @items = items
  @options = options
  @current_items = current_items
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



5
6
7
# File 'lib/happy-nav/navigation_presenter.rb', line 5

def items
  @items
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/happy-nav/navigation_presenter.rb', line 5

def options
  @options
end

Instance Method Details

#base_urlObject



76
77
78
79
80
81
82
# File 'lib/happy-nav/navigation_presenter.rb', line 76

def base_url
  if options[:base_url]
    options[:base_url]
  else
    admin? ? '/admin' : ''
  end
end

#current_item(current_item) ⇒ Object



84
85
86
# File 'lib/happy-nav/navigation_presenter.rb', line 84

def current_item(current_item)
  @current_items << current_item
end


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/happy-nav/navigation_presenter.rb', line 46

def item_link(item, options = {})

  item = format_item(item, options)
  content = item[:text]
  
  if item[:summary]
    item[:title] += ': '+item[:summary]
    content = h.(:strong, content) + ' ' + (:em, item[:summary])
  end
  
  if item[:id] || @options[:id]
    item[:id] = item[:permalink] if @options[:id] === true || item[:id] === true
    item[:id] = "nav_#{item[:id]}"
  end
  
  h.link_to content, item[:url], {
    :id => item[:id],
    :class => (current_item?(item[:permalink]) ? 'current' : nil),
    :title => item[:title],
  }
end

#item_url(text) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/happy-nav/navigation_presenter.rb', line 68

def item_url(text)
  if (text == 'Home')
    admin? ? '/admin' : '/'
  else
    base_url + '/' + text.parameterize('-')
  end
end

#nested_navigation(item) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/happy-nav/navigation_presenter.rb', line 31

def nested_navigation(item)
  unless @options[:flat]
    item = format_item(item)
    if item[:items] && current_item?(item[:permalink])
      options = {
        :level => next_level,
        :class => 'nav-sub-' + next_level.to_s,
        :base_url => item[:url],
        :wrap => false
      }
      NavigationPresenter.new(item[:items], options, @current_items[1..-1]).to_html
    end
  end
end

#to_htmlObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/happy-nav/navigation_presenter.rb', line 13

def to_html
  unless @items.empty?
    wrap do
      h. :ul, :class => @options[:class] do i=0
        content = ''
        @items.each do |item| i=i+1
          content += h. :li, :class => classes(i) do 
            item_link(item) + nested_navigation(item).to_s
          end
        end
        content.html_safe
      end
    end
  else
    ''
  end
end