Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
An implementation of presenter pattern used in Ruby projects
tree: 4029667aad

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec/support/macros
tasks
test
.gitignore
.rvmrc.template
.travis.yml
Gemfile
Guardfile
LICENSE
README.md
Rakefile
model_presenter.gemspec

README.md

ModelPresenter

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

Installation

Add this line to your application's Gemfile:

gem 'model_presenter'

And then execute:

$ bundle

Or install it yourself as:

$ gem install model_presenter

Usage

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}"
  end

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

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 presenter_object.property

An example output of as_json returns:

{
  name: "John Smith",
  gender: "Male",
  email: "jsmith@example.com"
}

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

A presenter instance can be initialized with

user = User.new(user_model)
user.to_json

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.

Rspec Macros

The gem provides some rspec macros for speeding up your test effort for your presenters. To use it, in your spec_helper.rb among your other setup:

require 'model_presenter/spec_support'
RSpec.configure do |config|
  ModelPresenter::SpecSupport.new(config).register
end

And in a presenter test, you can do:

describe User do
  forward_from_model_attributes :first_name, :last_name, :email
  as_json_attributes :first_name, :gender
end

The forward_from_model_attributes macro will generate the following tests:


User
  #first_name
    returns the model.first_name
  #last_name
    returns the model.last_name
  #email
    returns the model.email

and as_json_attributes macro will generate the following tests:


  #as_json
    has the key first_name with value set to presenter.first_name
    has the key gender with value set to presenter.gender

Contributing

  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
Something went wrong with that request. Please try again.