Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5112: allow_nil.patch

File allow_nil.patch, 9.0 kB (added by anonymous, 2 years ago)

[PATCH] Configurable allow_nil

  • activerecord/lib/active_record/base.rb

    old new  
    290290    cattr_accessor :table_name_suffix 
    291291    @@table_name_suffix = "" 
    292292 
     293    # Controls default over how :allow_nil works in validators 
     294    cattr_accessor :allow_nil 
     295    @@allow_nil = false 
     296 
    293297    # Indicates whether or not table names should be the pluralized versions of the corresponding class names. 
    294298    # If true, the default table name for a +Product+ class will be +products+. If false, it would just be +product+. 
    295299    # See table_name for the full rules on table/class naming. This is true, by default. 
  • activerecord/lib/active_record/validations.rb

    old new  
    11module ActiveRecord 
     2 
     3  class Base 
     4    # This determines whether validators allow_nil by default or not   
     5    cattr_accessor :allow_nil 
     6    @@allow_nil = false 
     7  end 
     8 
    29  # Raised by save! and create! when the record is invalid.  Use the 
    310  # record method to retrieve the record which did not validate. 
    411  #   begin 
     
    207214  # 
    208215  # Please do have a look at ActiveRecord::Validations::ClassMethods for a higher level of validations. 
    209216  module Validations 
    210     VALIDATIONS = %w( validate validate_on_create validate_on_update ) 
    211  
     217    
    212218    def self.append_features(base) # :nodoc: 
    213219      super 
    214220      base.extend ClassMethods 
     
    228234    # They offer a more declarative way of specifying when the model is valid and when it is not. It is recommended to use 
    229235    # these over the low-level calls to validate and validate_on_create when possible. 
    230236    module ClassMethods 
    231       DEFAULT_VALIDATION_OPTIONS = { 
    232         :on => :save, 
    233         :allow_nil => false, 
    234         :message => nil 
    235       }.freeze 
    236  
    237       ALL_RANGE_OPTIONS = [ :is, :within, :in, :minimum, :maximum ].freeze 
    238237 
    239238      def validate(*methods, &block) 
    240239        methods << block if block_given? 
     
    289288      # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }).  The 
    290289      # method, proc or string should return or evaluate to a true or false value. 
    291290      def validates_each(*attrs) 
     291        options = { :on        => :save, 
     292                    :allow_nil =>  self.allow_nil } 
    292293        options = attrs.last.is_a?(Hash) ? attrs.pop.symbolize_keys : {} 
    293294        attrs = attrs.flatten 
    294295 
    295296        # Declare the validation. 
    296         send(validation_method(options[:on] || :save)) do |record| 
     297        send(validation_method(options[:on])) do |record| 
    297298          # Don't validate when there is an :if condition and that condition is false 
    298299          unless options[:if] && !evaluate_condition(options[:if], record) 
    299300            attrs.each do |attr| 
     
    357358      # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }).  The 
    358359      # method, proc or string should return or evaluate to a true or false value. 
    359360      def validates_acceptance_of(*attr_names) 
    360         configuration = { :message => ActiveRecord::Errors.default_error_messages[:accepted], :on => :save, :allow_nil => true, :accept => "1" } 
     361        configuration = { :message => ActiveRecord::Errors.default_error_messages[:accepted],  
     362                          :on => :save,  
     363                          :allow_nil => self.allow_nil,  
     364                          :accept => "1" } 
    361365        configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 
    362366 
    363367        attr_accessor *attr_names 
     
    382386      # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }).  The 
    383387      # method, proc or string should return or evaluate to a true or false value. 
    384388      def validates_presence_of(*attr_names) 
    385         configuration = { :message => ActiveRecord::Errors.default_error_messages[:blank], :on => :save } 
     389        configuration = { :message => ActiveRecord::Errors.default_error_messages[:blank],  
     390                          :on => :save } 
    386391        configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 
    387392 
    388393        # can't use validates_each here, because it cannot cope with nonexistent attributes, 
     
    426431      def validates_length_of(*attrs) 
    427432        # Merge given options with defaults. 
    428433        options = { 
     434          :on => :save, 
     435          :allow_nil => self.allow_nil, 
     436          :message => nil, 
    429437          :too_long     => ActiveRecord::Errors.default_error_messages[:too_long], 
    430438          :too_short    => ActiveRecord::Errors.default_error_messages[:too_short], 
    431439          :wrong_length => ActiveRecord::Errors.default_error_messages[:wrong_length] 
    432         }.merge(DEFAULT_VALIDATION_OPTIONS) 
     440        } 
    433441        options.update(attrs.pop.symbolize_keys) if attrs.last.is_a?(Hash) 
    434442 
    435443        # Ensure that one and only one range option is specified. 
     
    548556      # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }).  The 
    549557      # method, proc or string should return or evaluate to a true or false value. 
    550558      def validates_format_of(*attr_names) 
    551         configuration = { :message => ActiveRecord::Errors.default_error_messages[:invalid], :on => :save, :with => nil } 
     559        configuration = { :message => ActiveRecord::Errors.default_error_messages[:invalid],  
     560                          :on => :save,  
     561                          :with => nil, 
     562                          :allow_nil => self.allow_nil } 
    552563        configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 
    553  
     564       
    554565        raise(ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash") unless configuration[:with].is_a?(Regexp) 
    555  
     566           
    556567        validates_each(attr_names, configuration) do |record, attr_name, value| 
    557568          record.errors.add(attr_name, configuration[:message]) unless value.to_s =~ configuration[:with] 
    558569        end 
     
    573584      # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }).  The 
    574585      # method, proc or string should return or evaluate to a true or false value. 
    575586      def validates_inclusion_of(*attr_names) 
    576         configuration = { :message => ActiveRecord::Errors.default_error_messages[:inclusion], :on => :save } 
     587        configuration = { :message => ActiveRecord::Errors.default_error_messages[:inclusion],  
     588                          :on => :save, 
     589                          :allow_nil => self.allow_nil } 
    577590        configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 
    578591 
    579592        enum = configuration[:in] || configuration[:within] 
     
    600613      # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }).  The 
    601614      # method, proc or string should return or evaluate to a true or false value. 
    602615      def validates_exclusion_of(*attr_names) 
    603         configuration = { :message => ActiveRecord::Errors.default_error_messages[:exclusion], :on => :save } 
     616        configuration = { :message => ActiveRecord::Errors.default_error_messages[:exclusion], 
     617                          :on => :save, 
     618                          :allow_nil => self.allow_nil } 
    604619        configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 
    605620 
    606621        enum = configuration[:in] || configuration[:within] 
     
    640655      # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }).  The 
    641656      # method, proc or string should return or evaluate to a true or false value. 
    642657      def validates_associated(*attr_names) 
    643         configuration = { :message => ActiveRecord::Errors.default_error_messages[:invalid], :on => :save } 
     658        configuration = { :message => ActiveRecord::Errors.default_error_messages[:invalid], 
     659                          :on => :save, 
     660                          :allow_nil => self.allow_nil } 
    644661        configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 
    645662 
    646663        validates_each(attr_names, configuration) do |record, attr_name, value| 
     
    666683      # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }).  The 
    667684      # method, proc or string should return or evaluate to a true or false value. 
    668685      def validates_numericality_of(*attr_names) 
    669         configuration = { :message => ActiveRecord::Errors.default_error_messages[:not_a_number], :on => :save, 
    670                            :only_integer => false, :allow_nil => false } 
     686        configuration = { :message => ActiveRecord::Errors.default_error_messages[:not_a_number], 
     687                           :on => :save, 
     688                           :only_integer => false, 
     689                           :allow_nil => self.allow_nil } 
    671690        configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 
    672691 
    673692        if configuration[:only_integer]