Skip to content

Instantly share code, notes, and snippets.

@randland
Forked from mtodd/enumerable_with_context.rb
Created June 22, 2010 15:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randland/448656 to your computer and use it in GitHub Desktop.
Save randland/448656 to your computer and use it in GitHub Desktop.
Breadcrumbs. Uses Route recognition and intelligent naming
module ApplicationHelper
def breadcrumb(url, options = {})
options.reverse_merge!( :separator => ' » ',
:home_link => 'Home')
path_segments = url.sub(/\?.*/,'').split('/')[1..-1]
return '' if path_segments.blank? # ie: suppress breadcrumbs on homepage
path_array = path_segments.inject([]){|m,x| m.push(m.empty? ? x : "#{m[-1]}/#{x}")}
middle_links = path_array[0..-2].map do |path|
link_to path_name_for(path), "/#{path}"
end
returning [middle_links, path_name_for(path_array[-1])] do |links|
links.unshift link_to(options[:home_link], root_path) if options[:home_link]
end.flatten.join(options[:separator])
end
def path_name_for path = ''
route = ActionController::Routing::Routes.recognize_path('/' + path, {:method => :get})
controller_name = route[:controller].split('/')[-1]
case route[:action]
when 'show' then controller_name.singularize.camelize.constantize.find(route[:id]).name
when 'index' then controller_name.titleize
else route[:action].titleize
end
rescue
(route ? "#{route[:controller]} #{route[:action]}" : path).titleize
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment