Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10876 (new enhancement)

Opened 1 year ago

Last modified 1 year ago

[PATCH] Extract ActiveRecord validations to ActiveSupport

Reported by: lifofifo Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveSupport Version: edge
Severity: normal Keywords:
Cc: nicwilliams

Description

The patch extracts AR validations to ActiveSupport::Validations module, so that it can be used with normal ruby classes as well.

class Person
  include ActiveSupport::Validations
  attr_accessor :name, :age
  
  validates_presence_of :name
  validates_numericality_of :age
end

Attachments

extract_validations.patch (49.1 kB) - added by lifofifo on 01/21/08 04:18:52.
extract_validations.2.patch (90.0 kB) - added by lifofifo on 03/13/08 00:56:20.

Change History

01/21/08 04:18:52 changed by lifofifo

  • attachment extract_validations.patch added.

01/21/08 04:22:15 changed by lifofifo

Please note that the patch is a work in progress. It still needs a few more tests and may be a notice to people using ActiveRecord::Errors.default_error_messages to modify default error messages.

01/21/08 06:34:11 changed by josh

We should try to get this in ActiveResource as well. At least the Validations::Errors class.

01/21/08 06:42:49 changed by nicwilliams

  • cc set to nicwilliams.
  • type changed from defect to enhancement.

01/21/08 16:09:02 changed by josh

I also think the docs should move with the code. Most people search the api by method name, and they will see two of each validation method. One with no docs (ActiveSupport) and one with them (ActiveRecord). A little confusing. I vote move the method docs to ActiveSupport and remove all the "super(*attr_names)" left overs.

01/21/08 16:12:19 changed by lifofifo

Yeah, I was a little confused there about docs. I guess I'll move the docs and remove super() stuff.

01/21/08 18:30:42 changed by josh

* "is_valid?" should be the conventional "valid?"
* Still need to fix up "validate" class method. Should write to instance variable instead of inheritable attribute.
* Also see "TODO: Use helper ActiveSupport::Callbacks#define_callbacks instead"

def valid?
  errors.clear
  run_callbacks(:validate)

  # Can we do this too? :)
  if respond_to?(:validate)
    ActiveSupport::Deprecation.warn(
      ActiveSupport::Deprecation.deprecated_method_warning(:validate, "(Use a callback symbol, string, or block instead)")
    )
    validate
  end

  errors.empty?
end 

03/13/08 00:56:20 changed by lifofifo

  • attachment extract_validations.2.patch added.