Ruby Rails
Rails Presenter
Presenter Classes
![Hero title.min](/images/hero-title.min-e1dcfd04.png)
![Hero title lg.min](/images/hero-title-lg.min-414c6d26.png)
![Hero linework.min](/images/hero-linework.min-f1c5ffa6.png)
It's time to beautify your Rails views
![Hero linework.min](/images/hero-linework.min-f1c5ffa6.png)
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](/images/fold-slash-white.min-80e77182.png)
Learn More
![Hero line base.min](/images/hero-line-base.min-5c78c366.png)
![Line base hero lg bottom.min](/images/line-base-hero-lg-bottom.min-b2576ea6.png)
MetaPresenter
![Linework.min](/images/linework.min-37c11ee4.png)
How does it work?
![Linework.min](/images/linework.min-37c11ee4.png)
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](/images/diagonal-slice-rectangle.min-27c80ab0.png)
![Red three dots.min](/images/red-three-dots.min-7498bd0b.png)
Decompose your view logic into tight, easily-testable classes
In 4 simple steps
![Overlay shape large.min](/images/overlay-shape-large.min-d57d1356.png)
![Overlay shape.min](/images/overlay-shape.min-fde3bee4.png)
![Line.min](/images/line.min-54d0f4bd.png)
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
![Line.min](/images/line.min-54d0f4bd.png)
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
![Line.min](/images/line.min-54d0f4bd.png)
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>
![Line.min](/images/line.min-54d0f4bd.png)
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](/images/overlay-shape-bottom.min-a6559a70.png)
![Side lines mobile.min](/images/side-lines-mobile.min-d437edf8.png)
![Side lines.min](/images/side-lines.min-278fca14.png)
![Red three dots.min](/images/red-three-dots.min-7498bd0b.png)
Are you ready to
Give it a try?
![Line bottom.min](/images/line-bottom.min-2253d843.png)
![Line bottom.min](/images/line-bottom.min-2253d843.png)
If you're ready to start beautifying your views, visit the GitHub page for setup instructions.