Created
January 21, 2011 20:28
-
-
Save Gregg/790356 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Lets talk about helpers! See once again your main twitter page | |
# <div class="followers"> | |
# Followers | |
# <span><%= @followers_count %></span> | |
# <% @recent_followers.each do |f| %> | |
# <a href="<%= user_path(f) %>"> | |
# <img src="<%= f.avatar.url(:thumb) %>" /> | |
# </a> | |
# <% end %> | |
# </div> | |
# | |
# <div class="following"> | |
# Following | |
# <span><%= @following_count %></span> | |
# <% @recent_following.each do |f| %> | |
# <a href="<%= user_path(f) %>"> | |
# <img src="<%= f.avatar.url(:thumb) %>" /> | |
# </a> | |
# <% end %> | |
# </div> | |
# Not only are each of these cluttered, but they're similar.. Helpers to the rescue: | |
# <%= follow_box("Followers", @followers_count, @recent_followers) %> | |
# <%= follow_box("Following", @following_count, @recent_following) %> | |
# What does that follow_box code look like? | |
def follow_box(title, count, recent) | |
str = "<div class=\"#{title.downcase}\">" + | |
"#{title}<span>#{count}</span>" | |
recent.each do |f| | |
str += "<a href=\"#{user_path(f)}\">" | |
str += "<img src=\"#{f.avatar.url(:thumb)}\">" | |
str += "</a>" | |
end | |
raw(str += "</div>") | |
end | |
# Buncha stuff wrong with this... first of all .. us a proper link and image_tag block syntax | |
def follow_box(title, count, recent) | |
str = "<div class=\"#{title.downcase}\">" + | |
"#{title}<span>#{count}</span>" | |
recent.each do |user| | |
str += link_to user do | |
image_tag(user.avatar.url(:thumb)) | |
end | |
end | |
raw(str += "</div>") | |
end | |
# next up, use a content_tag so you don't have messy html | |
def follow_box(title, count, recent) | |
content_tag :div, :class => title.downcase do | |
str = title + content_tag(:span, count) | |
recent.each do |user| | |
str += link_to user do | |
image_tag(user.avatar.url(:thumb)) | |
end | |
end | |
raw(str) | |
end | |
end | |
# Now we can get rid of the string! w00t. | |
def follow_box(title, count, recent) | |
content_tag :div, :class => title.downcase do | |
raw( | |
title + | |
content_tag(:span, count) + | |
recent.collect do |user| | |
link_to user do | |
image_tag(user.avatar.url(:thumb)) | |
end | |
end.join | |
) | |
end | |
end | |
# Helper win! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment