views:

45

answers:

1

I have this haml:

.kudobox
  %div.kudos= number_to_human_size_not_bytes kudos
  %div= (kudos == 1 || kudos == -1) ? "kudo" : "kudos"

Now I want the .kudobox have a positive, negative or zero class, depending on the number of kudos:

-# < 0 kudos
<div class="kudobox negative">

-# == 0 kudos
<div class="kudobox zero">

-# > 0 kudos
<div class="kudobox positive">

How can I achieve this? Thanks.


I know I can

%div{ :class => ( kudos < 0 ? "kudobox negative" : ( kudos == 0 ? "kudobox zero" : ( kudos > 0 ? "kudobox positive" : "kudobox impossible" ) ) ) }

But isn't there a shorter way?

+5  A: 

I haven't looked too closely at your code (it's kind of confusing to read) to tell you if there's a shorter way, but I can say that you'd be much better off writing a helper method than having all of that logic in the view. A helper method with a plain old if or case statement would get the job done more cleanly, and then length wouldn't really be a factor. That way, you could replace the long conditional statement in your view with something easy on the eyes, like

%div{ :class => kudobox(kudos) }
Eleo
Yes, do this. It's a perfect case for a helper.
rspeicher
I always forget helpers. Thanks! Btw. Congrats with your first 66 reputation all for one answer!
Time Machine