aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2008-04-01 00:13:39 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2008-04-01 00:13:39 +0000
commitd950addbfd275a307382a23064a890c1385579dd (patch)
treed0bc1c319ea8763ee61c3384e33d36a825733840 /activemodel
parent87535bd373a1d8a437b1d04a0272d35025192a79 (diff)
downloadrails-d950addbfd275a307382a23064a890c1385579dd.tar.gz
rails-d950addbfd275a307382a23064a890c1385579dd.tar.bz2
rails-d950addbfd275a307382a23064a890c1385579dd.zip
Move it around a bit
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9174 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/validations.rb78
-rw-r--r--activemodel/lib/active_model/validations/each.rb65
2 files changed, 64 insertions, 79 deletions
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb
index e2fd785ec6..503fb10795 100644
--- a/activemodel/lib/active_model/validations.rb
+++ b/activemodel/lib/active_model/validations.rb
@@ -1,12 +1,10 @@
module ActiveModel
module Validations
- VALIDATIONS = %w( validate validate_on_create validate_on_update )
-
def self.included(base) # :nodoc:
base.extend(ClassMethods)
base.send!(:include, ActiveSupport::Callbacks)
- VALIDATIONS.each do |validation_method|
+ %w( validate validate_on_create validate_on_update ).each do |validation_method|
base.class_eval <<-"end_eval"
def self.#{validation_method}(*methods, &block)
methods = CallbackChain.build(:#{validation_method}, *methods, &block)
@@ -25,16 +23,68 @@ module ActiveModel
end
end
- # All of the following validations are defined in the class scope of the model that you're interested in validating.
- # They offer a more declarative way of specifying when the model is valid and when it is not. It is recommended to use
- # these over the low-level calls to validate and validate_on_create when possible.
module ClassMethods
- DEFAULT_VALIDATION_OPTIONS = {
- :on => :save,
- :allow_nil => false,
- :allow_blank => false,
- :message => nil
- }.freeze
+ 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.
+ #
+ # This can be done with a symbol pointing to a method:
+ #
+ # class Comment < ActiveRecord::Base
+ # validate :must_be_friends
+ #
+ # def must_be_friends
+ # errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee)
+ # end
+ # end
+ #
+ # Or with a block which is passed the current record to be validated:
+ #
+ # class Comment < ActiveRecord::Base
+ # validate do |comment|
+ # comment.must_be_friends
+ # end
+ #
+ # def must_be_friends
+ # errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee)
+ # end
+ # end
+ #
+ # This usage applies to #validate_on_create and #validate_on_update as well.
+
+ # Validates each attribute against a block.
+ #
+ # class Person < ActiveRecord::Base
+ # validates_each :first_name, :last_name do |record, attr, value|
+ # record.errors.add attr, 'starts with z.' if value[0] == ?z
+ # end
+ # end
+ #
+ # Options:
+ # * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update)
+ # * <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
+ # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
+ # method, proc or string should return or evaluate to a true or false value.
+ # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should
+ # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The
+ # method, proc or string should return or evaluate to a true or false value.
+ def validates_each(*attrs)
+ options = attrs.extract_options!.symbolize_keys
+ attrs = attrs.flatten
+
+ # Declare the validation.
+ send(validation_method(options[:on] || :save), options) do |record|
+ attrs.each do |attr|
+ value = record.send(attr)
+ next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
+ yield record, attr, value
+ end
+ end
+ end
private
def validation_method(on)
@@ -67,7 +117,7 @@ module ActiveModel
if responds_to?(:validate_on_create)
ActiveSupport::Deprecations.warn(
- "Base#validate_on_create has been deprecated, please use Base.validate :method, :on => :create instead")
+ "Base#validate_on_create has been deprecated, please use Base.validate_on_create :method instead")
validate_on_create
end
else
@@ -75,7 +125,7 @@ module ActiveModel
if responds_to?(:validate_on_update)
ActiveSupport::Deprecations.warn(
- "Base#validate_on_update has been deprecated, please use Base.validate :method, :on => :update instead")
+ "Base#validate_on_update has been deprecated, please use Base.validate_on_update :method instead")
validate_on_update
end
end
diff --git a/activemodel/lib/active_model/validations/each.rb b/activemodel/lib/active_model/validations/each.rb
deleted file mode 100644
index e9a9e69a86..0000000000
--- a/activemodel/lib/active_model/validations/each.rb
+++ /dev/null
@@ -1,65 +0,0 @@
-module ActiveModel
- module Validations
- module ClassMethods
- # 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.
- #
- # This can be done with a symbol pointing to a method:
- #
- # class Comment < ActiveRecord::Base
- # validate :must_be_friends
- #
- # def must_be_friends
- # errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee)
- # end
- # end
- #
- # Or with a block which is passed the current record to be validated:
- #
- # class Comment < ActiveRecord::Base
- # validate do |comment|
- # comment.must_be_friends
- # end
- #
- # def must_be_friends
- # errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee)
- # end
- # end
- #
- # This usage applies to #validate_on_create and #validate_on_update as well.
-
- # Validates each attribute against a block.
- #
- # class Person < ActiveRecord::Base
- # validates_each :first_name, :last_name do |record, attr, value|
- # record.errors.add attr, 'starts with z.' if value[0] == ?z
- # end
- # end
- #
- # Options:
- # * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update)
- # * <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
- # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
- # method, proc or string should return or evaluate to a true or false value.
- # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should
- # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The
- # method, proc or string should return or evaluate to a true or false value.
- def validates_each(*attrs)
- options = attrs.extract_options!.symbolize_keys
- attrs = attrs.flatten
-
- # Declare the validation.
- send(validation_method(options[:on] || :save), options) do |record|
- attrs.each do |attr|
- value = record.send(attr)
- next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
- yield record, attr, value
- end
- end
- end
- end
- end
-end \ No newline at end of file