Brian H. wrote:
Wes:
It depends. In this case, I’d argue that something like this belongs in
the
model as a validation
Here’s an example of a validation I did for my project management
system.
validates_presence_of :fixed_bid_amount
:if => Proc.new{|p| p.fixed_bid == true}
Even though it seems coupled to the view, it’s not… it’s actually a
business rule. In the above example, I want to force validation (prevent
it
from saving) when the fixed bid amount field has been left blank, but
only
if they have marked the project as a fixed bid.
If I were to create a new project using an XML-based request instead of
a
web form, I would still want this rule to be enforced. Therefore, it
belongs
in the model.
This is a good example. In this case, your “fixed_bid” boolean, which I
assume is a model attribute, maps directly to a checkbox in your view.
In my case, I have a checkbox which represents an option to take a
shortcut to some field value definition, and this checkbox is definitely
not part of the model. In fact the fields that it’s dealing with aren’t
in the model either. They are view-specific representations of part of
the model as well.
I feel like what should be done here is to create a Facade object
“around” the model object that provides all of the non-model related UI
logic, but is capable of passing through to/from the model object when
model fields are displayed/received without modification in the view.
Another issue I’m noticing is that I’m having to do this in my
initialization of my model in order to correctly initialize view
components without overwriting DB provided values when I instantiate
this view object. For example:
self.FAX = contact.FAX if self.FAX.nil?
self.EMAIL = contact.EMAIL if self.EMAIL.nil?
When the object is created from nothing, I want to default certain
attributes. When the object is created from the DB, I don’t want to
default the attributes, I want to get the correct values from the DB.
If I were dealing with a separate form object, I could just default
fields and then they would just become attributes on a successful
save.
Make sense? Or am I overcomplicating things?
Wes
Make sense?