diff options
author | José Valim <jose.valim@gmail.com> | 2009-12-23 00:36:51 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-12-23 00:36:51 +0100 |
commit | f1085f41287687835659fa23079080204fe32e96 (patch) | |
tree | 8d8899c556c2b4aec9e377f842c15fe0a51416fd /activemodel/lib/active_model/validations/exclusion.rb | |
parent | 2476c5312dcbd29f49672f71617a3d34c6a60cc7 (diff) | |
download | rails-f1085f41287687835659fa23079080204fe32e96.tar.gz rails-f1085f41287687835659fa23079080204fe32e96.tar.bz2 rails-f1085f41287687835659fa23079080204fe32e96.zip |
Move validations in ActiveModel to validators, however all validatity checks are still in the class method.
Diffstat (limited to 'activemodel/lib/active_model/validations/exclusion.rb')
-rw-r--r-- | activemodel/lib/active_model/validations/exclusion.rb | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/activemodel/lib/active_model/validations/exclusion.rb b/activemodel/lib/active_model/validations/exclusion.rb index 2cfdec97a5..209b1c76f9 100644 --- a/activemodel/lib/active_model/validations/exclusion.rb +++ b/activemodel/lib/active_model/validations/exclusion.rb @@ -1,5 +1,12 @@ module ActiveModel module Validations + class ExclusionValidator < EachValidator + def validate_each(record, attribute, value) + return unless options[:in].include?(value) + record.errors.add(attribute, :exclusion, :default => options[:message], :value => value) + end + end + module ClassMethods # Validates that the value of the specified attribute is not in a particular enumerable object. # @@ -21,17 +28,13 @@ module ActiveModel # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The # method, proc or string should return or evaluate to a true or false value. def validates_exclusion_of(*attr_names) - configuration = attr_names.extract_options! - - enum = configuration[:in] || configuration[:within] + options = attr_names.extract_options! + options[:in] ||= options.delete(:within) - raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?(:include?) + raise ArgumentError, "An object with the method include? is required must be supplied as the " << + ":in option of the configuration hash" unless options[:in].respond_to?(:include?) - validates_each(attr_names, configuration) do |record, attr_name, value| - if enum.include?(value) - record.errors.add(attr_name, :exclusion, :default => configuration[:message], :value => value) - end - end + validates_with ExclusionValidator, options.merge(:attributes => attr_names) end end end |