Code Monkey home page Code Monkey logo

burgundy's Introduction

Burgundy

Travis-CI Code Climate Test Coverage Gem Gem

A simple wrapper for objects (think of Burgundy as a decorator/presenter) in less than 150 lines.

Installation

Add this line to your application's Gemfile:

gem 'burgundy'

And then execute:

$ bundle

Or install it yourself as:

$ gem install burgundy

Usage

First, define your wrapping class.

class UserPresenter < Burgundy::Item
end

Then you can instantiate it:

user = UserPresenter.new(User.first)

The Burgundy::Item has access to helper and route methods. Notice that the wrapper item is accessible through the Burgundy::Item#item method.

class UserPresenter < Burgundy::Item
  def profile_url
    routes.profile_url(item.username)
  end
end

You don't have to expose attributes; everything is delegated to the wrapped item.

To wrap an entire collection, just use the Burgundy::Collection class.

class WorkshopsController < ApplicationController
  def index
    @workshops = Burgundy::Collection.new(
      Workshop.sorted_by_name,
      WorkshopPresenter
    )
  end
end

or just call WorkshopPresenter.wrap(Workshop.sorted_by_name). Both ways return a Burgundy::Collection instance.

You may need to provide additional arguments to the item class. On your collection, all additional arguments will be delegated to the item classe, like the following example:

WorkshopPresenter.wrap(Workshop.all, current_user)
Burgundy::Collection.new(Workshop.all, WorkshopPresenter, current_user)

class WorkshopPresenter < Burgundy::Item
  def initialize(workshop, current_user)
    super(workshop)
    @current_user = current_user
  end
end

The query will be performed only when needed, usually on the view (easier to cache). The collection is an enumerable object and can be passed directly to the render method. Each item will be wrapped by the provided class.

<%= render @workshops %>

Route URLs may require the default url options. Burgundy try to get them from the following objects:

  • Rails.configuration.action_mailer.default_url_options
  • Rails.application.routes.default_url_options

So you can just put this on your environment file

config.action_controller.default_url_options = {
  host: "example.org"
}

You can map attributes into a hash; I use this strategy for using presenters on API responses (so I can skip adding yet another dependency to my project).

class UserPresenter < Burgundy::Item
  attributes :username, :name, :email

  def profile_url
    routes.profile_url(item.username)
  end
end

UserPresenter.new(User.first).attributes
#=> {:username=>'johndoe', :name=>'John Doe', :email=>'[email protected]'}

UserPresenter.new(User.first).to_hash
#=> {:username=>'johndoe', :name=>'John Doe', :email=>'[email protected]'}

UserPresenter.new(User.first).to_h
#=> {:username=>'johndoe', :name=>'John Doe', :email=>'[email protected]'}

If you want to remap an attribute, provide a hash.

class UserPresenter < Burgundy::Item
  attributes :name, :email, :username => :login

  def profile_url
    routes.profile_url(item.username)
  end
end

UserPresenter.new(User.first).attributes
#=> {:login=>'johndoe', :name=>'John Doe', :email=>'[email protected]'}

Contributing

  1. Fork it
  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 new Pull Request

burgundy's People

Contributors

fnando avatar lsdr avatar josegm 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.