[Nitro] 0.26.0 preview
Brian Bugh
brian at xsi-design.com
Thu Dec 15 23:24:23 EST 2005
Ah, I see. The current release gem package does this as well, so it's a
general issue with the way validations are handled. I'm innocent! ;)
I've tested with extend_object and append_features methods and the
on_included block, and I can't seem to find a reliable way to check if
the file is being loaded/extended again. The problem is that when it is
reloaded, the class is just reopened and declared the same way it was
before.
Any logic I can think of to add to validations for uniqueness checking
will have a performance penalty in production, even though production
doesn't have the reload issue.
I will spend more time on this tomorrow to see if I can find a
easy/quick solution for your upcoming release.
Brian B.
On Thu, 2005-12-15 at 13:24 -0800, Bryan Soto wrote:
> Sorry if I wasn't clear. In debug mode, i.e. webrick, files are
> automatically reloaded on change. I noticed after adding and removing a
> field to test Mysql evolution that my validations had increased. Something
> along the lines of:
>
> # user.rb
> class User
> property :username
> property :password
>
> validate_unique :username
> validate_value :password
> end
>
> User.validations.size # => 2
>
> # touch user.rb
>
> User.validations.size # => 4
>
> assuming user.rb is a model class in a Nitro application. Basically, after
> the touch is performed, the file is reloaded and the validations are added
> anew to the already existing validations array (speculation on my part as to
> your implementation) in the already existing User class.
>
> As I said, it's not that a big a deal. A stop and restart of the webrick
> server clears it up. I just happended to notice twice as many error messages
> as I expected from a form. Just make sure your production apps aren't in
> debug mode.
>
> bryan
>
> On 12/14/05, Brian Bugh <brian at xsi-design.com> wrote:
> >
> > Hi,
> >
> > I did the new validation code. I am confused about your statement
> > 'every time a file is loaded.' Do you mean every time the file
> > containing the User classes is loaded? Or any file? Could you give me
> > a simple example that exhibits this behavior?
> >
> > Validations are added to the validation group each time they are
> > declared. If you re-declare a validation, like this:
> >
> > class User
> > property :username
> > property :password
> >
> > validate_unique :username
> > validate_value :password
> > validate_unique :username
> > end
> >
> > Then the username would have a duplicate validation check. Perhaps that
> > is the behavior you are experiencing?
> >
> > I decided to see how the 'other guys' handled validation to see if there
> > was a similar issue. Ironically, it's pretty similar. If you specify
> > something like validates_presence_of twice, it behaves the same way
> > Glue/Og does, and validates twice.
> >
> > If this is determined to be a problem, one way to solve it is to declare
> > the validation type when creating the validation error, in order to test
> > for uniqueness. However, you might have to make a special case for
> > validates_length since a lot of times you want to check for a min and a
> > max. Arguably, you could force the user to use :range for both, but I
> > don't like the idea of artificial limitations.
> >
> > Unless there is a bigger issue here, my suggestion is that the behavior
> > be documented and put under 'least surprise'. If I declared a
> > validation twice, I would expect that it did that validation twice.
> >
> > I have an idea about another way to handle validation, but I have not
> > fleshed it out enough to attempt it. I will keep this issue in
> > consideration while pondering it.
> >
> > Brian B.
> >
> >
> > On Wed, 2005-12-14 at 15:56 -0800, Bryan Soto wrote:
> > > I don't know if it's a big deal, but validation code appears to be
> > > added everytime a file is loaded. Given,
> > >
> > > class User
> > > property :username
> > > property :password
> > >
> > > validate_unique :username
> > > validate_value :password
> > > end
> > >
> > > Everytime the file is reloaded, the validations are added to
> > > validations array so that they're run multiple times.
More information about the Nitro-general
mailing list