Sha256: 72c9d64decde1b0f204334d47b1781c537440b8fddddb761cd488745c9470d9d

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

module Gretel
  module HelperMethods
    include ActionView::Helpers::UrlHelper
    def controller # hack because ActionView::Helpers::UrlHelper needs a controller method
    end
    
    def self.included(base)
      base.send :helper_method, :breadcrumb_for, :breadcrumb
    end
    
    def breadcrumb(*args)
      options = args.extract_options!
      name, object = args[0], args[1]
      
      if name
        @_breadcrumb_name = name
        @_breadcrumb_object = object
      else
        if @_breadcrumb_name
          crumb = breadcrumb_for(@_breadcrumb_name, @_breadcrumb_object, options)
        elsif options[:show_root_alone]
          crumb = breadcrumb_for(:root, options)
        end
      end
      
      if crumb && options[:pretext]
        crumb = options[:pretext].html_safe + " " + crumb
      end
      
      crumb
    end
    
    def breadcrumb_for(*args)
      options = args.extract_options!
      link_last = options[:link_last]
      options[:link_last] = true
      separator = (options[:separator] || ">").html_safe

      name, object = args[0], args[1]
      
      crumb = Crumbs.get_crumb(name, object)
      if link_last
        out = link_to_if(link_last, crumb.link.text, crumb.link.url, crumb.link.options.merge(:class => "current"))
      else
        out = content_tag(:span, crumb.link.text, :class => "current")
      end
      
      while parent = crumb.parent
        last_parent = parent.name
        crumb = Crumbs.get_crumb(parent.name, parent.object)
        out = link_to(crumb.link.text, crumb.link.url, crumb.link.options) + " " + separator + " " + out
      end
      
      # TODO: Refactor this
      if options[:autoroot] && name != :root && last_parent != :root
        crumb = Crumbs.get_crumb(:root)
        out = link_to(crumb.link.text, crumb.link.url, crumb.link.options) + " " + separator + " " + out
      end
      
      out
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gretel-1.0.9 lib/gretel/helper_methods.rb