This project is archived and is in readonly mode.
value_changed? helper method
-
Ian Terrell
Are there other use cases?
Do you see this as much better than using Enumerable#group_by? (http://api.rubyonrails.org/class...)
<% @people.group_by(&:occupation).each do |occupation, peeps| %> <h2><%= occupation %></h2> <% peeps.each do |person| %> <%= person.name %> <% end %> <% end %> -
Jason Arora
Thanks for posting, Ian. You are correct, Enumerable#group_by is the best way to handle this pattern when ordering by a model attribute.
However, the value_changed? method works really well for dynamic cases, such as:
<% @people.each do |person| %> <%= "<h2>#{person.name.first}</h2>" if value_changed?(person.name.first) %> <%= person.name %><br/> <% end %> # => <h2>A</h2> # => Alan # => Amy # => <h2>B</h2> # => Barry # => ...Is this more acceptable? Thanks again for the feedback.
Regards, Jason
-
Ian Terrell
For this use case, Enumerable#group_by still seems best. The &:symbol syntax is shorthand for a block; you can spell it out for cases where you wish to group by something that isn't a method:
<% @people.group_by{|person|person.name.first}.each do |initial, peeps| %> <h2><%= initial %></h2> <% peeps.each do |person| %> <%= person.name %> <% end %> <% end %> -
Jeremy Kemper
- State changed from new to wontfix
Nice patch, but agreed that group_by is a better choice for the problem it's solving
-
Jason Arora
Duh. :-P
You're totally right, Ian. Thanks!
