deckorator Build Status

Deckorator is a PORO (plain old Ruby object) implementation of the Decorator pattern. It can be easily integrated into Rails/Sinatra apps or any other Ruby project.

Installation

gem 'deckorator'

Then run bundler

$ bundle

Or, install it yourself as

$ gem install deckorator

With Rails

Include Deckorator in the application controller

class ApplicationController < ActionController::Base
  include Deckorator
end

Then, run the install generator

$ rails g deckorator:install

An application decorator will be placed in app/decorators.

Usage

Using the decorate method in the controller

class UsersController < ApplicationController
  before_action :set_user

  def show
    @user = decorate(@user)
  end

  private

  def set_user
    @user = User.find(params[:id])
  end
end

Example decorator

class UserDecorator < ApplicationDecorator

  def full_name
    if first_name.blank? && last_name.blank?
      'Unnamed User'
    else
      "#{first_name} #{last_name}".strip
    end
  end
end

There's a generator

$ rails g deckorator:decorator user

This will create a UserDecorator in the app/decorators directory while also generating a stubbed test.

Also using Pundit?

You might want to add this to your app/decorators/ApplicationDecorator:

def self.policy_class
  "#{decorated_object_class}Policy"
end

License

MIT

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

Crafted with <3 by John Otander and Jake Mays.