Sha256: 7996a69b4e201d6c6c1b56ac74b81da0bdee9c393b7cea1701c2835fcb1247cc

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

module DataMapper
  module Validations
    
    class ConfirmationValidator < GenericValidator
      
      ERROR_MESSAGES = {
        :confirmation => '#{field} does not match the confirmation'
      }
      
      def initialize(field_name, options = {})
        @options = options
        @field_name, @confirm_field_name = field_name, (options[:confirm] || "#{field_name}_confirmation").to_sym
      end
      
      def call(target)
        unless valid?(target)
          error_message = '%s does not match the confirmation'.t(Inflector.humanize(@field_name))
          add_error(target, error_message , @field_name)
          return false
        end
        
        return true
      end
      
      def valid?(target)
        field_value = target.instance_variable_get("@#{@field_name}")
        return true if @options[:allow_nil] && field_value.nil?

        confirm_value = target.instance_variable_get("@#{@confirm_field_name}")
        field_value == confirm_value
      end
      
    end
    
    module ValidatesConfirmationOf
      def self.included(base)
        base.extend(ClassMethods)
      end
      
      module ClassMethods
        # No bueno?
        DEFAULT_OPTIONS = { :on => :save }

        def validates_confirmation_of(field, options = {})
          opts = retrieve_options_from_arguments_for_validators([options], DEFAULT_OPTIONS)
          validations.context(opts[:context]) << Validations::ConfirmationValidator.new(field, opts)
        end

      end
    end
    
  end  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
datamapper-0.2.3 lib/data_mapper/validations/confirmation_validator.rb