mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
Added check_model! method
This commit is contained in:
parent
e3df7f033e
commit
9667a38bc9
3 changed files with 57 additions and 0 deletions
|
@ -1,5 +1,8 @@
|
|||
module Devise
|
||||
module Models
|
||||
class MissingAttribute < StandardError
|
||||
end
|
||||
|
||||
# Creates configuration values for Devise and for the given module.
|
||||
#
|
||||
# Devise::Models.config(Devise::Authenticatable, :stretches, 10)
|
||||
|
@ -39,6 +42,16 @@ module Devise
|
|||
end
|
||||
end
|
||||
|
||||
def self.check_fields!(klass)
|
||||
klass.devise_modules.each do |mod|
|
||||
instance = klass.new
|
||||
|
||||
const_get(mod.to_s.classify)::ModuleMethods.required_fields.each do |field|
|
||||
raise Devise::Models::MissingAttribute unless instance.respond_to?(field)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Include the chosen devise modules in your model:
|
||||
#
|
||||
# devise :database_authenticatable, :confirmable, :recoverable
|
||||
|
|
|
@ -27,6 +27,14 @@ module Devise
|
|||
attr_accessor :password_confirmation
|
||||
end
|
||||
|
||||
module ModuleMethods
|
||||
extend self
|
||||
|
||||
def required_fields
|
||||
[:encrypted_password, :email]
|
||||
end
|
||||
end
|
||||
|
||||
# Generates password encryption based on the given value.
|
||||
def password=(new_password)
|
||||
@password = new_password
|
||||
|
|
|
@ -107,3 +107,39 @@ class ActiveRecordTest < ActiveSupport::TestCase
|
|||
Admin.create!
|
||||
end
|
||||
end
|
||||
|
||||
class CheckFieldsTest < ActiveSupport::TestCase
|
||||
test 'checks if the class respond_to the required fields' do
|
||||
Player = Class.new do
|
||||
extend Devise::Models
|
||||
|
||||
def self.before_validation(x)
|
||||
end
|
||||
|
||||
devise :database_authenticatable
|
||||
|
||||
attr_accessor :encrypted_password, :email
|
||||
end
|
||||
|
||||
assert_nothing_raised Devise::Models::MissingAttribute do
|
||||
Devise::Models.check_fields!(Player)
|
||||
end
|
||||
end
|
||||
|
||||
test 'raises Devise::Models::MissingAtrribute if the class doesn\'t respond_to one of the attributes' do
|
||||
Clown = Class.new do
|
||||
extend Devise::Models
|
||||
|
||||
def self.before_validation(instance)
|
||||
end
|
||||
|
||||
devise :database_authenticatable
|
||||
|
||||
attr_accessor :encrypted_password
|
||||
end
|
||||
|
||||
assert_raise Devise::Models::MissingAttribute do
|
||||
Devise::Models.check_fields!(Clown)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue