Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source
Show
Ignore:
Timestamp:
06/23/07 17:29:54 (1 year ago)
Author:
david
Message:

Big documentation upgrade for ARes (closes #8694) [jeremymcanally]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activeresource/lib/active_resource/validations.rb

    r5962 r7098  
    1515    end 
    1616 
     17    # Add an error to the base Active Resource object rather than an attribute. 
     18    # 
     19    # ==== Examples 
     20    #   my_folder = Folder.find(1) 
     21    #   my_folder.errors.add_to_base("You can't edit an existing folder") 
     22    #   my_folder.errors.on_base 
     23    #   # => "You can't edit an existing folder" 
     24    # 
     25    #   my_folder.errors.add_to_base("This folder has been tagged as frozen") 
     26    #   my_folder.valid? 
     27    #   # => false 
     28    #   my_folder.errors.on_base 
     29    #   # => ["You can't edit an existing folder", "This folder has been tagged as frozen"] 
     30    # 
    1731    def add_to_base(msg) 
    1832      add(:base, msg) 
    1933    end 
    2034 
     35    # Adds an error to an Active Resource object's attribute (named for the +attribute+ parameter) 
     36    # with the error message in +msg+. 
     37    # 
     38    # ==== Examples 
     39    #   my_resource = Node.find(1) 
     40    #   my_resource.errors.add('name', 'can not be "base"') if my_resource.name == 'base' 
     41    #   my_resource.errors.on('name') 
     42    #   # => 'can not be "base"!' 
     43    # 
     44    #   my_resource.errors.add('desc', 'can not be blank') if my_resource.desc == '' 
     45    #   my_resource.valid? 
     46    #   # => false 
     47    #   my_resource.errors.on('desc') 
     48    #   # => 'can not be blank!' 
     49    # 
    2150    def add(attribute, msg) 
    2251      @errors[attribute.to_s] = [] if @errors[attribute.to_s].nil? 
     
    2554 
    2655    # Returns true if the specified +attribute+ has errors associated with it. 
     56    # 
     57    # ==== Examples 
     58    #   my_resource = Disk.find(1) 
     59    #   my_resource.errors.add('location', 'must be Main') unless my_resource.location == 'Main' 
     60    #   my_resource.errors.on('location') 
     61    #   # => 'must be Main!' 
     62    # 
     63    #   my_resource.errors.invalid?('location') 
     64    #   # => true 
     65    #   my_resource.errors.invalid?('name') 
     66    #   # => false 
    2767    def invalid?(attribute) 
    2868      !@errors[attribute.to_s].nil? 
    2969    end 
    3070 
    31     # * Returns nil, if no errors are associated with the specified +attribute+. 
    32     # * Returns the error message, if one error is associated with the specified +attribute+. 
    33     # * Returns an array of error messages, if more than one error is associated with the specified +attribute+. 
     71    # A method to return the errors associated with +attribute+, which returns nil, if no errors are  
     72    # associated with the specified +attribute+, the error message if one error is associated with the specified +attribute+, 
     73    # or an array of error messages if more than one error is associated with the specified +attribute+. 
     74    # 
     75    # ==== Examples 
     76    #   my_person = Person.new(params[:person]) 
     77    #   my_person.errors.on('login') 
     78    #   # => nil 
     79    # 
     80    #   my_person.errors.add('login', 'can not be empty') if my_person.login == '' 
     81    #   my_person.errors.on('login') 
     82    #   # => 'can not be empty' 
     83    # 
     84    #   my_person.errors.add('login', 'can not be longer than 10 characters') if my_person.login.length > 10 
     85    #   my_person.errors.on('login') 
     86    #   # => ['can not be empty', 'can not be longer than 10 characters'] 
    3487    def on(attribute) 
    3588      errors = @errors[attribute.to_s] 
     
    4093    alias :[] :on 
    4194 
    42     # Returns errors assigned to base object through add_to_base according to the normal rules of on(attribute). 
     95    # A method to return errors assigned to +base+ object through add_to_base, which returns nil, if no errors are  
     96    # associated with the specified +attribute+, the error message if one error is associated with the specified +attribute+, 
     97    # or an array of error messages if more than one error is associated with the specified +attribute+. 
     98    # 
     99    # ==== Examples 
     100    #   my_account = Account.find(1) 
     101    #   my_account.errors.on_base 
     102    #   # => nil 
     103    # 
     104    #   my_account.errors.add_to_base("This account is frozen") 
     105    #   my_account.errors.on_base 
     106    #   # => "This account is frozen" 
     107    # 
     108    #   my_account.errors.add_to_base("This account has been closed") 
     109    #   my_account.errors.on_base 
     110    #   # => ["This account is frozen", "This account has been closed"] 
     111    # 
    43112    def on_base 
    44113      on(:base) 
     
    46115 
    47116    # Yields each attribute and associated message per error added. 
     117    # 
     118    # ==== Examples 
     119    #   my_person = Person.new(params[:person]) 
     120    # 
     121    #   my_person.errors.add('login', 'can not be empty') if my_person.login == '' 
     122    #   my_person.errors.add('password', 'can not be empty') if my_person.password == '' 
     123    #   messages = '' 
     124    #   my_person.errors.each {|attr, msg| messages += attr.humanize + " " + msg + "<br />"} 
     125    #   messages 
     126    #   # => "Login can not be empty<br />Password can not be empty<br />" 
     127    # 
    48128    def each 
    49129      @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } } 
     
    52132    # Yields each full error message added. So Person.errors.add("first_name", "can't be empty") will be returned 
    53133    # through iteration as "First name can't be empty". 
     134    # 
     135    # ==== Examples 
     136    #   my_person = Person.new(params[:person]) 
     137    # 
     138    #   my_person.errors.add('login', 'can not be empty') if my_person.login == '' 
     139    #   my_person.errors.add('password', 'can not be empty') if my_person.password == '' 
     140    #   messages = '' 
     141    #   my_person.errors.each_full {|msg| messages += msg + "<br/>"} 
     142    #   messages 
     143    #   # => "Login can not be empty<br />Password can not be empty<br />" 
     144    # 
    54145    def each_full 
    55146      full_messages.each { |msg| yield msg } 
     
    57148 
    58149    # Returns all the full error messages in an array. 
     150    # 
     151    # ==== Examples 
     152    #   my_person = Person.new(params[:person]) 
     153    # 
     154    #   my_person.errors.add('login', 'can not be empty') if my_person.login == '' 
     155    #   my_person.errors.add('password', 'can not be empty') if my_person.password == '' 
     156    #   messages = '' 
     157    #   my_person.errors.full_messages.each {|msg| messages += msg + "<br/>"} 
     158    #   messages 
     159    #   # => "Login can not be empty<br />Password can not be empty<br />" 
     160    # 
    59161    def full_messages 
    60162      full_messages = [] 
     
    80182    # Returns the total number of errors added. Two errors added to the same attribute will be counted as such 
    81183    # with this as well. 
     184    # 
     185    # ==== Examples 
     186    #   my_person = Person.new(params[:person]) 
     187    #   my_person.errors.size 
     188    #   # => 0 
     189    # 
     190    #   my_person.errors.add('login', 'can not be empty') if my_person.login == '' 
     191    #   my_person.errors.add('password', 'can not be empty') if my_person.password == '' 
     192    #   my_person.error.size 
     193    #   # => 2 
     194    # 
    82195    def size 
    83196      @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size } 
     
    87200    alias_method :length, :size 
    88201     
     202    # Grabs errors from the XML response. 
    89203    def from_xml(xml) 
    90204      clear 
     
    103217  end 
    104218   
    105   # Module to allow validation of ActiveResource objects, which are implemented by overriding +Base#validate+ or its variants.  
    106   # Each of these methods can inspect the state of the object, which usually means ensuring that a number of  
    107   # attributes have a certain value (such as not empty, within a given range, matching a certain regular expression). For example: 
     219  # Module to allow validation of ActiveResource objects, which creates an Errors instance for every resource. 
     220  # Methods are implemented by overriding +Base#validate+ or its variants   Each of these methods can inspect  
     221  # the state of the object, which usually means  ensuring that a number of attributes have a certain value  
     222  # (such as not empty, within a given range, matching a certain regular expression and so on). 
     223  # 
     224  # ==== Example 
    108225  # 
    109226  #   class Person < ActiveResource::Base 
     
    134251  #   person.save                         # => true (and person is now saved to the remote service) 
    135252  # 
    136   # An Errors object is automatically created for every resource. 
    137253  module Validations 
    138254    def self.included(base) # :nodoc: 
     
    142258    end 
    143259 
     260    # Validate a resource and save (POST) it to the remote web service. 
    144261    def save_with_validation 
    145262      save_without_validation 
     
    150267    end 
    151268 
     269    # Checks for errors on an object (i.e., is resource.errors empty?). 
     270    #  
     271    # ==== Examples 
     272    #   my_person = Person.create(params[:person]) 
     273    #   my_person.valid? 
     274    #   # => true 
     275    # 
     276    #   my_person.errors.add('login', 'can not be empty') if my_person.login == '' 
     277    #   my_person.valid? 
     278    #   # => false 
    152279    def valid? 
    153280      errors.empty?