How to associate a model's attribute with CSS style


#1

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


#2

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, removed_email_address@domain.invalid removed_email_address@domain.invalid 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 R.
www.sorrylies.com


#3

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


#4

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


#5

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