rpheath / navigation_helper
- Source
- Commits
- Network (21)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
e881967
rpheath (author)
Wed Dec 03 08:45:58 -0800 2008
navigation_helper / lib / navigation.rb
| 116d7ccb » | rpheath | 2008-10-27 | 1 | %w(error navigator).each do |f| | |
| 2 | require File.join(File.dirname(__FILE__), 'navigation', f) | ||||
| 3 | end | ||||
| 4 | # = NavigationHelper | ||||
| 5 | # Use this plugin if you are seeking a navigation solution that keeps track of | ||||
| 6 | # the current tab or section, and returns an unordered list of links for easy | ||||
| 7 | # styling in CSS. | ||||
| 8 | # | ||||
| 9 | # == Get it at GitHub | ||||
| 10 | # This repository is public on GitHub: | ||||
| 11 | # | ||||
| 12 | # git clone git://github.com/rpheath/navigation_helper.git | ||||
| 13 | # | ||||
| 14 | # == Examples | ||||
| 15 | # see the README for more details and a full list of example usage | ||||
| 16 | module RPH | ||||
| 17 | module Navigation | ||||
| e881967f » | rpheath | 2008-12-03 | 18 | SUBTITLES, ROUTES = {}, {} | |
| ef229475 » | rpheath | 2008-10-27 | 19 | ||
| 116d7ccb » | rpheath | 2008-10-27 | 20 | # InstanceMethods will be mixed in with ActionView::Base which will | |
| 21 | # make the navigation helper available to any view | ||||
| 22 | module InstanceMethods | ||||
| 23 | # called from any view and will return an unordered list containing | ||||
| 24 | # links to the sections passed in | ||||
| 25 | # | ||||
| 26 | # Example: | ||||
| 27 | # <%= navigation [:home, :about, :contact_me] -%> | ||||
| 28 | # | ||||
| 29 | # ...would render... | ||||
| 30 | # | ||||
| 31 | # <ul class="nav_bar"> | ||||
| 32 | # <li class="current"><a href="/home">Home</a></li> | ||||
| 33 | # <li><a href="/about">About</a></li> | ||||
| 34 | # <li><a href="/contact">Contact Me</a></li> | ||||
| 35 | # </ul> | ||||
| 36 | # | ||||
| 37 | # Configuration Options: | ||||
| 38 | # * +authorize+ - specifies which of the sections require authorization before showing up | ||||
| 39 | # (note: use <tt>:authorize => [:all]</tt> if all sections require authorization... i.e, an admin menu) | ||||
| 40 | # * +with+ - specifies the method to use to authorize against (defaults to <tt>logged_in?</tt> method... | ||||
| 41 | # Note - requires the <tt>authorize</tt> option to work) | ||||
| 42 | # * +hover_text+ - specifies to use the subtitles as hovertext instead of showing up as span's under the links | ||||
| 43 | def navigation(sections, options={}) | ||||
| ef229475 » | rpheath | 2008-10-27 | 44 | navigation, items = Navigator.new(sections, options), [] | |
| 116d7ccb » | rpheath | 2008-10-27 | 45 | ||
| 46 | navigation.sections.each do |link| | ||||
| ef229475 » | rpheath | 2008-10-27 | 47 | css = 'current' if link == controller.class.current_tab | |
| 48 | |||||
| 116d7ccb » | rpheath | 2008-10-27 | 49 | if navigation.methods_to_authorize.include?(link) | |
| 50 | items << content_tag(:li, construct(navigation, link), :class => [css.to_s, navigation.authorized_css.to_s].compact.join(' ')) if allowed?(navigation) | ||||
| 51 | else | ||||
| 52 | items << content_tag(:li, construct(navigation, link), :class => css.to_s) | ||||
| 53 | end | ||||
| 54 | end | ||||
| ef229475 » | rpheath | 2008-10-27 | 55 | ||
| 116d7ccb » | rpheath | 2008-10-27 | 56 | return '' if items.blank? | |
| e881967f » | rpheath | 2008-12-03 | 57 | content_tag(:ul, items, :class => 'navigation') | |
| 116d7ccb » | rpheath | 2008-10-27 | 58 | end | |
| e881967f » | rpheath | 2008-12-03 | 59 | ||
| 116d7ccb » | rpheath | 2008-10-27 | 60 | private | |
| ef229475 » | rpheath | 2008-10-27 | 61 | # builds the actual link and determines if subtitles are present | |
| 62 | def construct(nav, section) | ||||
| 63 | text = nav.text_for(section) | ||||
| e881967f » | rpheath | 2008-12-03 | 64 | path = ROUTES[section.to_sym] || send("#{section.to_s.downcase}_path") | |
| 65 | link = link_to(text, path) | ||||
| ef229475 » | rpheath | 2008-10-27 | 66 | ||
| e881967f » | rpheath | 2008-12-03 | 67 | link = link_to(text, path, :title => SUBTITLES[section]) if nav.wants_hover_text? | |
| 68 | link += content_tag(:span, SUBTITLES[section]) if nav.wants_subtitles? | ||||
| ef229475 » | rpheath | 2008-10-27 | 69 | ||
| 70 | link | ||||
| 71 | end | ||||
| 72 | |||||
| 73 | # checks if the authorization method exists; | ||||
| 74 | # then checks the boolean value it returns | ||||
| 75 | def allowed?(navigation) | ||||
| 76 | controller.respond_to?(navigation.authorization_method) && controller.send(navigation.authorization_method) | ||||
| 77 | end | ||||
| 116d7ccb » | rpheath | 2008-10-27 | 78 | end | |
| ef229475 » | rpheath | 2008-10-27 | 79 | ||
| 116d7ccb » | rpheath | 2008-10-27 | 80 | # ClassMethods will be extended by ActionController::Base which will | |
| 81 | # make the current_tab method available to any controller inhereting | ||||
| 82 | # from ActionController::Base | ||||
| 83 | module ClassMethods | ||||
| 84 | # used to set the current tab for any controller (defaults to current controller's name) | ||||
| 85 | # | ||||
| 86 | # Example: | ||||
| 87 | # class PublicController < ApplicationController | ||||
| 88 | # current_tab :home | ||||
| 89 | # end | ||||
| ef229475 » | rpheath | 2008-10-27 | 90 | def current_tab(tab=nil) | |
| 91 | self._current_tab = tab if tab | ||||
| 92 | self._current_tab ||= self.to_s.sub('Controller', '').downcase.to_sym | ||||
| 116d7ccb » | rpheath | 2008-10-27 | 93 | end | |
| 94 | end | ||||
| 95 | end | ||||
| 96 | end | ||||
