Created
April 12, 2012 19:22
-
-
Save mmacedo/2370308 to your computer and use it in GitHub Desktop.
helper vs loop
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
module ApplicationHelper | |
def grid_for(collection, options={}, &block) | |
options[:columns] ||= 4 | |
options[:class] ||= '' | |
content_tag :table, :class => options[:class] do | |
content_tag :tbody do | |
collection.to_a.in_groups_of(options[:columns]).collect {|row| | |
content_tag :tr do | |
row.collect{|col| content_tag :td, (capture(col, &block) unless col.nil?) }.reduce(:+) | |
end | |
}.reduce(:+) | |
end | |
end | |
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
-# dummy css | |
:css | |
td { width:200px; } | |
-# helper | |
= grid_for @people, :columns => 4 do |person| | |
= link_to person.display_name, person | |
%br | |
person.birth_date | |
%br | |
= link_to 'Edit', edit_person_path(person) | |
%br | |
= link_to 'Delete', person, :method => :delete | |
-# loop | |
%table | |
%tbody | |
- @people.to_a.in_groups_of 4 do |people_row| | |
%tr | |
- people_row.each do |person| | |
- unless person.nil? | |
%td | |
= link_to person.display_name, person | |
%br | |
person.birth_date | |
%br | |
= link_to 'Edit', edit_person_path(person) | |
%br | |
= link_to 'Delete', person, :method => :delete |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment