Skip to content

Instantly share code, notes, and snippets.

@steveklabnik
Created October 9, 2012 06:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveklabnik/3856921 to your computer and use it in GitHub Desktop.
Save steveklabnik/3856921 to your computer and use it in GitHub Desktop.
Presenters in Rails?
<article>
<header>
<h3><%= comment.author %></h3>
</header>
<%= comment.body %>
</article>
class ArticlesController < ApplicationController
def show
article = Article.find(params[:id])
comments = article.comments
@nope = "lol"
render ArticleView.new(article, comments)
end
def render(*args)
if args.length == 1
@presenter = args.first
else
super
end
end
def view_assigns
{:presenter => @presenter }
end
end
<article>
<header>
<h1><%= @presenter.article.title %></h1>
<h2><%= @presenter.article.author %></h2>
</header>
<p><%= @presenter.article.body %></p>
<section>
<h2>Comments</h2>
<!-- rendering collections is preserved -->
<%= render @presenter.comments %>
</section>
</article>
<!-- this was not mutated -->
<h1><%= @presenter.article.title %></h1>
<h2><%= @presenter.article.author %></h2>
<!-- the only thing we expose is @presenter -->
<!-- this will not output anything; the ivar is just nil -->
<%= @nope %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment