Ruby Rails

Rails Presenter

Presenter Classes

Hero title.min Hero title lg.min
Hero linework.min

It's time to beautify your Rails views

Hero linework.min

MetaPresenter is a 100% free Ruby gem for view presenter classes.

MetaPresenter is a 100% free Ruby gem for view presenter classes. Pull yourself out of the vortex of scattered logic.

To install just add to your Gemfile and bundle:

gem 'meta_presenter'
$ bundle install
Fold slash white.min

Learn More

Hero line base.min Line base hero lg bottom.min

MetaPresenter

Linework.min

How does it work?

Linework.min

Each action gets its own Presenter class that the view calls with presenter.method_name . Presenters can also delegate to objects or controller methods.

Diagonal slice rectangle.min
Red three dots.min

Decompose your view logic into tight, easily-testable classes

In 4 simple steps

Overlay shape large.min
Overlay shape.min
01
Line.min

Include in your controller

Including MetaPresenter::Helpers gives you access to the presenter method within your views.

# app/presenters/application_presenter.rb
class ApplicationController < ActionController::Base
  include MetaPresenter::Helpers
end

# Works with mailers, too!
class ApplicationMailer < ActionMailer::Base
  include MetaPresenter::Helpers
end
02
Line.min

Create a presenter class

Your presenter class is scoped to an individual view. No more chaos of global helper methods!

# app/presenters/pages/home_presenter.rb
class Pages::HomePresenter < PagesPresenter
  # presenter.email, presenter.last_login_at or any
  # other method not already defined will delegate
  # to the current_user
  delegate_all_to :current_user

  # presenter.greeting in views
  def greeting
    "Hello, #{current_user.name}"
  end
end
03
Line.min

Write clean views with the presenter object

Now you can use presenter.whatever in your views and it will delegate to the presenter.

<h1>Home</h1>
<p><%= presenter.greeting %></p>
<p>Last login <%= presenter.last_login_at %></p>
04
Line.min

Test your presenter class

You can test presenters just like any other class. Don't be afraid to test every permutation because unlike system specs they run blazing fast!

describe Pages::HomePresenter do
  let(:user) { User.new(name: "Mario") }
  let(:controller) do
    OpenStruct.new(current_user: user)
  end
  let(:presenter) do
    described_class.new(controller)
  end
  describe '#hello' do
    subject { presenter.hello }
    it { is_expected.to eql("Hello, Mario") }
  end
end
Overlay shape bottom.min Side lines mobile.min Side lines.min
Red three dots.min

Are you ready to

Give it a try?

Line bottom.min
Line bottom.min

If you're ready to start beautifying your views, visit the GitHub page for setup instructions.