-
-
Save martinrehfeld/445977 to your computer and use it in GitHub Desktop.
A more clumsy take on breadcrumbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_bread_crumb(request, options = {}, breadcrumb = [], so_far = '/') | |
options.reverse_merge!( :not_available => "Not available", | |
:separator => " » " ) | |
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