diff options
Diffstat (limited to 'activemodel/lib/active_model')
| -rw-r--r-- | activemodel/lib/active_model/validations/exclusion.rb | 5 | ||||
| -rw-r--r-- | activemodel/lib/active_model/validations/format.rb | 39 | ||||
| -rw-r--r-- | activemodel/lib/active_model/validations/inclusion.rb | 3 | 
3 files changed, 27 insertions, 20 deletions
diff --git a/activemodel/lib/active_model/validations/exclusion.rb b/activemodel/lib/active_model/validations/exclusion.rb index d135d0164a..a85c23f725 100644 --- a/activemodel/lib/active_model/validations/exclusion.rb +++ b/activemodel/lib/active_model/validations/exclusion.rb @@ -9,13 +9,14 @@ module ActiveModel                        "and must be supplied as the :in option of the configuration hash"        def check_validity! -        unless [:include?, :call].any?{ |method| options[:in].respond_to?(method) } +        unless [:include?, :call].any? { |method| options[:in].respond_to?(method) }            raise ArgumentError, ERROR_MESSAGE          end        end        def validate_each(record, attribute, value) -        exclusions = options[:in].respond_to?(:call) ? options[:in].call(record) : options[:in] +        delimiter = options[:in] +        exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter          if exclusions.send(inclusion_method(exclusions), value)            record.errors.add(attribute, :exclusion, options.except(:in).merge!(:value => value))          end diff --git a/activemodel/lib/active_model/validations/format.rb b/activemodel/lib/active_model/validations/format.rb index 349e110a4b..6f23d492eb 100644 --- a/activemodel/lib/active_model/validations/format.rb +++ b/activemodel/lib/active_model/validations/format.rb @@ -5,19 +5,11 @@ module ActiveModel      class FormatValidator < EachValidator        def validate_each(record, attribute, value)          if options[:with] -          regexp = options[:with].respond_to?(:call) ? options[:with].call(record) : options[:with] -          if regexp.is_a?(Regexp) -            record.errors.add(attribute, :invalid, options.except(:with).merge!(:value => value)) if value.to_s !~ regexp -          else -            raise ArgumentError, "A proc or lambda given to :with option must returns a regular expression" -          end +          regexp = option_call(record, :with) +          record_error(record, attribute, :with, value) if value.to_s !~ regexp          elsif options[:without] -          regexp = options[:without].respond_to?(:call) ? options[:without].call(record) : options[:without] -          if regexp.is_a?(Regexp) -            record.errors.add(attribute, :invalid, options.except(:without).merge!(:value => value)) if value.to_s =~ regexp -          else -            raise ArgumentError, "A proc or lambda given to :without option must returns a regular expression" -          end +          regexp = option_call(record, :without) +          record_error(record, attribute, :without, value) if value.to_s =~ regexp          end        end @@ -26,12 +18,25 @@ module ActiveModel            raise ArgumentError, "Either :with or :without must be supplied (but not both)"          end -        if options[:with] && !options[:with].is_a?(Regexp) && !options[:with].respond_to?(:call) -          raise ArgumentError, "A regular expression or a proc or lambda must be supplied as the :with option of the configuration hash" -        end +        check_options_validity(options, :with) +        check_options_validity(options, :without) +      end + +      private + +      def option_call(record, name) +        option = options[name] +        option.respond_to?(:call) ? option.call(record) : option +      end + +      def record_error(record, attribute, name, value) +        record.errors.add(attribute, :invalid, options.except(name).merge!(:value => value)) +      end -        if options[:without] && !options[:without].is_a?(Regexp) && !options[:without].respond_to?(:call) -          raise ArgumentError, "A regular expression or a proc or lambda must be supplied as the :without option of the configuration hash" +      def check_options_validity(options, name) +        option = options[name] +        if option && !option.is_a?(Regexp) && !option.respond_to?(:call) +          raise ArgumentError, "A regular expression or a proc or lambda must be supplied as :#{name}"          end        end      end diff --git a/activemodel/lib/active_model/validations/inclusion.rb b/activemodel/lib/active_model/validations/inclusion.rb index 43b1d87d0b..d32aebeb88 100644 --- a/activemodel/lib/active_model/validations/inclusion.rb +++ b/activemodel/lib/active_model/validations/inclusion.rb @@ -15,7 +15,8 @@ module ActiveModel        end        def validate_each(record, attribute, value) -        exclusions = options[:in].respond_to?(:call) ? options[:in].call(record) : options[:in] +        delimiter = options[:in] +        exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter          unless exclusions.send(inclusion_method(exclusions), value)            record.errors.add(attribute, :inclusion, options.except(:in).merge!(:value => value))          end  | 
