|
# lib/phone_breadcrumbs_builder.rb |
|
# inspired by https://gist.githubusercontent.com/marlosirapuan/3ceb02972400ea98ac5c8f419a61806b/raw/d994dc2d6b35702b4a66e2d1a01a9e9956883375/bulma_breadcrumbs_builder.rb |
|
# |
|
# Make use of your Breadcrumbs in your phone-specific views, to give a simple page title and navigation up 1 |
|
# in the Breadcrumb stack. You have already configured every page with Breadcrumbs, so use them in your phone view header. |
|
# |
|
# Example render in your phone view header: |
|
# |
|
# `<a href="/"><i class="fa fa-chevron-left"></i></a> This Page Title` |
|
# |
|
# |
|
# You can use it in a view by passing it to the `:builder` option on `render_breadcrumbs`: |
|
# |
|
# <% if request.variant == [:phone] %> |
|
# <%= render_breadcrumbs builder: ::PhoneBreadcrumbsBuilder %> |
|
# <% end %> |
|
# |
|
# You will need to place this class in a location that is loaded by Rails. One |
|
# suggested is `lib/phone_breadcrumbs_builder.rb`. You may need to adjust |
|
# Rails' load path in `config/application.rb`: |
|
# |
|
# config.eager_load_paths << Rails.root.join('lib') |
|
# |
|
# |
|
class PhoneBreadcrumbsBuilder < BreadcrumbsOnRails::Breadcrumbs::Builder |
|
|
|
def render |
|
ret = [] |
|
ret << render_back_element( @elements[-2] ) |
|
ret << render_element( @elements[-1] ) |
|
ret.join(" ") |
|
end |
|
|
|
def render_back_element( element ) |
|
return nil unless element && !element.path.blank? |
|
content = @context.link_to_unless_current( show_arrow_back, compute_path(element), element.options) |
|
if @options[:tag] |
|
@context.content_tag(@options[:tag], content) |
|
else |
|
ERB::Util.h(content) |
|
end |
|
end |
|
|
|
def render_element( element ) |
|
return nil unless element |
|
if element.path == nil |
|
content = compute_name(element) |
|
else |
|
content = @context.link_to_unless_current( compute_name(element), |
|
compute_path(element), |
|
element.options ) |
|
end |
|
if @options[:tag] |
|
@context.content_tag(@options[:tag], content) |
|
else |
|
ERB::Util.h(content) |
|
end |
|
end |
|
|
|
# font-awesome back button |
|
def show_arrow_back |
|
"<i class='fa fa-chevron-left'></i>".html_safe |
|
end |
|
|
|
end |