From 8828b2ca674acfa028a3c1e086a1795d3bb893e1 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Thu, 19 Mar 2009 23:28:59 +0000 Subject: Move all the Active Record validations to Active Model --- activemodel/lib/active_model/validations.rb | 41 ++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 12 deletions(-) (limited to 'activemodel/lib/active_model/validations.rb') diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 460d2d82e5..3ea17381c9 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -38,7 +38,7 @@ module ActiveModel # end # # This usage applies to +validate_on_create+ and +validate_on_update as well+. - # + # Validates each attribute against a block. # # class Person < ActiveRecord::Base @@ -48,7 +48,7 @@ module ActiveModel # end # # Options: - # * :on - Specifies when this validation is active (default is :save, other options :create, :update) + # * :on - Specifies when this validation is active (default is :save, other options :create, :update). # * :allow_nil - Skip validation if attribute is +nil+. # * :allow_blank - Skip validation if attribute is blank. # * :if - Specifies a method, proc or string to call to determine if the validation should @@ -83,7 +83,7 @@ module ActiveModel # Returns the Errors object that holds all information about attribute error messages. def errors - @errors ||= Errors.new + @errors ||= Errors.new(self) end # Runs all the specified validations and returns true if no errors were added otherwise false. @@ -92,35 +92,52 @@ module ActiveModel run_callbacks(:validate) - if responds_to?(:validate) - ActiveSupport::Deprecations.warn "Base#validate has been deprecated, please use Base.validate :method instead" + if respond_to?(:validate) + # ActiveSupport::Deprecation.warn "Base#validate has been deprecated, please use Base.validate :method instead" validate end if new_record? run_callbacks(:validate_on_create) - if responds_to?(:validate_on_create) - ActiveSupport::Deprecations.warn( - "Base#validate_on_create has been deprecated, please use Base.validate_on_create :method instead") + if respond_to?(:validate_on_create) + # ActiveSupport::Deprecation.warn "Base#validate_on_create has been deprecated, please use Base.validate_on_create :method instead" validate_on_create end else run_callbacks(:validate_on_update) - if responds_to?(:validate_on_update) - ActiveSupport::Deprecations.warn( - "Base#validate_on_update has been deprecated, please use Base.validate_on_update :method instead") + if respond_to?(:validate_on_update) + # ActiveSupport::Deprecation.warn "Base#validate_on_update has been deprecated, please use Base.validate_on_update :method instead" validate_on_update end end errors.empty? end + + # Performs the opposite of valid?. Returns true if errors were added, false otherwise. + def invalid? + !valid? + end + + protected + + # Overwrite this method for validation checks on all saves and use Errors.add(field, msg) for invalid attributes. + def validate + end + + # Overwrite this method for validation checks used only on creation. + def validate_on_create + end + + # Overwrite this method for validation checks used only on updates. + def validate_on_update + end end end Dir[File.dirname(__FILE__) + "/validations/*.rb"].sort.each do |path| filename = File.basename(path) require "active_model/validations/#{filename}" -end \ No newline at end of file +end -- cgit v1.2.3