Skip to content

Instantly share code, notes, and snippets.

@martinrehfeld
Forked from mtodd/first.rb
Created June 20, 2010 17:57
Show Gist options
  • Save martinrehfeld/445977 to your computer and use it in GitHub Desktop.
Save martinrehfeld/445977 to your computer and use it in GitHub Desktop.
A more clumsy take on breadcrumbs
def get_bread_crumb(request, separator = " » ", breadcrumb = [], so_far = '/')
url = request.respond_to?(:request_uri) ? request.request_uri : request
elements = url.split('/')
elements.each_with_index do |element, i|
is_last = i == elements.size - 1 # whether this is the last item or not
so_far += element + '/' # the URL for this element
breadcrumb << # append this element to the breadcrumb
case element
when /^\d+$/
begin
previous = elements[i - 1]
record = previous.singularize.camelize.constantize.find(element)
link_to_unless(last, record.display_name, so_far)
rescue ActiveRecord::RecordNotFound
element
end
else
link_to_unless(last, element.gsub("_"," ").titleize, so_far)
end
end
breadcrumb.join(separator)
rescue
'Not available'
end
def get_bread_crumb(request, options = {}, breadcrumb = [], so_far = '/')
options.reverse_merge!( :not_available => "Not available",
:separator => " &raquo; " )
url = request.respond_to?(:request_uri) ? request.request_uri : request
elements = url.split('/')
elements.each_with_index do |element, i|
is_last = i == elements.size - 1 # whether this is the last item or not
so_far += element + '/' # the URL for this element
breadcrumb << # append this element to the breadcrumb
case element
when "" then next
when /^\d+$/
begin
previous = elements[i - 1]
record = previous.singularize.camelize.constantize.find(element)
link_to_unless(last, record.display_name, so_far)
rescue ActiveRecord::RecordNotFound
element
end
else # String
link_to_unless(last, element.gsub("_"," ").titleize, so_far)
end
end
breadcrumb.join(options[:separator])
rescue
options[:not_available]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment