From 9131a88bb8e82f139ec49b4057fb6065ba0a2c6a Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Tue, 11 May 2010 12:28:42 +0200 Subject: validation macros can now be used within an instance --- .../lib/active_model/validations/acceptance.rb | 2 +- .../lib/active_model/validations/confirmation.rb | 2 +- .../lib/active_model/validations/exclusion.rb | 2 +- activemodel/lib/active_model/validations/format.rb | 2 +- .../lib/active_model/validations/helper_methods.rb | 12 ++++ .../lib/active_model/validations/inclusion.rb | 2 +- activemodel/lib/active_model/validations/length.rb | 2 +- .../lib/active_model/validations/numericality.rb | 2 +- .../lib/active_model/validations/presence.rb | 2 +- activemodel/lib/active_model/validations/with.rb | 66 ++++++++++++++++++++++ 10 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 activemodel/lib/active_model/validations/helper_methods.rb (limited to 'activemodel/lib/active_model/validations') diff --git a/activemodel/lib/active_model/validations/acceptance.rb b/activemodel/lib/active_model/validations/acceptance.rb index fbd622eb6d..26b12b504b 100644 --- a/activemodel/lib/active_model/validations/acceptance.rb +++ b/activemodel/lib/active_model/validations/acceptance.rb @@ -21,7 +21,7 @@ module ActiveModel end end - module ClassMethods + module HelperMethods # Encapsulates the pattern of wanting to validate the acceptance of a terms of service check box (or similar agreement). Example: # # class Person < ActiveRecord::Base diff --git a/activemodel/lib/active_model/validations/confirmation.rb b/activemodel/lib/active_model/validations/confirmation.rb index 8041d4b61f..51445343f2 100644 --- a/activemodel/lib/active_model/validations/confirmation.rb +++ b/activemodel/lib/active_model/validations/confirmation.rb @@ -12,7 +12,7 @@ module ActiveModel end end - module ClassMethods + module HelperMethods # Encapsulates the pattern of wanting to validate a password or email address field with a confirmation. Example: # # Model: diff --git a/activemodel/lib/active_model/validations/exclusion.rb b/activemodel/lib/active_model/validations/exclusion.rb index 2f51edfa9a..2ee78f5dd2 100644 --- a/activemodel/lib/active_model/validations/exclusion.rb +++ b/activemodel/lib/active_model/validations/exclusion.rb @@ -12,7 +12,7 @@ module ActiveModel end end - module ClassMethods + module HelperMethods # Validates that the value of the specified attribute is not in a particular enumerable object. # # class Person < ActiveRecord::Base diff --git a/activemodel/lib/active_model/validations/format.rb b/activemodel/lib/active_model/validations/format.rb index 9a9e7eca4d..c34c860d4d 100644 --- a/activemodel/lib/active_model/validations/format.rb +++ b/activemodel/lib/active_model/validations/format.rb @@ -24,7 +24,7 @@ module ActiveModel end end - module ClassMethods + module HelperMethods # Validates whether the value of the specified attribute is of the correct form, going by the regular expression provided. # You can require that the attribute matches the regular expression: # diff --git a/activemodel/lib/active_model/validations/helper_methods.rb b/activemodel/lib/active_model/validations/helper_methods.rb new file mode 100644 index 0000000000..4c709b1fa9 --- /dev/null +++ b/activemodel/lib/active_model/validations/helper_methods.rb @@ -0,0 +1,12 @@ +module ActiveModel + module Validations + module HelperMethods + private + + def _merge_attributes(attr_names) + options = attr_names.extract_options! + options.merge(:attributes => attr_names.flatten) + end + end + end +end \ No newline at end of file diff --git a/activemodel/lib/active_model/validations/inclusion.rb b/activemodel/lib/active_model/validations/inclusion.rb index 96dc8b6e15..446646d247 100644 --- a/activemodel/lib/active_model/validations/inclusion.rb +++ b/activemodel/lib/active_model/validations/inclusion.rb @@ -12,7 +12,7 @@ module ActiveModel end end - module ClassMethods + module HelperMethods # Validates whether the value of the specified attribute is available in a particular enumerable object. # # class Person < ActiveRecord::Base diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb index 95da3e93ea..d7218f4f52 100644 --- a/activemodel/lib/active_model/validations/length.rb +++ b/activemodel/lib/active_model/validations/length.rb @@ -51,7 +51,7 @@ module ActiveModel end end - module ClassMethods + module HelperMethods # Validates that the specified attribute matches the length restrictions supplied. Only one option can be used at a time: # diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index f974999bef..716010e88b 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -70,7 +70,7 @@ module ActiveModel end - module ClassMethods + module HelperMethods # Validates whether the value of the specified attribute is numeric by trying to convert it to # a float with Kernel.Float (if only_integer is false) or applying it to the regular expression # /\A[\+\-]?\d+\Z/ (if only_integer is set to true). diff --git a/activemodel/lib/active_model/validations/presence.rb b/activemodel/lib/active_model/validations/presence.rb index 4a71cf79b5..b319f4834b 100644 --- a/activemodel/lib/active_model/validations/presence.rb +++ b/activemodel/lib/active_model/validations/presence.rb @@ -8,7 +8,7 @@ module ActiveModel end end - module ClassMethods + module HelperMethods # Validates that the specified attributes are not blank (as defined by Object#blank?). Happens by default on save. Example: # # class Person < ActiveRecord::Base diff --git a/activemodel/lib/active_model/validations/with.rb b/activemodel/lib/active_model/validations/with.rb index 83d3ea80d6..2a2d0d55b4 100644 --- a/activemodel/lib/active_model/validations/with.rb +++ b/activemodel/lib/active_model/validations/with.rb @@ -75,5 +75,71 @@ module ActiveModel end end end + + # Passes the record off to the class or classes specified and allows them + # to add errors based on more complex conditions. + # + # class Person + # include ActiveModel::Validations + # + # validates :instance_validations + # + # def instance_validations + # validates_with MyValidator + # end + # end + # + # class MyValidator < ActiveModel::Validator + # def validate(record) + # if some_complex_logic + # record.errors[:base] << "This record is invalid" + # end + # end + # + # private + # def some_complex_logic + # # ... + # end + # end + # + # You may also pass it multiple classes, like so: + # + # class Person + # include ActiveModel::Validations + # + # validates :instance_validations, :on => :create + # + # def instance_validations + # validates_with MyValidator, MyOtherValidator + # end + # end + # + # Standard configuration options (:on, :if and :unless), which are + # available on the class version of validates_with, should instead be + # placed on the validates method as these are applied and tested + # in the callback + # + # If you pass any additional configuration options, they will be passed + # to the class and available as options: + # + # class Person + # include ActiveModel::Validations + # validates_with MyValidator, :my_custom_key => "my custom value" + # end + # + # class MyValidator < ActiveModel::Validator + # def validate(record) + # options[:my_custom_key] # => "my custom value" + # end + # end + # + def validates_with(*args, &block) + options = args.extract_options! + args.each do |klass| + validator = klass.new(options, &block) + validator.setup(self) if validator.respond_to?(:setup) + validator.validate(self) + end + end end end \ No newline at end of file -- cgit v1.2.3