One exception to rule them all

This commit is contained in:
Rodrigo Flores 2012-02-21 10:31:38 -02:00
parent e3412d4207
commit bc11e9f300
3 changed files with 33 additions and 4 deletions

View File

@ -43,13 +43,19 @@ module Devise
end
def self.check_fields!(klass)
failed_attributes = []
klass.devise_modules.each do |mod|
instance = klass.new
const_get(mod.to_s.classify).required_fields(klass).each do |field|
fail Devise::Models::MissingAttribute unless instance.respond_to?(field)
failed_attributes << field unless instance.respond_to?(field)
end
end
if failed_attributes.any?
fail Devise::Models::MissingAttribute, "The following attributes are missing on your model: #{failed_attributes.join(", ")}"
end
end
# Include the chosen devise modules in your model:

View File

@ -126,7 +126,7 @@ class CheckFieldsTest < ActiveSupport::TestCase
end
end
test 'raises Devise::Models::MissingAtrribute if the class doesn\'t respond_to one of the attributes' do
test 'raises Devise::Models::MissingAtrribute and shows the missing attribute if the class doesn\'t respond_to one of the attributes' do
Clown = Class.new do
extend Devise::Models
@ -138,8 +138,23 @@ class CheckFieldsTest < ActiveSupport::TestCase
attr_accessor :encrypted_password
end
assert_raise Devise::Models::MissingAttribute do
assert_raise_with_message Devise::Models::MissingAttribute, "The following attributes are missing on your model: email" do
Devise::Models.check_fields!(Clown)
end
end
end
test 'raises Devise::Models::MissingAtrribute with all the missing attributes if there is more than one' do
Magician = Class.new do
extend Devise::Models
def self.before_validation(instance)
end
devise :database_authenticatable
end
exception = assert_raise_with_message Devise::Models::MissingAttribute, "The following attributes are missing on your model: encrypted_password, email" do
Devise::Models.check_fields!(Magician)
end
end
end

View File

@ -32,4 +32,12 @@ class ActiveSupport::TestCase
assert !element.nil?, "the arrays doesn't have the same content"
end
end
def assert_raise_with_message(exception_klass, message)
exception = assert_raise exception_klass do
yield
end
assert_equal exception.message, message, "The expected message was #{message} but your exception throwed #{exception.message}"
end
end