Permalink
Browse files

Added validation macros to make the stackable just like the lifecycle…

… callbacks

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@94 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 586aa2f commit fc817eff447a268ce245a48369e7bd90d8f7f31f @dhh dhh committed Dec 9, 2004
Showing with 69 additions and 2 deletions.
  1. +13 −0 activerecord/CHANGELOG
  2. +46 −1 activerecord/lib/active_record/validations.rb
  3. +10 −1 activerecord/test/fixtures/reply.rb
@@ -1,5 +1,18 @@
*CVS*
+* Added validation macros to make the stackable just like the lifecycle callbacks. Examples:
+
+ class Person < ActiveRecord::Base
+ validate { |record| record.errors.add("name", "too short") unless name.size > 10 }
+ validate { |record| record.errors.add("name", "too long") unless name.size < 20 }
+ validate_on_create :validate_password
+
+ private
+ def validate_password
+ errors.add("password", "too short") unless password.size > 6
+ end
+ end
+
* Added ActiveRecord::Mixins::Touch that will record creation and update times of objects [xal]. Example:
class Bill < ActiveRecord::Base
@@ -37,6 +37,8 @@ module ActiveRecord
#
# An +Errors+ object is automatically created for every Active Record.
module Validations
+ VALIDATIONS = %w( validate validate_on_create validate_on_create )
+
def self.append_features(base) # :nodoc:
super
@@ -46,6 +48,8 @@ def self.append_features(base) # :nodoc:
alias_method :update_attribute_without_validation_skipping, :update_attribute
alias_method :update_attribute, :update_attribute_with_validation_skipping
+
+ VALIDATIONS.each { |vd| base.class_eval("def self.#{vd}(*methods) write_inheritable_array(\"#{vd}\", methods - (read_inheritable_attribute(\"#{vd}\") || [])) end") }
end
end
@@ -66,8 +70,18 @@ def update_attribute_with_validation_skipping(name, value)
# Runs validate and validate_on_create or validate_on_update and returns true if no errors were added otherwise false.
def valid?
errors.clear
+
+ run_validations(:validate)
validate
- if new_record? then validate_on_create else validate_on_update end
+
+ if new_record?
+ run_validations(:validate_on_create)
+ validate_on_create
+ else
+ run_validations(:validate_on_update)
+ validate_on_update
+ end
+
errors.empty?
end
@@ -89,6 +103,37 @@ def validate_on_create #:doc:
# Overwrite this method for validation checks used only on updates.
def validate_on_update # :doc:
end
+
+ private
+ def run_validations(validation_method)
+ validations = self.class.read_inheritable_attribute(validation_method.to_s)
+ if validations.nil? then return end
+ validations.each do |validation|
+ if Symbol === validation
+ self.send(validation)
+ elsif String === validation
+ eval(validation, binding)
+ elsif validation_block?(validation)
+ validation.call(self)
+ elsif filter_class?(validation, validation_method)
+ validation.send(validation_method, self)
+ else
+ raise(
+ ActiveRecordError,
+ "Validations need to be either a symbol, string (to be eval'ed), proc/method, or " +
+ "class implementing a static validation method"
+ )
+ end
+ end
+ end
+
+ def validation_block?(validation)
+ validation.respond_to?("call") && (validation.arity == 1 || validation.arity == -1)
+ end
+
+ def validation_class?(validation, validation_method)
+ validation.respond_to?(validation_method)
+ end
end
# Active Record validation is reported to and from this object, which is used by Base#save to
@@ -1,20 +1,29 @@
class Reply < Topic
belongs_to :topic, :foreign_key => "parent_id", :counter_cache => true
+
+ validate :errors_on_empty_content
+ validate_on_create :title_is_wrong_create
attr_accessible :title, :author_name, :author_email_address, :written_on, :content, :last_read
def validate
errors.add("title", "Empty") unless attribute_present? "title"
+ end
+
+ def errors_on_empty_content
errors.add("content", "Empty") unless attribute_present? "content"
end
def validate_on_create
- errors.add("title", "is Wrong Create") if attribute_present?("title") && title == "Wrong Create"
if attribute_present?("title") && attribute_present?("content") && content == "Mismatch"
errors.add("title", "is Content Mismatch")
end
end
+ def title_is_wrong_create
+ errors.add("title", "is Wrong Create") if attribute_present?("title") && title == "Wrong Create"
+ end
+
def validate_on_update
errors.add("title", "is Wrong Update") if attribute_present?("title") && title == "Wrong Update"
end

0 comments on commit fc817ef

Please sign in to comment.