Ruby on Rails Friday, April 1, 2011

My application has the following models:

Form
Field
FieldForm
Value
Record

(and others that are irrelevant to the question)

In a Form, I have many Fields and for a Field, there are many Forms. The
idea is to generate a form with customizable Fields. The Field has an
attribute called "type" that defines what control will be rendered
(input:text, input:checkbox, etc) and "format", that defines what data
validation will be made. It can be a date, a phone number or something
else.

The FieldForm has attributes to reference Field and Form, and also
max_length, min_length, unique and required, as well. These attrs are
there because they don't depend directly on Field type and can vary
between forms.

The unique attribute is specially critical. It allow the user to specify
what field will be used to check duplicities (for example, denying a
user to register a form with the same email twice). It's a boolean field
and for a Form, just one field will be "unique".

The Value model is a simple dictionary to use with the Fields that are
radio, select or checkbox. A Field can have many Values.

Finally, the Record model has attributes to save the reference of a
FieldForm, the reference of a Value and a data field that corresponds to
the user input, if Field is something that can be manually entered,
there is no need to reference Value, so it can be null or zero.

Well... The complicated part comes now. I haven't idea on how to
implement the validation. If the validation is done in the controller, I
don't know how to show the errors that could happen (errors.add_to_base
doesn't work because 'errors' object belongs to the model). If the
validation is done in the model, I'll mighty be breaking 50% of the MVC
rules by including references of other models into the Record model
skeleton.

For instance, this is the method I use to check duplicates:

(actually it is part of Records controller)

private
def _exists_by_unique? form, records

data = nil

unique_field = FieldForm.where(:form_id => form.id, :unique =>
true).first

if not records or not unique_field

false

else

records.each do |record|

if record.field_form_id == unique_field.id

data = record.data

end

end

if data

Record.exists? ["field_form_id = ? AND data = ?",
unique_field.id, data ]

end

end

end

It returns true if the record is duplicate or false if it is not or
there is no unique field defined.

I think it's pretty absurd to put this method in the model! And so the
further validations the Records must do before saving into the db, like
checking if record.data.length is in
field_form.min_length..field_form.max_length and if record.data.empty?
if field_form.required == true...

So, any suggestions to help me?

[ Please, have patience with me! I'm about 1 month working with Rails.
Sorry for the english errors! ]

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment