View models

Rails Presenters with a little bit of decoration - add an R to your MVC

View the Project on GitHub beatrichartz/view_models

View Models Build Status CodeClimate

A representer solution for Rails 3 and 4 inspired by Florian Hankes View Models for Rails 2 ( For older versions of Rails please use versions up to 3. You may find view models useful (or not). Fact: They will keep code out of your views. Because view models do not like code in your view.

Installing View Models

Add this to your gemfile

gem "view_models",   "~> 4.0"

And this to your application.rb: (It adds the app folder to the autoload paths, which is necessary for the view models to load)

config.autoload_paths += %W(#{config.root}/app)

Create a directory "view_models" in your app folder, and you're good to go.

mkdir ./app/view_models


What you can look forward to in future versions of this gem:

Basic Usage

Let's say you have a model User (with a first and a last name and an address in the database):

class User 

Write a corresponding view model user.rb in your view_models folder

class ViewModels::User 

In your view, call the view_model for a model by using «view_model_for»

- view_model = view_model_for(@user)
%h2= view_model.creation_time

All beautiful, you may think, but...

Why View Models? (aka «The Problem»)

Ever felt like putting something like this in your views (example in haml)?

  .name= [@user.first_name, @user.last_name].join ' '
  .address= [@user.street, @user.street_number, @user.zip_code,].join ' '

Well, that may feel good if you're in a hurry. Soon there comes the time when you use this code in more than one place. Time to build a helper:

module UserHelper
  def user_name user
    [user.first_name, user.last_name].join ' '

  def user_address user
    [user.street, user.street_number, user.zip_code,].join ' '

  .name= user_name @user
  .address= user_address @user

It may be a lot cleaner, but something just does not feel right. Right, but what? Well, for instance, you have to namespace all your methods with «user» so your methods don't get messy. Second, you have to include the helper either in every context you use it, or even in the whole app. If only you had a polymorphic object to represent your models in the views...

Meet View Models (aka «The Solution»)

View models are pretty, because they represent your models in your views. They look good in every context, and therefore make you look good, too. Take our example from before:

class ViewModels::User 

And your view will look like this:

- user_view_model = view_model_for(@user)
  .address= user_view_model.address

No helper inclusion needed, no further noise involved. In fact, you can make it even prettier: What if you needed that partial with the user name and address somewhere else in your code?

Meet View Models render_as Method

View models feature template rendering: You can render any partial in your views, following your views directory tree structure: A view_model variable will automatically be included as a local variable in your partial if you render it with view models:

Let's say you have the user model from before, and the following partial written for the view model to render named "_info":

  .address= view_model.address

Now everything you have to do to render that partial is:

= view_model_for(@user).render_as :info

View models feature hierarchical template rendering. That means, if you have a parent view model which has an identical partial already installed, you do not need to copy identical code just to render the same template. The view model will lookup its inheritance chain and Rails template paths to find the suitable partial. Which brings us to another great feature:

View Models Can Haz Inheritance

Ever tried to do inheritance in Rails helpers? Right. View Models, on the other side, love inheritance. They do not need arguments to display a formatted creation time for all your models. Consider the following:

The View Model Way

You can generate a view model tree structure with a view model for your whole app for your other view models to inherit from. The polymorphic class matching of the view models ignores the missing class YourApp candidly.

class ViewModels::YourApp 

In your view:

= view_model_for(@user).creation_time

The Helper Way

Imagine how to do this in a helper ? Well, better go ahead and use time_ago_in_words everywhere.

Testing View Models

Testing View Models is easy. You can use the view models initializer instead of the view_model_for mapping (example in rspec with factory girl):

describe "ViewModels::User" do
  let(:user) { build_stubbed(:user) }
  subject {, @controller) } # @controller may be nil or the controller
  describe "model_readers"
    .. there you go

Further Reads

RubyGems Bug Tracker Source