Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
`breadcrumbs_on_rails` builder with Bulma compatible output
# `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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment