Fetching contributors…
Cannot retrieve contributors at this time
114 lines (76 sloc) 2.91 KB


Build Status

DelegatedPresenter gives you an easy way to present objects and collections to your models. It uses ruby's Delegate model so it is lightweight and functional!


Add this line to your application's Gemfile:

gem 'delegated_presenter'

And then execute:

$ bundle

Or install it yourself as:

$ gem install delegated_presenter


Generate a presenter using a rails generator:

$ rails g delegated_presenter contact

Add some functionality to your presenter

class ContactPresenter < DelegatedPresenter::Base
  # By default this presenter will try and present a Contact if it exists.
  # You can explicitly tell the presenter to present other models using the following syntax:

  presents OtherContact, SomeOtherModel

  # Add some functionality to your presenter!
  # The presenter will always look to the model it is presenting for methods and attributes not defined in the presenter.
  # If you want to override model method, you can always call `presented_model.{method_name}` to access the original method.

  def middle_initial
    "#{middle_name.first}." if middle_name

  def full_name
    [prefix, first_name, middle_initial, last_name, suffix].compact.join(' ')

Hide or expose methods from the presented model:

There are two helper methods in the presenter.

  • expose: :method_name, :another_method_name will hide all methods except the ones you specify.
  • hide: :method_name, :another_method_name will only hide the methods you specify.
class ContactPresenter < DelegatedPresenter::Base
  hide :id, :crypted_password


class ContactPresenter < DelegatedPresenter::Base
  expose :first_name, :last_name

Use the controller helper

See: {DelegatedPresenter::PresentsBeforeRendering}

Use the following to present a model instance or collection with a presenter, by default it will try and use a presenter with the same class as the instance or collection.

class ContactsController < ApplicationController
  presents :contact

If you for any reason need to explicitly define the presenter you may define a with: :presenter_name option, like so.

class UsersController < ApplicationController
  presents :user, with: :contact_presenter

Using the Presenter without the controller helper:

class ContactsController < ApplicationController
  def index
    @contacts = Contact.all

  def show
    @contact = Contact.find(params[:id])


  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