ActionPresenter

A lightweight presenter implementation for Ruby on Rails.

Requirements

Installation

  1. Add this to your Gemfile: ruby gem 'actionpresenter', '0.2.0'

  2. Create the app/presenters folder in your project which will be holding your presenter files.

Usage

Lets say we have a model named User.

class User < ActiveRecord::Base
  # Model with columns first_name and last_name
  # ...
  def name
    "#{first_name} #{last_name}".strip
  end
end
Creating a Presenter

In our views we always want to display the name of a specific user with a link to his profile. The simplest way of providing similar behavior to all our views is to create a UserPresenter in app/presenters/user_presenter.rb.

class UserPresenter < ActionPresenter::Base
  presents :user

  def name(linked = true)
    h.link_to_if linked, super(), h.user_profile_path(user)
  end
end

We can use super as alternative to user.name or object.name in this case to get the value from the original User instance.

All helpers from the view context are available through the template accessor (or its short variant h).

You can also make all view helpers available to the presenter as if they were mixed into the presenter by including ActionPresenter::TemplateDelegation:

class UserPresenter < ActionPresenter::Base
  include ActionPresenter::TemplateDelegation

  presents :user

  def name(linked = true)
    link_to_if linked, super(), user_profile_path(user)
  end
end
Using Presenters in a View

A presenter can be embedded into a view by using the present helper.

<% present @user do |user| %>
  <strong>Name:</strong>
  <br>
  <%= user.name %>
<% end %>

You can also use reference to any custom presenter:

<% present @user, with: Admin::UserPresenter do |user| %>
  ...
<% end %>

Alternatively, you can use:

<% present [:admin, @user] do |user| %>
  ...
<% end %>

If you have a collection of objects you can rely on present_collection which will wrap each element of the collection with its own presenter:

<% present_collection(@users).each do |user| %>
  ...
<% end %>

The following snippet will search for matching presenter classes within the Admin module:

<% present_collection(@users, scope: :admin).each do |user| %>
  ...
<% end %>
Keeping it DRY

To share common functionality between different presenters we encourage you to use presenter concerns. Latter should be put into app/presenters/concerns. Note: As this folder is not recognized by the default Rails setup, it has to be added manually to config/application.rb:

config.autoload_paths << Rails.root.join('app', 'presenters', 'concerns').to_s

Contributing to ActionPresenter

Copyright © 2015 Tobias Casper. See LICENSE.txt for further details.