Submitted 
https://github.com/rails/rails/pull/22369


On Friday, November 20, 2015 at 6:18:02 AM UTC+1, Jon Calhoun wrote:
>
> I'd love to see this feature added to rails core, and thanks Benjamin for 
> posting this workaround for now :D
>
> On Wednesday, October 14, 2015 at 3:50:40 AM UTC-4, 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 rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
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