No commit activity in last 3 years
No release in over 3 years
Useful for wrapping model objects with presentation methods.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.12
~> 10.0
~> 3.5
 Project Readme

PresenterObject

This is a simple presenter base class you can use to create model specific presenters.

Installation

Add this line to your application's Gemfile:

gem 'presenter_object'

And then execute:

$ bundle

Or install it yourself as:

$ gem install presenter_object

Usage

Given a model class:

class User < ActiveRecord::Base
   validates :first_name, presence: true # just to show one of the attributes of this model
end

Create a subclass such as:

class UserPresenter < PresenterObject::Base
  presents :user

  def formatted_phone_number
    I18n.l phone_number, format: :fancy
  end  
end

Then wrap a model instance:

@user = UserPresenter.new User.find(params[:id])

The @user presenter object can be used as if it were the model object. Then, just place all presentation methods in the presenter class and call them from the views.

@user.class # => User
@user.respond_to? :first_name # => true
@user.formatted_phone_number # => "(555) 444-3322"

Collection Presenter

You can also wrap whole collections of objects at once. Create a collection presenter:

class UserCollectionPresenter < PresenterObject::Collection
  def render_table
    each do |object|
      # object is now wrapped in its presenter
    end
  end
end

Instantiate the collection presenter by passing in an object collection and an optional view_context:

@user_collection = UserCollectionPresenter.new User.all, view_context
@user_collection.render_table

The @user_collection instance is an Enumerable and will lazily wrap each object when you iterate over the collection.

Development

After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/DiegoSalazar/presenter_object.