Normal validation messages tell the user that something is so terribly wrong as to prohibit an object from saving. I recently had the additional need to warn users that somethings fishy about their object even though it doesn''t preclude saving it. In that particular case fishiness only involved a custom validation method and a new ''warning'' level def validates_not_fishy(options = {}) level = options[:level] ? options[:level].to_s.pluralize.to_sym : :errors send(validation_method(options[:on] || :save)) do |record| if something_is_fishy # Instead of # record.errors.add_to_base("Something is wrong") # do record.send(level).add_to_base("Something is wrong") end end end def warnings @warnings ||= ActiveRecord::Errors.new(self) end All of the above can, of course, be easily added to the classes where it is needed, or duck punched into ActiveRecord. What I''d like to have is a way to use the already existing validations for custom validation levels, too. To be clear, I don''t want any further levels to be added to AR, but I want the necessary hooks to add them myself in an app or plugin. Michael -- Michael Schuerig mailto:michael@schuerig.de http://www.schuerig.de/michael/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
> All of the above can, of course, be easily added to the classes where it > is needed, or duck punched into ActiveRecord. What I''d like to have is > a way to use the already existing validations for custom validation > levels, too. To be clear, I don''t want any further levels to be added > to AR, but I want the necessary hooks to add them myself in an app or > plugin.What kind of hooks do you have in mind? I''m always a little suspect of adding ''hooks'' for some theoretical future extensibility, but there''s definitely some scope for improving the implementation of the validations. I''ve always found it a little frustrating that the actual logic for ''validating the presence of'' isn''t encapsulated in a single function that you can call from your own validations. So the validations could be used in situations where the macros don''t apply. -- Cheers Koz --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
On Sunday 04 May 2008, Michael Koziarski wrote:> > All of the above can, of course, be easily added to the classes > > where it is needed, or duck punched into ActiveRecord. What I''d > > like to have is a way to use the already existing validations for > > custom validation levels, too. To be clear, I don''t want any > > further levels to be added to AR, but I want the necessary hooks to > > add them myself in an app or plugin. > > What kind of hooks do you have in mind? I''m always a little suspect > of adding ''hooks'' for some theoretical future extensibility, but > there''s definitely some scope for improving the implementation of the > validations.I''m asking for nothing more than record.send(level).add_to_base("Something is wrong") instead of record.errors.add_to_base("Something is wrong") wherever it applies with level defaulting to :errors, i.e. level = options[:level] ? options[:level].to_s.pluralize.to_sym : :errors Michael -- Michael Schuerig mailto:michael@schuerig.de http://www.schuerig.de/michael/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
> wherever it applies with level defaulting to :errors, i.e. > > > level = options[:level] ? > options[:level].to_s.pluralize.to_sym : > :errorsWhip up a patch for this so we can see how widespread the result is. I''m a little suspect of making this kind of change, but if it''s relatively low impact perhaps it''s not so bad. -- Cheers Koz --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
On Sunday 04 May 2008, Michael Koziarski wrote:> > wherever it applies with level defaulting to :errors, i.e. > > > > > > level = options[:level] ? > > > > options[:level].to_s.pluralize.to_sym : > > :errors > > Whip up a patch for this so we can see how widespread the result is. > I''m a little suspect of making this kind of change, but if it''s > relatively low impact perhaps it''s not so bad.http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/107-level-hooks-for-validations I have used neither lighthouse nor git before, I hope I didn''t blow anything. Michael -- Michael Schuerig mailto:michael@schuerig.de http://www.schuerig.de/michael/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---