Giter Site home page Giter Site logo

little_decorator's Introduction

Gem Version Code Climate Travis CI

LittleDecorator: a rails model decorator. A cute husky with sunglasses proclaims his surprise at how small this gem is.

LittleDecorator is an ultra-lightweight decorator for Rails models. There are only 42 lines of code in lib.

Installation

Include it in your Gemfile.

gem 'little_decorator'

Usage

Create Your Decorator

Add your decorator in app/decorators:

# app/decorators/user_decorator.rb
class UserDecorator < LittleDecorator

  def full_name
    "#{first_name} #{last_name}"
  end

  def updated_at
    record.updated_at.strftime("%A, %B %e, %Y")
  end

end

Method calls are sent to the model via method_missing, so you can call model methods directly as in the full_name method defined above.

Call model methods with record when you want to override a method but still get to the original model.

You can access helper methods and route helpers in your decorators.

Decorate Your Objects

The API consists of a single method: decorate. This method will be available in your controllers and views. You can call decorate on an object or a collection. Examples:

In Controllers

Just call decorate:

decorate(user)

In Views

Just call decorate:

<%= decorate(user) %>

On Collections

Just call decorate. You'll get an array of decorated objects

decorate(users)
<%= decorate(users) %>

Vim Projections

For use with Rails.vim. Place in config/projections.json.

{
  "app/decorators/*_decorator.rb": {
    "command": "decorator",
    "alternate": "spec/decorators/%s_decorator_spec.rb",
    "template": "class %SDecorator < LittleDecorator\nend"
  }
}

Contribute

Pull requests are welcome, but I want to keep this gem simple.

little_decorator's People

Contributors

vicramon avatar rwz avatar

Watchers

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