Created
October 9, 2012 06:05
-
-
Save steveklabnik/3856921 to your computer and use it in GitHub Desktop.
Presenters in Rails?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<article> | |
<header> | |
<h3><%= comment.author %></h3> | |
</header> | |
<%= comment.body %> | |
</article> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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