Skip to content

Instantly share code, notes, and snippets.

@mmacedo
Created April 12, 2012 19:22
Show Gist options
  • Save mmacedo/2370308 to your computer and use it in GitHub Desktop.
Save mmacedo/2370308 to your computer and use it in GitHub Desktop.
helper vs loop
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
-# 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