Anthony, good point!

validates_with_dependency is the a specific case of suggested 
'validate_further' approach.

Two stage validation doesn't require you to manage dependencies (order, 
etc.) so no need to specify them.

You just rely on results from first stage.

One more example from real project:

order.rb

validates_presence_of :shop
validates_presence_of :line_items
validates_associated :billing_address

...
 
validate_further :on => :create do
  errors.add(:signature, :invalid) unless signature_verified?
end

method signature_verified? calculates signature with

   - shop.secret_key  - shop should present
   - amount, quantity, name for each line_item
   - attributes for billing/shipping, etc.

and compares it with received value from user

do I really have to check manually that everything valid before calculate 
signature?


On Friday, June 22, 2012 3:23:56 AM UTC+3, Richo99 wrote:
>
> I can see some advantge of declaring a validation dependency. Checking 
> if an age is less than 16 makes no sense if the age has failed a 
> validation on being a number already. 
>
> maybe something like: 
>
> class User < AR::Base 
>    validates_numericality_of :age 
>
>    #custom validation 
>    validate :user_older_16, :depends => :age 
>
>    def user_older_16 
>      errors.add(:age, "should be older 16") if age <= 16 
>    end 
>  end 
>
> Where :user_older_16 would only run if age has no errors. In the 
> context shown above this is a sensible dependency, but used 
> incorrectly the concerns expressed by others would be valid. 
>
> Another suggestion: 
>
> class User < AR::Base 
>
>    validates_with_dependency do 
>      validates_numericality_of :age 
>      validate :user_older_16 
>    end 
>
>    def user_older_16 
>      errors.add(:age, "should be older 16") if age <= 16 
>    end 
>  end 
>
> In the above the validations in the validate_with_dependency block 
> would execute in order, exiting the sequence if any fail. 
>
> I think the suggestion to have dependent validaiton is a good one as 
> it allows for structuring simiple validations where you know a 
> validation cannot possibly pass if an earlier one has failed. 
>
> Cheers, 
>
> Anthony Richardson. 
>
> On Thu, Jun 21, 2012 at 8:26 PM, Pavel Gabriel <[email protected]> wrote: 
> > Hi! 
> > 
> > I found that it's a popular use case for AR/AM validations: 
> > 
> > class User < AR::Base 
> >   validates_numericality_of :age 
> > 
> >   #custom validation 
> >   validate :user_older_16 
> > 
> >   def user_older_16 
> >     errors.add(:age, "should be older 16") if age <= 16 
> >   end 
> > end 
> > 
> > When you will try to validate as the following: 
> > 
> >   user =  User.new(age: 'bla') 
> >   user.valid? 
> > 
> > you will get: ArgumentError: comparison of String with 16 failed. 
> > 
> > Folks write a lot of code to check attribute values in their custom 
> > validations, but it's not a DRY way because attributes already validated 
> by 
> > AM. 
> > 
> > Is it a good idea to run validation methods only if errors.count == 0? 
> > 
> > Or maybe we can add something like "validate_further :user_older_16" 
> that 
> > will run only if attributes are valid? 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups 
> > "Ruby on Rails: Core" group. 
> > To view this discussion on the web visit 
> > https://groups.google.com/d/msg/rubyonrails-core/-/Ca3mpVVPWlgJ. 
> > To post to this group, send email to [email protected]. 
> > To unsubscribe from this group, send email to 
> > [email protected]. 
> > For more options, visit this group at 
> > http://groups.google.com/group/rubyonrails-core?hl=en. 
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/rubyonrails-core/-/9eIOuEaQmw4J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to