README.md

ModelPresenter

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

What is a presenter

A presenter is a bridge between a business model and how it will be presented (as json, as HTML, as text, as PDF, etc.). A presenter covers logic that is necessary for presenting a business model, but such logic should not be put into the business model because it doesn't belong to.

For example, what attributes are going to be included in the JSON return is not a business model's concern. Another example, If the front end needs to have a person's gender in a humanly readable way (such as "Male" and "Female"), it is not the business model's concern. The business model knows it's gender in some way, it might choose to represent it as a boolean model.male? and model.female?. It is the responsibility of a presenter to provide what the front end needs. The Presenter may return the string Male or Female in its gender method.

What is not a presenter

A Presenter is not a place for generating HTML tags and code segments. An HTML file is where you write HTML code in.

The forward_from_model defines methods attr1 and attr2 which calls the model.attr1model.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.

Moneyize

The presenter comes with a small helper for format the money. The usage:

has_many

The macro will generates a groups methods, which will return an array. Each element of the array is an instance of Presenters::Group whose model is one of the group models that the user has.

Convention for Using with Rails

In a Rails controller, I always initialize one and only one instance variable for using in the view - an instance of a Presenter class. The presenter has all necessary logic to make the view as dumb as possible. And all the logic can be unit tested just like any PORO, make your testing effort easy and enjoyable.

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