Forum: Ruby on Rails HOW TO: Easily output alternating item styles via metaprogra

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
181976602397443ce119909b9d82cae2?d=identicon&s=25 blinking bear (Guest)
on 2007-07-03 08:43
(Received via mailing list)
I always need to apply alternating item styles when outputting a
collection,
here's what I've been using recently.

In the app helper I define this method:

  def alternate(items,alt=false
    items.map{|i| i.class}.each { |k| k.class_eval {attr_accessor :alt}}
    items.each { |i| i.alt, alt = alt, !alt }
  end

When I call the partial I wrap the collection with this method:

<%= render :partial => 'story', :collection => alternate(@stories) %>

Now in the partial each object in the collection has the alt attribute
available:

<li class="<%= story.alt ? "alt" : "" %>">


Obviously I'm making the assumption that the objects don't already
have/use
an alt attribute.  Anyone solving this similarly?

- Nathan
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-07-03 08:53
(Received via mailing list)
On 7/3/07, blinking bear <blinkingbear@gmail.com> wrote:
>
> Obviously I'm making the assumption that the objects don't already
> have/use an alt attribute.  Anyone solving this similarly?
>
> - Nathan


To be honest I'm not really following your code.

items.each { |i| i.alt, alt = alt, !alt }  What is this doing?

I must be missing something because it seems that every call to alt
should
return false from the setup in the alternate method.

My mind went to the cycle method, but I'm not sure if that fits or not
since
I don't follow your code.

Anyway the docs are
http://api.rubyonrails.org/classes/ActionView/Help...

Cheers
Daniel
4c3acdc3d93f54cc7a7281780ec8a4ee?d=identicon&s=25 Shai Rosenfeld (shai)
on 2007-07-03 09:01
> <%= render :partial => 'story', :collection => alternate(@stories) %>
>
> <li class="<%= story.alt ? "alt" : "" %>">

seems kinda ferocious to me... you could use the partial counter to
count even uneven objects, and then style them accordingly:

===========some.rhtml===============

<%= render :partial => 'story', :collection => @stories %>

=======_story.rhtml=================

<li class="<%= story_counter.modulo(2).nonzero? 'even' : 'odds' %>">


works for ya...?
181976602397443ce119909b9d82cae2?d=identicon&s=25 blinking bear (Guest)
on 2007-07-03 09:13
(Received via mailing list)
Shai,
haha - ferocious?  you're right

your solution worked (just needed another ?):

<li class="<%= story_counter.modulo(2).nonzero? *?* 'even' : 'odds' %>">

thanks,
nathan
4c3acdc3d93f54cc7a7281780ec8a4ee?d=identicon&s=25 Shai Rosenfeld (shai)
on 2007-07-03 09:21
> your solution worked (just needed another ?):
>
> <li class="<%= story_counter.modulo(2).nonzero? *?* 'even' : 'odds' %>">


oh yea, thanks..glad it helps :)
B8873547e864c0f41eb0ccb0f8b18ecf?d=identicon&s=25 Timo Springmann (Guest)
on 2007-07-03 09:33
(Received via mailing list)
If you just want to use another style for every odd row, I'm using
something
like this:


<% @page.items.each do |item| %>
   ...
   <tr class="<%= cycle("even", "odd") %>">
   ...
<% end %>


API:
http://api.rubyonrails.org/classes/ActionView/Help...


Regards,
Timo
This topic is locked and can not be replied to.