Sha256: 534840b8c9fd7beaa8f84140e51dea6c079853161169f1c942de4e347a47c9a6

Contents?: true

Size: 1.55 KB

Versions: 6

Compression:

Stored size: 1.55 KB

Contents

module Devise
  module Models

    # Validatable creates all needed validations for a user email and password.
    # It's optional, given you may want to create the validations by yourself.
    # Automatically validate if the email is present, unique and it's format is
    # valid. Also tests presence of password, confirmation and length
    module Validatable
      # All validations used by this module.
      VALIDATIONS = [ :validates_presence_of, :validates_uniqueness_of, :validates_format_of,
                      :validates_confirmation_of, :validates_length_of ].freeze

      def self.included(base)
        assert_validations_api!(base)

        base.class_eval do
          validates_presence_of   :email
          validates_uniqueness_of :email, :scope => authentication_keys[1..-1], :allow_blank => true
          validates_format_of     :email, :with  => EMAIL_REGEX, :allow_blank => true

          with_options :if => :password_required? do |v|
            v.validates_presence_of     :password
            v.validates_confirmation_of :password
            v.validates_length_of       :password, :within => 6..20, :allow_blank => true
          end
        end
      end

      def self.assert_validations_api!(base) #:nodoc:
        unavailable_validations = VALIDATIONS.select { |v| !base.respond_to?(v) }

        unless unavailable_validations.empty?
          raise "Could not use :validatable module since #{base} does not respond " <<
                "to the following methods: #{unavailable_validations.to_sentence}."
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 3 rubygems

Version Path
dcu-devise-1.0.7 lib/devise/models/validatable.rb
devise-1.0.8 lib/devise/models/validatable.rb
ivanvc-devise-1.0.7.1 lib/devise/models/validatable.rb
devise-1.0.7 lib/devise/models/validatable.rb
devise-1.0.6 lib/devise/models/validatable.rb
devise-1.0.5 lib/devise/models/validatable.rb