aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/validations.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activemodel/lib/active_model/validations.rb')
-rw-r--r--activemodel/lib/active_model/validations.rb61
1 files changed, 19 insertions, 42 deletions
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb
index 460d2d82e5..64343f7a5c 100644
--- a/activemodel/lib/active_model/validations.rb
+++ b/activemodel/lib/active_model/validations.rb
@@ -1,16 +1,12 @@
-require 'active_model/core'
-
module ActiveModel
module Validations
def self.included(base) # :nodoc:
base.extend(ClassMethods)
base.__send__(:include, ActiveSupport::Callbacks)
- base.define_callbacks :validate, :validate_on_create, :validate_on_update
+ base.define_callbacks :validate
end
module ClassMethods
- DEFAULT_VALIDATION_OPTIONS = { :on => :save, :allow_nil => false, :allow_blank => false, :message => nil }.freeze
-
# Adds a validation method or block to the class. This is useful when
# overriding the +validate+ instance method becomes too unwieldly and
# you're looking for more descriptive declaration of your validations.
@@ -38,7 +34,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 +44,7 @@ module ActiveModel
# end
#
# Options:
- # * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>)
+ # * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).
# * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+.
# * <tt>:allow_blank</tt> - Skip validation if attribute is blank.
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
@@ -62,9 +58,9 @@ module ActiveModel
attrs = attrs.flatten
# Declare the validation.
- send(validation_method(options[:on] || :save), options) do |record|
+ send(validation_method(options[:on]), options) do |record|
attrs.each do |attr|
- value = record.send(attr)
+ value = record.get_attribute_value(attr)
next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
yield record, attr, value
end
@@ -72,50 +68,31 @@ module ActiveModel
end
private
- def validation_method(on)
- case on
- when :save then :validate
- when :create then :validate_on_create
- when :update then :validate_on_update
- end
- end
+
+ def validation_method(on)
+ :validate
+ end
end
# 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.
def valid?
errors.clear
-
run_callbacks(:validate)
-
- if responds_to?(:validate)
- ActiveSupport::Deprecations.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")
- validate_on_create
- end
- else
- run_callbacks(:validate_on_update)
+ errors.empty?
+ end
- if responds_to?(:validate_on_update)
- ActiveSupport::Deprecations.warn(
- "Base#validate_on_update has been deprecated, please use Base.validate_on_update :method instead")
- validate_on_update
- end
- end
+ # Performs the opposite of <tt>valid?</tt>. Returns true if errors were added, false otherwise.
+ def invalid?
+ !valid?
+ end
- errors.empty?
+ def get_attribute_value(attribute)
+ respond_to?(attribute.to_sym) ? send(attribute.to_sym) : instance_variable_get(:"@#{attribute}")
end
end
end
@@ -123,4 +100,4 @@ 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