Module: NavigationHelper

Defined in:
app/helpers/navigation_helper.rb

Instance Method Summary collapse

Instance Method Details

#active_if_here(matches) ⇒ Object

Returns ' active' if you are on a given controller

- active_if_here(:users) => ' active' if users controller

Or on one of a list of controllers

  • active_if_here([:users, :blogs, :comments])

Or on certain action(s) in a certain controller

- active_if_here(:users => :index, :blogs => [:create, :update], :comments)

Useful for navigation elements that have a certain state when your on a given page. Returns nil if there are no matches so when passing:

link_to 'link', '#', :class => active_if_here(:users)

will not even set a class attribute if nil is passed back.


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/helpers/navigation_helper.rb', line 14

def active_if_here(matches)
  current_controller = controller.controller_name.to_sym
  current_action     = controller.action_name.to_sym

  sections =  case matches
              when Hash
                matches
              when Array
                s = {}
                matches.each {|c| s[c] = :all}
                s
              else
                {matches => :all}
              end

  active = nil
  sections.each do |controller, actions|
    actions = ([] << actions) unless actions.kind_of?(Array)
    active = ' active' if current_controller == controller && (actions.include?(:all) || actions.include?(current_action))
  end
  active
end

#page_count_from_end(current_page, total_pages) ⇒ Object

Returns the page number in reverse order. Needed when reverse chronological paginating notices but want to display the actual chronological occurrence number.

E.G. - Given 6 notices, page 2 shows the second from last occurrence indexed by 1, but it is diplaying the 5th ever occurence of that error.


44
45
46
# File 'app/helpers/navigation_helper.rb', line 44

def page_count_from_end(current_page, total_pages)
  (total_pages.to_i - current_page.to_i) + 1
end