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

Changeset 109

Show
Ignore:
Timestamp:
12/10/04 16:46:41 (5 years ago)
Author:
david
Message:

Options for the new validation methods are now given as a hash

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

    r108 r109  
    325325      def quote(value, column = nil) 
    326326        case value 
    327           when String                then %('#{quote_string(value)}') # ' (for ruby-mode) 
     327          when String                then "'#{quote_string(value)}'" # ' (for ruby-mode) 
    328328          when NilClass              then "NULL" 
    329329          when TrueClass             then (column && column.type == :boolean ? "'t'" : "1") 
  • trunk/activerecord/lib/active_record/validations.rb

    r108 r109  
    6262      #     class Person < ActiveRecord::Base 
    6363      #       validate_confirmation :user_name, :password 
    64       #       validate_confirmation :email_address, "should match confirmation" 
     64      #       validate_confirmation :email_address, :message => "should match confirmation" 
    6565      #     end 
    6666      # 
     
    7373      # See validate_confirmation_on_create and validate_confirmation_on_update if you want to restrict the validation to just one of the two 
    7474      # situations. 
     75      # 
     76      # Configuration options: 
     77      # ::message: Specifies a custom error message (default is: "doesn't match confirmation") 
    7578      def validate_confirmation(*attr_names) 
    76         error_message = attr_names.last.is_a?(String) ? attr_names.pop : "doesn't match confirmation" 
     79        configuration = { :message => "doesn't match confirmation" } 
     80        configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 
     81 
    7782        validation_method = block_given? ? yield : "validate" 
    7883 
    7984        for attr_name in attr_names 
    8085          attr_accessor "#{attr_name}_confirmation" 
    81           class_eval(%(#{validation_method} %{errors.add('#{attr_name}', "#{error_message}") unless #{attr_name} == #{attr_name}_confirmation})) 
     86          class_eval(%(#{validation_method} %{errors.add('#{attr_name}', "#{configuration[:message]}") unless #{attr_name} == #{attr_name}_confirmation})) 
    8287        end 
    8388      end 
     
    98103      #     class Person < ActiveRecord::Base 
    99104      #       validate_acceptance :terms_of_service 
    100       #       validate_acceptance :eula, "must be abided" 
     105      #       validate_acceptance :eula, :message => "must be abided" 
    101106      #     end 
    102107      # 
     
    108113      # situations. 
    109114      # 
     115      # Configuration options: 
     116      # ::message: Specifies a custom error message (default is: "must be accepted") 
     117      # 
    110118      # NOTE: The agreement is considered valid if it's set to the string "1". This makes it easy to relate it to an HTML checkbox. 
    111119      def validate_acceptance(*attr_names) 
    112         error_message = attr_names.last.is_a?(String) ? attr_names.pop : "must be accepted" 
     120        configuration = { :message => "must be accepted" } 
     121        configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 
     122 
    113123        validation_method = block_given? ? yield : "validate" 
    114124         
    115125        for attr_name in attr_names 
    116126          attr_accessor(attr_name) 
    117           class_eval(%(#{validation_method} %{errors.add('#{attr_name}', '#{error_message}') unless #{attr_name} == "1"})) 
     127          class_eval(%(#{validation_method} %{errors.add('#{attr_name}', '#{configuration[:message]}') unless #{attr_name} == "1"})) 
    118128        end 
    119129      end 
     
    130140 
    131141      def validate_presence(*attr_names) 
    132         error_message = attr_names.last.is_a?(String) ? attr_names.pop : "can't be empty" 
     142        configuration = { :message => "can't be empty" } 
     143        configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 
     144 
    133145        validation_method = block_given? ? yield : "validate" 
    134146 
    135147        for attr_name in attr_names 
    136           class_eval(%(#{validation_method} %{errors.add_on_empty('#{attr_name}', "#{error_message}")})) 
     148          class_eval(%(#{validation_method} %{errors.add_on_empty('#{attr_name}', "#{configuration[:message]}")})) 
    137149        end 
    138150      end 
     
    161173      # When the record is created, a check is performed to make sure that no record exist in the database with the given value for the specified 
    162174      # attribute (that maps to a column). When the record is updated, the same check is made but disregarding the record itself. 
     175      # 
     176      # Configuration options: 
     177      # ::message: Specifies a custom error message (default is: "has already been taken") 
    163178      def validate_uniqueness(*attr_names) 
    164         error_message = attr_names.last.is_a?(String) ? attr_names.pop : "has already been taken" 
     179        configuration = { :message => "has already been taken" } 
     180        configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 
    165181 
    166182        for attr_name in attr_names 
    167           class_eval(%(validate %{errors.add("#{attr_name}", "#{error_message}") if self.class.find_first(new_record? ? ["#{attr_name} = ?", #{attr_name}] : ["#{attr_name} = ? AND id <> ?", #{attr_name}, id])})) 
     183          class_eval(%(validate %{errors.add("#{attr_name}", "#{configuration[:message]}") if self.class.find_first(new_record? ? ["#{attr_name} = ?", #{attr_name}] : ["#{attr_name} = ? AND id <> ?", #{attr_name}, id])})) 
    168184        end 
    169185      end 
  • trunk/activerecord/test/validations_test.rb

    r108 r109  
    150150 
    151151  def test_eula 
    152     Topic.validate_acceptance_on_create(:eula, "must be abided") 
     152    Topic.validate_acceptance_on_create(:eula, :message => "must be abided") 
    153153 
    154154    t = Topic.create("title" => "We should be confirmed")