Code Monkey home page Code Monkey logo

croutons's Introduction

Croutons

Easy breadcrumbs for Rails apps.

Usage

Required steps

  1. Include Croutons::Controller in your ApplicationController.

    This will make a #breadcrumbs helper available in your layouts and views.

  2. Call the #breadcrumbs helper in your layouts or views.

  3. Define a BreadcrumbTrail class, which inherits from Croutons::BreadcrumbTrail.

  4. Define missing methods on the BreadcrumbTrail class.

    For example, for the admin/locations/index.html.erb view you would define an #admin_locations_index method.

    In these methods, you build up a breadcrumb trail by calling #breadcrumb with a label and an optional URL. You can also call previously defined methods to build on existing trails. View assigns (i.e. the controller instance variables) are available via the #objects method which returns a Hash. Rails route helpers are also available inside this class.

Please see the example below for further reference.

Optional steps

  • Instead of defining a BreadcrumbTrail class you can use an object of your own that responds to #breadcrumbs.

    To do this, override the private #breadcrumb_trail method in the controller where you included Croutons::Controller, to return the object you want to use.

    The #breadcrumbs method is passed two parameters: one template_identifier String and one objects Hash. The #breadcrumbs method should return an Array of Croutons::Breadcrumbs.

  • Override the view used to render breadcrumbs.

    To do this, create a view called breadcrumbs/_breadcrumbs.html.erb.

    In this view, an Array of Croutons::Breadcrumbs is assigned to the local variable breadcrumbs. These Croutons::Breadcrumbs have two public attributes: #label and #url. The #url attribute is optional. To check whether the Croutons::Breadcrumb has a #url or not (i.e. should be rendered as a link or not), check whether the #link? method returns true or false.

Example

app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  include Croutons::Controller
end

app/controllers/posts_controller.rb

class PostsController < ApplicationController
  def index
    @posts = Post.all
  end

  def show
    @post = Post.find(params[:id])
  end
end

app/views/layouts/application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title>My blog</title>
  </head>
  <body>
    <%= breadcrumbs %>
    <%= yield %>
  </body>
</html>

app/models/breadcrumb_trail.rb

class BreadcrumbTrail < Croutons::BreadcrumbTrail
  def posts_index
    breadcrumb("Posts", posts_path)
  end

  def posts_show
    posts_index
    breadcrumb(objects[:post].title, post_path(objects[:post]))
  end
end

License

Croutons is Copyright © 2014-2017 Calle Erlandsson, George Brocklehurst, and thoughtbot. It is free software, and may be redistributed under the terms specified in the LICENSE file.

About thoughtbot

thoughtbot

Croutons is maintained and funded by thoughtbot, inc. The names and logos for thoughtbot are trademarks of thoughtbot, inc.

We love open source software! See our other projects. We are available for hire.

croutons's People

Contributors

calleluks avatar mjankowski avatar georgebrock avatar tysongach avatar dependabot[bot] 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.