Git Product home page Git Product logo

migajas's Introduction

Migajas

Migajas is a tiny library for adding breadcrumbs to any Rack application. It was designed with routing tree frameworks like Cuba or Roda in mind, but works well with any Rack-based framework, including Rails.

Usage

First, build up your trail of crumbs as you match your routes:

Cuba.plugin Migajas

Cuba.define do
  breacrumbs << "Home"

  on "users" do
    breadcrumbs << "Users"

    on get do
      # render the list of users
    end

    on ":id" do |id|
      user = User[id]
      breadcrumbs << user.name

      on get do
        # render the user profile page
      end
    end
  end
end

Then, in the view, go over the breadcrumbs list:

<ol class="breadcrumbs">
  <% breadcrumbs.each do |crumb| %>
    <li class="<%= "active" if crumb.current? %>">
      <a href="<%= crumb.url %>"><%= crumb.name %></a>
    </li>
  <% end %>
</ol>

That's it :)

Rails

Start by adding Migajas::Rails to your controllers:

class ApplicationController < ActionController::Base
  include Migajas::Rails
end

This gives you a breadcrumbs method that you can access from your controllers and views. The recommended approach is to use action callbacks to add breadcrumbs:

class ApplicationController < ActionController::Base
  include Migajas::Rails

  before_action { breadcrumbs.add "Home", root_path }
end

class UsersController < ApplicationController
  before_action { breadcrumbs.add "Users", users_path }
  before_action(only: [:show, :edit, :update]) { breadcrumbs.add @user.name, user_path(@user) }
end

Then, in your layout, you can iterate over the Array:

<ol class="breadcrumbs">
  <% breadcrumbs.each do |crumb| %>
    <%= tag.li class: { active: crumb.current? } do %>
      <%= link_to crumb.name, crumb.url %>
    <% end %>
  <% end %>
</ol>

Install

gem install migajas

Do we need a gem for this?

Probably not, but after 2 years of using this code, I got bored of copying and pasting it from a gist.

What's in a name?

Migajas is the Spanish word for breadcrumbs. It's pronounced like you'd pronounce me-gha-has in English (me as in the word "me", gha as in "ghast", and has as the word "has".)

License

This project is shared under the MIT license. See the attached LICENSE file for details.

migajas's People

Contributors

foca avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.