From fc817eff447a268ce245a48369e7bd90d8f7f31f Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 9 Dec 2004 12:50:18 +0000 Subject: 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 --- activerecord/CHANGELOG | 13 ++++++++ activerecord/lib/active_record/validations.rb | 47 ++++++++++++++++++++++++++- activerecord/test/fixtures/reply.rb | 11 ++++++- 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index eaaa2027b4..16ec1a1e14 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -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 diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 07bc7b99b5..14e79cf68b 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -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 @@ module ActiveRecord 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 @@ module ActiveRecord # 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 @@ module ActiveRecord # 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 diff --git a/activerecord/test/fixtures/reply.rb b/activerecord/test/fixtures/reply.rb index 51dfe21d2d..127f2052f3 100755 --- a/activerecord/test/fixtures/reply.rb +++ b/activerecord/test/fixtures/reply.rb @@ -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 -- cgit v1.2.3