Skip to content

Instantly share code, notes, and snippets.

@Gregg
Created January 21, 2011 21:59
Show Gist options
  • Save Gregg/790508 to your computer and use it in GitHub Desktop.
Save Gregg/790508 to your computer and use it in GitHub Desktop.
# So you might have something like this:
# <% @presenter.tweets.each do |tweet| %>
# <div id="tweet_#{tweet.id}" class="<%= 'favorite' if current_user && tweet.is_a_favorite?(current_user) %>">
# <%= tweet.status %>
# </div>
# <% end %>
# Kinda ugly... this is a simple example of where you might want a block helper so our view becomes:
# <% tweet_div(tweet) do %>
# <%= tweet.status %>
# <% end %>
# then we have a helper
def tweet_div(tweet, &block)
klass = 'favorite' if current_user && tweet.is_a_favorite?(current_user)
div_for tweet, :class => klass do
yield
end
end
# if we wanted to make it even more testable, we might do
def tweet_div(tweet, &block)
div_for tweet, :class => tweet_class(tweet) do
yield
end
end
def tweet_class(tweet)
'favorite' if current_user && tweet.is_a_favorite?(current_user)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment