Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tag: v0.0.1
Fetching contributors…

Cannot retrieve contributors at this time

80 lines (50 sloc) 2.308 kB


Build Status

ModelPresenter provides basic framework in creating a presenter. The goal of the gem is that the presenter can be used in any Ruby projects that feel it needs to use this pattern, not limited to Rails projects.

The presenter puts more emphasis on JSON representation of a business model object. But it can be used in classic HTML view just as easy.

The gem is not trying to provide a automagic way to look cool. Rather, it focuses on explicitly expressing the intent of a Presenter class. Please read the usage for the details


Add this line to your application's Gemfile:

gem 'model_presenter'

And then execute:

$ bundle

Or install it yourself as:

$ gem install model_presenter


An example presenter that uses the ModelPresenter::Base

class User
  include ModelPresenter::Base
  forward_from_model :last_name, :first_name, :email
  json_properties :name, :gender, :email

  def name
    "#{first_name} #{last_name}"

  def gender
    model.gender == "M" ? "Male" : "Female"

The forward_from_model defines methods attr1 and attr2 which calls the model.attr1 model.attr2 respectively.

The json_properties defines methods as_json which returns a hash in which the keys are the properties being passed in the json_properties and the value of a given property the

An example output of as_json returns:

  name: "John Smith",
  gender: "Male",
  email: ""

Then one can use presenter_object.to_json to serilize it into a JSON string.

A presenter instance can be initialized with

user =

It always takes a model object as the only argument in the initializer. The model object is referred from within the presenter as presenter.model. It is a private attribute reader.


  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
Jump to Line
Something went wrong with that request. Please try again.