Giter VIP home page Giter VIP logo

breadcrumb_trail's Introduction

Breadcrumb Trail

Code Climate Build Status Test Coverage

Helps you create a breadcrumb system for your Rails application. Better than any other library, guarenteed*.

Installation

Add this line to your application's Gemfile:

gem 'breadcrumb_trail'

And you're done!

Usage

The gem adds some nice methods to your controller:

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base

  breadcrumb name: "Home", path: :root_path
end
# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
  breadcrumb name: "Articles", path: :articles_path

  def show
    @article = Article.find(params[:id])
    breadcrumb name: @article.name, path: article_path(@article)
    breadcrumbs # => returns all of your breadcrumbs
  end
end
<!-- app/views/layouts/application.html.erb -->
<!-- ... -->
<%= render_breadcrumbs outer: "ul" %>
<!-- ... -->

...all results in (with some assumptions):

<!-- ... -->
<ul>
  <li><a href="/">Home</a></li>
  <li><a href="/articles">Articles</a></li>
  <li><a href="/articles/1">Hello, World</a></li>
</ul>
<!-- ... -->

You can pass #breadcrumb some options, which it'll use as HTML options by default.

Simple, right?

Builders

#render_breadcrumbs takes an option for a builder, or defaults to one if you don't provide it. There are two default builders: HTMLBuilder and BlockBuilder. If you provide a block to #render_breadcrumbs, then BlockBuilder is used; otherwise, HTMLBuilder is used.

HTMLBuilder

HTMLBuilder builds a sensible block of HTML based on some options. The exact options you can provide are:

  • outer: The outer tag that is used. The default for this is ol. If this is nil, then no outer tag is rendered.
  • inner: The inner tag that is used. The default for this is li. If this is nil, then no inner tag is rendered.
  • outer_options: The html attributes that are used for the outer tag. By default, there are no options. If you want to add class="some-class", this is the place to provide it.
  • inner_options: The html attributes that are used for the inner tag. By default, there are no options. If you want to add class="some-class", this is the place to provide it.

That's it!

BlockBuilder

BlockBuilder yields each breadcrumb to the given block. Each breadcrumb has three attributes: name, path, and options. To recreate the default output of HTMLBuilder, you'd have to do this with BlockBuilder:

<!-- ... -->
<ol>
<%= render_breadcrumbs do |breadcrumb| %>
  <li><%= link_to(breadcrumb.name, breadcrumb.path, breadcrumb.options) %></li>
<%= end %>
</ol>
<!-- ... -->

Your Own Builder

You don't have to use one of these. You can use your own builder. However, if you're using a builder because the default builders don't provide a feature you like, open an issue!

Your builder only needs to subclass BreadcrumbTrail::Builder and define the method #call, and that's it! Then, you pass the builder to #render_breadcrumbs with the builder option:

<!-- ... -->
<%= render_breadcrumbs builder: MyCustomBuilder %>
<!-- ... -->

Any options passed to #render_breadcrumbs are passed to your builder's #initialize via the last argument.

Contributing

  1. Fork it (https://github.com/medcat/breadcrumb_trail/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

*: Not guarenteed.

breadcrumb_trail's People

Contributors

teliosdev avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.