Changeset 9172
- Timestamp:
- 03/31/08 23:45:48 (4 months ago)
- Files:
-
- trunk/activemodel/lib/active_model/validations.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activemodel/lib/active_model/validations.rb
r9171 r9172 623 623 end 624 624 625 # The validation process on save can be skipped by passing false. The regular Base#save method is626 # replaced with this when the validations module is mixed in, which it is by default.627 def save_with_validation(perform_validation = true)628 if perform_validation && valid? || !perform_validation629 save_without_validation630 else631 false632 end633 end634 635 # Attempts to save the record just like Base#save but will raise a RecordInvalid exception instead of returning false636 # if the record is not valid.637 def save_with_validation!638 if valid?639 save_without_validation!640 else641 raise RecordInvalid.new(self)642 end643 end644 645 # Updates a single attribute and saves the record without going through the normal validation procedure.646 # This is especially useful for boolean flags on existing records. The regular +update_attribute+ method647 # in Base is replaced with this when the validations module is mixed in, which it is by default.648 def update_attribute_with_validation_skipping(name, value)649 send(name.to_s + '=', value)650 save(false)651 end652 625 653 626 # Runs validate and validate_on_create or validate_on_update and returns true if no errors were added otherwise false. … … 656 629 657 630 run_callbacks(:validate) 658 validate 631 632 if responds_to?(:validate) 633 ActiveSupport::Deprecations.warn "Base#validate has been deprecated, please use Base.validate :method instead" 634 validate 635 end 659 636 660 637 if new_record? 661 638 run_callbacks(:validate_on_create) 662 validate_on_create 639 640 if responds_to?(:validate_on_create) 641 ActiveSupport::Deprecations.warn( 642 "Base#validate_on_create has been deprecated, please use Base.validate :method, :on => :create instead") 643 validate_on_create 644 end 663 645 else 664 646 run_callbacks(:validate_on_update) 665 validate_on_update 647 648 if responds_to?(:validate_on_update) 649 ActiveSupport::Deprecations.warn( 650 "Base#validate_on_update has been deprecated, please use Base.validate :method, :on => :update instead") 651 validate_on_update 652 end 666 653 end 667 654 … … 671 658 # Returns the Errors object that holds all information about attribute error messages. 672 659 def errors 673 @errors ||= Errors.new (self)660 @errors ||= Errors.new 674 661 end 675 676 protected677 # Overwrite this method for validation checks on all saves and use Errors.add(field, msg) for invalid attributes.678 def validate #:doc:679 end680 681 # Overwrite this method for validation checks used only on creation.682 def validate_on_create #:doc:683 end684 685 # Overwrite this method for validation checks used only on updates.686 def validate_on_update # :doc:687 end688 662 end 689 663 end