1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Assert validations API inside validatable module.

This commit is contained in:
José Valim 2009-11-24 13:56:54 -02:00
parent 6c3bebe630
commit 52885725a9
4 changed files with 23 additions and 5 deletions

View file

@ -6,9 +6,11 @@ module Devise
#
# # resets the user password and save the record, true if valid passwords are given, otherwise false
# User.find(1).reset_password!('password123', 'password123')
#
# # only resets the user password, without saving the record
# user = User.find(1)
# user.reset_password('password123', 'password123')
#
# # creates a new token and send it with instructions about how to reset the password
# User.find(1).send_reset_password_instructions
module Recoverable

View file

@ -10,7 +10,13 @@ module Devise
# Email regex used to validate email formats. Retrieved from authlogic.
EMAIL_REGEX = /\A[\w\.%\+\-]+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2,4}|museum|travel)\z/i
# 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
attribute = authentication_keys.first
@ -27,6 +33,15 @@ module Devise
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
protected
# Checks whether a password is needed or not. For validations only.

View file

@ -5,11 +5,6 @@ module Devise
klass.send :extend, self
yield
# DataMapper validations have a completely different API
if modules.include?(:validatable) && !klass.respond_to?(:validates_presence_of)
raise ":validatable is not supported in DataMapper, please craft your validations by hand"
end
modules.each do |mod|
klass.send(mod) if klass.respond_to?(mod)
end

View file

@ -96,4 +96,10 @@ class ValidatableTest < ActiveSupport::TestCase
assert user.errors[:password]
assert_not user.errors[:password].to_a.include?('is too short (minimum is 6 characters)')
end
test 'shuold not be included in objects with invalid API' do
assert_raise RuntimeError do
Class.new.send :include, Devise::Models::Validatable
end
end
end