|
# `BulmaBreadcrumbsBuilder `is a Bulma compatible breadcrumb |
|
# builder. It is designed to work with the `breadcrumbs_on_rails` gem as a |
|
# drop-in builder replacement. |
|
# |
|
# BulmaBreadcrumbsBuilder accepts a limited set of options: |
|
# |
|
# | option | default | description | |
|
# | ---------------- | ------- | ------------------------------------------ | |
|
# | `:container_tag` | `:ol` | What tag to use for the list container | |
|
# | `:tag` | `:li` | What HTML tag to use for breadcrumb items | |
|
# | `:show_empty` | `false` | Whether to render container when no crumbs | |
|
# |
|
# You can use it by passing it to the `:builder` option on `render_breadcrumbs`: |
|
# |
|
# <nav class="breadcrumb" aria-label="breadcrumbs"> |
|
# <%= render_breadcrumbs builder: ::BulmaBreadcrumbsBuilder %> |
|
# </nav> |
|
# |
|
# You will need to place this class in a location that is loaded by Rails. One |
|
# suggested is `lib/bulma_breadcrumbs_builder.rb`. You may need to adjust |
|
# Rails' load path in `config/application.rb`: |
|
# |
|
# config.eager_load_paths << Rails.root.join('lib') |
|
# |
|
# |
|
# See also: |
|
# <http://bulma.io/documentation/components/breadcrumb/> |
|
# |
|
# Based on: |
|
# - BreadcrumbsOnRails::Breadcrumbs::SimpleBuilder |
|
# <https://github.com/weppos/breadcrumbs_on_rails/blob/v3.0.1/lib/breadcrumbs_on_rails/breadcrumbs.rb#L79> |
|
# - BootstrapBreadcrumbsBuilder |
|
# <https://gist.github.com/riyad/1933884> |
|
# - Bootstrap4BreadcrumbsBuilder |
|
# <https://gist.github.com/SaladFork/270a4cb3ac20be9715b7117551c31ec7> |
|
# |
|
class BulmaBreadcrumbsBuilder < BreadcrumbsOnRails::Breadcrumbs::Builder |
|
def render |
|
return '' unless should_render? |
|
|
|
container_tag = @options.fetch(:container_tag, :ul) |
|
|
|
@context.content_tag container_tag, class: nil do |
|
@elements.collect do |element| |
|
render_element(element) |
|
end.join.html_safe |
|
end |
|
end |
|
|
|
def render_element(element) |
|
name = compute_name(element) |
|
path = compute_path(element) |
|
|
|
current = @context.current_page?(path) |
|
|
|
item_tag = @options.fetch(:tag, :li) |
|
|
|
@context.content_tag(item_tag, class: [('is-active' if current)]) do |
|
@context.link_to(name, path, element.options) |
|
end |
|
end |
|
|
|
private |
|
|
|
def should_render? |
|
@elements.any? || @options[:show_empty] |
|
end |
|
end |