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?
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.
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!
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.