Ticket #5112: allow_nil.patch
| File allow_nil.patch, 9.0 kB (added by anonymous, 2 years ago) |
|---|
-
activerecord/lib/active_record/base.rb
old new 290 290 cattr_accessor :table_name_suffix 291 291 @@table_name_suffix = "" 292 292 293 # Controls default over how :allow_nil works in validators 294 cattr_accessor :allow_nil 295 @@allow_nil = false 296 293 297 # Indicates whether or not table names should be the pluralized versions of the corresponding class names. 294 298 # If true, the default table name for a +Product+ class will be +products+. If false, it would just be +product+. 295 299 # See table_name for the full rules on table/class naming. This is true, by default. -
activerecord/lib/active_record/validations.rb
old new 1 1 module 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 2 9 # Raised by save! and create! when the record is invalid. Use the 3 10 # record method to retrieve the record which did not validate. 4 11 # begin … … 207 214 # 208 215 # Please do have a look at ActiveRecord::Validations::ClassMethods for a higher level of validations. 209 216 module Validations 210 VALIDATIONS = %w( validate validate_on_create validate_on_update ) 211 217 212 218 def self.append_features(base) # :nodoc: 213 219 super 214 220 base.extend ClassMethods … … 228 234 # They offer a more declarative way of specifying when the model is valid and when it is not. It is recommended to use 229 235 # these over the low-level calls to validate and validate_on_create when possible. 230 236 module ClassMethods 231 DEFAULT_VALIDATION_OPTIONS = {232 :on => :save,233 :allow_nil => false,234 :message => nil235 }.freeze236 237 ALL_RANGE_OPTIONS = [ :is, :within, :in, :minimum, :maximum ].freeze238 237 239 238 def validate(*methods, &block) 240 239 methods << block if block_given? … … 289 288 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 290 289 # method, proc or string should return or evaluate to a true or false value. 291 290 def validates_each(*attrs) 291 options = { :on => :save, 292 :allow_nil => self.allow_nil } 292 293 options = attrs.last.is_a?(Hash) ? attrs.pop.symbolize_keys : {} 293 294 attrs = attrs.flatten 294 295 295 296 # Declare the validation. 296 send(validation_method(options[:on] || :save)) do |record|297 send(validation_method(options[:on])) do |record| 297 298 # Don't validate when there is an :if condition and that condition is false 298 299 unless options[:if] && !evaluate_condition(options[:if], record) 299 300 attrs.each do |attr| … … 357 358 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 358 359 # method, proc or string should return or evaluate to a true or false value. 359 360 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" } 361 365 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 362 366 363 367 attr_accessor *attr_names … … 382 386 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 383 387 # method, proc or string should return or evaluate to a true or false value. 384 388 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 } 386 391 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 387 392 388 393 # can't use validates_each here, because it cannot cope with nonexistent attributes, … … 426 431 def validates_length_of(*attrs) 427 432 # Merge given options with defaults. 428 433 options = { 434 :on => :save, 435 :allow_nil => self.allow_nil, 436 :message => nil, 429 437 :too_long => ActiveRecord::Errors.default_error_messages[:too_long], 430 438 :too_short => ActiveRecord::Errors.default_error_messages[:too_short], 431 439 :wrong_length => ActiveRecord::Errors.default_error_messages[:wrong_length] 432 } .merge(DEFAULT_VALIDATION_OPTIONS)440 } 433 441 options.update(attrs.pop.symbolize_keys) if attrs.last.is_a?(Hash) 434 442 435 443 # Ensure that one and only one range option is specified. … … 548 556 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 549 557 # method, proc or string should return or evaluate to a true or false value. 550 558 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 } 552 563 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 553 564 554 565 raise(ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash") unless configuration[:with].is_a?(Regexp) 555 566 556 567 validates_each(attr_names, configuration) do |record, attr_name, value| 557 568 record.errors.add(attr_name, configuration[:message]) unless value.to_s =~ configuration[:with] 558 569 end … … 573 584 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 574 585 # method, proc or string should return or evaluate to a true or false value. 575 586 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 } 577 590 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 578 591 579 592 enum = configuration[:in] || configuration[:within] … … 600 613 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 601 614 # method, proc or string should return or evaluate to a true or false value. 602 615 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 } 604 619 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 605 620 606 621 enum = configuration[:in] || configuration[:within] … … 640 655 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 641 656 # method, proc or string should return or evaluate to a true or false value. 642 657 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 } 644 661 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 645 662 646 663 validates_each(attr_names, configuration) do |record, attr_name, value| … … 666 683 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 667 684 # method, proc or string should return or evaluate to a true or false value. 668 685 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 } 671 690 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 672 691 673 692 if configuration[:only_integer]