Design Pattern: Presenter
23 Jun 2023Philosophy
- Views are for presentation.
- There should be no
ActiveRecord
queries in views. - Most logic (
if
statements) should be excluded from views.
Why Not Helpers?
Helpers are better to use if you have a global formatting method that you re-use in different views.
Things like:
- rendering markdown
- showing dates in a specific format
- removing specific words from text
Helpers are not great to overuse because they lack organization and are difficult to reuse across your app.
Using Presenter Objects
- create
app/presenters
. - name after model,
app/presenters/post_presenter.rb
.
class PostPresenter
def initialize(post)
@post = post
end
def title_without_forbidden_words
@post.title.gsub("forbidden word", "")
end
def css_color
@post.draft? ? "orange" : "green"
end
end
<% presenter = PostPresenter.new(post) %>
<p>
Post title: <%= presenter.title_without_forbidden_words %>
<%= link_to "Read post", post, class: "w-75 p-3 text-#{presenter.css_color} border-#{presenter.css_color}" %>
</p>
This accomplishes a few things:
- it removes logic from views
- creates meaningful names for methods and logic
- allows logic to more easily be reused in other views and mailers