I wish something like that on ActiveRecord yesterday, so, definitely [image:
:+1:].

On 27 October 2015 at 04:27, Benjamin B <[email protected]> wrote:

> Up
> Any thoughts?
>
> On Wednesday, October 14, 2015 at 9:50:40 AM UTC+2, Benjamin B wrote:
>>
>> Greeting,
>>
>>
>> Want to gather your [image: :+1:] and [image: :-1:] on an idea I had,
>> before spending time working on a PR.
>>
>>
>> *The idea is simple*
>> The same way that ActiveModel::Validations::ClassMethods#validates
>> accepts the option on to specify the contexts where this validation is
>> active, I'm thinking to add the option except to exclude contexts where
>> the validation is active.
>>
>>
>> *Use case*
>> Publication has many Page, AttachmentFile, Image, Comments etc...
>>
>> All those model have their own set of validation that execute on *create*
>> and on *update*. Few of them are quite expensive (such as triggering
>> database access to verify uniqueness)
>>
>> Publication can be *published*. Before publishing we run a specific set
>> of validations with on: :publishing options. Those validation are not
>> needed for the model to exist on the database, but needed for the
>> publishing operation. But, we don't want to run again the expensive
>> original sets of validation, we assume they are already valid anyhow
>> because the record exists in the database.
>>
>>
>> *How to do*
>> Currently, this works perfectly:
>>
>>
>> with_options unless: Proc.new { |record| record.validation_context == 
>> :publishing } do |record|
>>   record.validates_presence_of :things
>>   record.validates_uniqueness_of :fields, scope: :publication_id
>> end
>>
>> with_options on: :publishing do |record|
>>   record.validates_presence_of :pages
>> end
>>
>>
>> *Suggestion*
>> But it is not very intuitive and require to dive a bit into the source
>> code to find about validation_context before being able to implement it.
>>
>> So I would suggest to write:
>>
>>
>> with_options except: :publishing do |record|
>>   ...
>> end
>>
>>
>> Thoughts?
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/rubyonrails-core.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Allam Marcos Campanini Matsubara

skype: allam.matsubara
twitter: @allam_matsubara
Phone: +55 (41) 8847-8677

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to