Skip to content

Instantly share code, notes, and snippets.

@steveclarke
Last active May 27, 2021 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveclarke/ef39f3f0fa8f28ca01e28111a3a64880 to your computer and use it in GitHub Desktop.
Save steveclarke/ef39f3f0fa8f28ca01e28111a3a64880 to your computer and use it in GitHub Desktop.

Custom Builder for Breadcrumbs on Rails with Bootstrap 5:

# app/lib/bootstrap_five_breadcrumbs.rb
class BootstrapFiveBreadcrumbs < BreadcrumbsOnRails::Breadcrumbs::SimpleBuilder
  def render
    return '' if @elements.size == 0

    @options[:outer_tag] ||= :ol
    @options[:tag] ||= :li
    @options[:separator] ||= ""

    @context.content_tag(@options[:outer_tag], class: 'breadcrumb') do
      @elements.collect do |element|
        render_element(element)
      end.join(@options[:separator]).html_safe
    end
  end

  def render_element(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 @elements.last == element
      @context.content_tag(@options[:tag], content, class: "breadcrumb-item active", "aria-current": "page")
    else
      @context.content_tag(@options[:tag], content, class: "breadcrumb-item")
    end
  end
end
# _breadcrumbs.html.erb
<nav aria-label="breadcrumb">
  <%= render_breadcrumbs builder: ::BootstrapFiveBreadcrumbs %>
</nav>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment