Forum: Ruby on Rails How to associate a model's attribute with CSS style

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.
22785d4dbf585723bf60458ece0170e1?d=identicon&s=25 jjathman@gmail.com (Guest)
on 2006-12-31 09:01
(Received via mailing list)
So this isn't a question specific to Rails, but I wanted to keep the
purity of the MVC architecture as much as possible.

I have an AR model, order, which can have a status.  I want to style
the orders based on what their status is.  Right now all my status AR
model has is a name and I'm creating the CSS class in my view with
something like:

class="<%= order.status.name %>"

but I don't like how coupled this makes everything.  I'm sure people
have figured out a best practice for this.  I was thinking maybe adding
a style attribute to Status?  Anyone else have a better suggestion?

Thanks,
Joe
Fd4f7add4445007baedc5d84f9e67ca2?d=identicon&s=25 Ross Riley (Guest)
on 2006-12-31 10:22
(Received via mailing list)
I don't see how this is incorrectly coupled, you are printing out the
status name in the view which the view then styles accordingly. Adding
a style attribute to the model would be incorrect because then the
model would know about the view.

Since the styling only makes sense in this particular html view (if
you were to render as xml it would be unnecessary) then that seems
like the best place to me.

On 12/31/06, jjathman@gmail.com <jjathman@gmail.com> wrote:
>
> but I don't like how coupled this makes everything.  I'm sure people
> have figured out a best practice for this.  I was thinking maybe adding
> a style attribute to Status?  Anyone else have a better suggestion?
>
> Thanks,
> Joe
>
>
> >
>


--
Ross Riley
www.sorrylies.com
4daaf36a6c70d477e1c855c32520ef37?d=identicon&s=25 Steve Bartholomew (Guest)
on 2006-12-31 14:22
(Received via mailing list)
I don't see this as a violation of MVC at all.  In fact, I'd say that
adding a style attribute to status *would* be a violation.

One thing i would say is that it's probably worth making a helper to
get the css class:

def css_style_from(text)
   text.tablelize
end

class="<%= css_style_from(order.status.name) %>"

or something like that.  Tablelizing the text will mean you don't get
problems with spaces in status names:
Order Complete becomes order_complete

It's not great to use tablelize in the view as you may change your CSS
naming rules, so a nice little helper will keep you DRY.

Hope that helps,

Steve
www.curve21.com
22785d4dbf585723bf60458ece0170e1?d=identicon&s=25 jjathman@gmail.com (Guest)
on 2006-12-31 23:40
(Received via mailing list)
Yeah, I figured out early on that having spaces in a style is not
possible.  The only thing I'm worried about is that if I make a style
for "pending" it will be a programming change (changing the style's
name) if the user's ever want to change what the name is, "unprocessed"
for example.  Not a big deal, but I never like having to make
programming changes when an arbitrary value is changed.  Thoughts?
Thanks for the help!

Joe
4daaf36a6c70d477e1c855c32520ef37?d=identicon&s=25 Steve Bartholomew (Guest)
on 2007-01-02 12:09
(Received via mailing list)
> Not a big deal, but I never like having to make
> programming changes when an arbitrary value is changed.  Thoughts?

I'd say that unless the client *needs* to update the status name
regularly, it's unnecessary to provide them with that functionality.  I
guess it comes down to the system in the end.  If this is a system
where the styles and names are changed regularly, then storing styling
in the db might not be such a bad thing.  However, in most cases it's
probably not needed and you should choose the method that makes your
life easier and benefits the application.

Steve
This topic is locked and can not be replied to.