Skip to content
This repository
Browse code

Merge pull request #4673 from carlosantoniodasilva/validation-guides-…

…update-3-2

Validation guides update 3 2
  • Loading branch information...
commit b0239211411ce45f9cbaf5dfaf2ba28cc93c5a6d 2 parents a267090 + 2d73f70
Vijay Dev authored January 25, 2012
2  activeresource/lib/active_resource/validations.rb
@@ -41,7 +41,7 @@ def from_xml(xml, save_cache = false)
41 41
   # Module to support validation and errors with Active Resource objects. The module overrides
42 42
   # Base#save to rescue ActiveResource::ResourceInvalid exceptions and parse the errors returned
43 43
   # in the web service response. The module also adds an +errors+ collection that mimics the interface
44  
-  # of the errors provided by ActiveRecord::Errors.
  44
+  # of the errors provided by ActiveModel::Errors.
45 45
   #
46 46
   # ==== Example
47 47
   #
4  activesupport/lib/active_support/core_ext/string/inflections.rb
@@ -100,8 +100,8 @@ def titleize
100 100
   #
101 101
   # +underscore+ will also change '::' to '/' to convert namespaces to paths.
102 102
   #
103  
-  #   "ActiveRecord".underscore         # => "active_record"
104  
-  #   "ActiveRecord::Errors".underscore # => active_record/errors
  103
+  #   "ActiveModel".underscore         # => "active_model"
  104
+  #   "ActiveModel::Errors".underscore # => "active_model/errors"
105 105
   def underscore
106 106
     ActiveSupport::Inflector.underscore(self)
107 107
   end
12  activesupport/lib/active_support/inflector/methods.rb
@@ -42,10 +42,10 @@ def singularize(word)
42 42
     # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
43 43
     #
44 44
     # Examples:
45  
-    #   "active_record".camelize                # => "ActiveRecord"
46  
-    #   "active_record".camelize(:lower)        # => "activeRecord"
47  
-    #   "active_record/errors".camelize         # => "ActiveRecord::Errors"
48  
-    #   "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
  45
+    #   "active_model".camelize                # => "ActiveModel"
  46
+    #   "active_model".camelize(:lower)        # => "activeModel"
  47
+    #   "active_model/errors".camelize         # => "ActiveModel::Errors"
  48
+    #   "active_model/errors".camelize(:lower) # => "activeModel::Errors"
49 49
     #
50 50
     # As a rule of thumb you can think of +camelize+ as the inverse of +underscore+,
51 51
     # though there are cases where that does not hold:
@@ -66,8 +66,8 @@ def camelize(term, uppercase_first_letter = true)
66 66
     # Changes '::' to '/' to convert namespaces to paths.
67 67
     #
68 68
     # Examples:
69  
-    #   "ActiveRecord".underscore         # => "active_record"
70  
-    #   "ActiveRecord::Errors".underscore # => active_record/errors
  69
+    #   "ActiveModel".underscore         # => "active_model"
  70
+    #   "ActiveModel::Errors".underscore # => "active_model/errors"
71 71
     #
72 72
     # As a rule of thumb you can think of +underscore+ as the inverse of +camelize+,
73 73
     # though there are cases where that does not hold:
33  railties/guides/source/active_record_validations_callbacks.textile
Source Rendered
@@ -614,7 +614,7 @@ As shown in the example, you can also combine standard validations with your own
614 614
 
615 615
 h4. Custom Methods
616 616
 
617  
-You can also create methods that verify the state of your models and add messages to the +errors+ collection when they are invalid. You must then register these methods by using one or more of the +validate+, +validate_on_create+ or +validate_on_update+ class methods, passing in the symbols for the validation methods' names.
  617
+You can also create methods that verify the state of your models and add messages to the +errors+ collection when they are invalid. You must then register these methods by using the +validate+ class method, passing in the symbols for the validation methods' names.
618 618
 
619 619
 You can pass more than one symbol for each class method and the respective validations will be run in the same order as they were registered.
620 620
 
@@ -637,12 +637,24 @@ class Invoice < ActiveRecord::Base
637 637
 end
638 638
 </ruby>
639 639
 
  640
+By default such validations will run every time you call +valid?+. It is also possible to control when to run these custom validations by giving an +:on+ option to the +validate+ method, with either: +:create+ or +:update+.
  641
+
  642
+<ruby>
  643
+class Invoice < ActiveRecord::Base
  644
+  validate :active_customer, :on => :create
  645
+
  646
+  def active_customer
  647
+    errors.add(:customer_id, "is not active") unless customer.active?
  648
+  end
  649
+end
  650
+</ruby>
  651
+
640 652
 You can even create your own validation helpers and reuse them in several different models. For example, an application that manages surveys may find it useful to express that a certain field corresponds to a set of choices:
641 653
 
642 654
 <ruby>
643 655
 ActiveRecord::Base.class_eval do
644 656
   def self.validates_as_choice(attr_name, n, options={})
645  
-    validates attr_name, :inclusion => { {:in => 1..n}.merge(options) }
  657
+    validates attr_name, :inclusion => { { :in => 1..n }.merge!(options) }
646 658
   end
647 659
 end
648 660
 </ruby>
@@ -659,7 +671,7 @@ h3. Working with Validation Errors
659 671
 
660 672
 In addition to the +valid?+ and +invalid?+ methods covered earlier, Rails provides a number of methods for working with the +errors+ collection and inquiring about the validity of objects.
661 673
 
662  
-The following is a list of the most commonly used methods. Please refer to the +ActiveRecord::Errors+ documentation for a list of all the available methods.
  674
+The following is a list of the most commonly used methods. Please refer to the +ActiveModel::Errors+ documentation for a list of all the available methods.
663 675
 
664 676
 h4(#working_with_validation_errors-errors). +errors+
665 677
 
@@ -889,13 +901,8 @@ Below is a simple example where we change the Rails behavior to always display t
889 901
 
890 902
 <ruby>
891 903
 ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
892  
-  if instance.error_message.kind_of?(Array)
893  
-    %(#{html_tag}<span class="validation-error">&nbsp;
894  
-      #{instance.error_message.join(',')}</span>).html_safe
895  
-  else
896  
-    %(#{html_tag}<span class="validation-error">&nbsp;
897  
-      #{instance.error_message}</span>).html_safe
898  
-  end
  904
+  errors = Array(instance.error_message).join(',')
  905
+  %(#{html_tag}<span class="validation-error">&nbsp;#{errors}</span>).html_safe
899 906
 end
900 907
 </ruby>
901 908
 
@@ -949,6 +956,7 @@ h4. Creating an Object
949 956
 * +before_validation+
950 957
 * +after_validation+
951 958
 * +before_save+
  959
+* +around_save+
952 960
 * +before_create+
953 961
 * +around_create+
954 962
 * +after_create+
@@ -959,6 +967,7 @@ h4. Updating an Object
959 967
 * +before_validation+
960 968
 * +after_validation+
961 969
 * +before_save+
  970
+* +around_save+
962 971
 * +before_update+
963 972
 * +around_update+
964 973
 * +after_update+
@@ -967,8 +976,8 @@ h4. Updating an Object
967 976
 h4. Destroying an Object
968 977
 
969 978
 * +before_destroy+
970  
-* +after_destroy+
971 979
 * +around_destroy+
  980
+* +after_destroy+
972 981
 
973 982
 WARNING. +after_save+ runs both on create and update, but always _after_ the more specific callbacks +after_create+ and +after_update+, no matter the order in which the macro calls were executed.
974 983
 
@@ -1013,7 +1022,7 @@ The following methods trigger callbacks:
1013 1022
 * +increment!+
1014 1023
 * +save+
1015 1024
 * +save!+
1016  
-* +save(false)+
  1025
+* +save(:validate => false)+
1017 1026
 * +toggle!+
1018 1027
 * +update+
1019 1028
 * +update_attribute+
2  railties/guides/source/active_resource_basics.textile
Source Rendered
@@ -71,7 +71,7 @@ person.destroy
71 71
 
72 72
 h3. Validations
73 73
 
74  
-Module to support validation and errors with Active Resource objects. The module overrides Base#save to rescue ActiveResource::ResourceInvalid exceptions and parse the errors returned in the web service response. The module also adds an errors collection that mimics the interface of the errors provided by ActiveRecord::Errors.
  74
+Module to support validation and errors with Active Resource objects. The module overrides Base#save to rescue ActiveResource::ResourceInvalid exceptions and parse the errors returned in the web service response. The module also adds an errors collection that mimics the interface of the errors provided by ActiveModel::Errors.
75 75
 
76 76
 h4. Validating client side resources by overriding validation methods in base class
77 77
 
6  railties/guides/source/i18n.textile
Source Rendered
@@ -819,13 +819,13 @@ h5. Action View Helper Methods
819 819
 
820 820
 * The +number_to_currency+, +number_with_precision+, +number_to_percentage+, +number_with_delimiter+, and +number_to_human_size+ helpers use the number format settings located in the "number":https://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L2 scope.
821 821
 
822  
-h5. Active Record Methods
  822
+h5. Active Model Methods
823 823
 
824 824
 * +model_name.human+ and +human_attribute_name+ use translations for model names and attribute names if available in the "activerecord.models":https://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml#L29 scope. They also support translations for inherited class names (e.g. for use with STI) as explained above in "Error message scopes".
825 825
 
826  
-* +ActiveRecord::Errors#generate_message+ (which is used by Active Record validations but may also be used manually) uses +model_name.human+ and +human_attribute_name+ (see above). It also translates the error message and supports translations for inherited class names as explained above in "Error message scopes".
  826
+* +ActiveModel::Errors#generate_message+ (which is used by Active Model validations but may also be used manually) uses +model_name.human+ and +human_attribute_name+ (see above). It also translates the error message and supports translations for inherited class names as explained above in "Error message scopes".
827 827
 
828  
-* +ActiveRecord::Errors#full_messages+ prepends the attribute name to the error message using a separator that will be looked up from "activerecord.errors.format.separator":https://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L91 (and which defaults to +'&nbsp;'+).
  828
+* +ActiveModel::Errors#full_messages+ prepends the attribute name to the error message using a separator that will be looked up from "errors.format":https://github.com/rails/rails/blob/master/activemodel/lib/active_model/locale/en.yml#L4 (and which defaults to +"%{attribute} %{message}"+).
829 829
 
830 830
 h5. Active Support Methods
831 831
 

0 notes on commit b023921

Please sign in to comment.
Something went wrong with that request. Please try again.