Sha256: fd2be506e87f7e6806cd5d5d94a7b63aa04888ab3cd74e1e5cee164fd0d0f724

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 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)
      out = link_to_if(link_last, crumb.link.text, crumb.link.url)
      
      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) + " " + 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) + " " + separator + " " + out
      end
      
      out
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gretel-1.0.4 lib/gretel/helper_methods.rb
gretel-1.0.3 lib/gretel/helper_methods.rb
gretel-1.0.1 lib/gretel/helper_methods.rb
gretel-1.0.0 lib/gretel/helper_methods.rb