Deprecation warning on module doesn't have a required_fields method
This commit is contained in:
parent
ba80074b7b
commit
ecfc7d752a
|
@ -55,9 +55,13 @@ module Devise
|
|||
klass.devise_modules.each do |mod|
|
||||
instance = klass.new
|
||||
|
||||
if const_get(mod.to_s.classify).respond_to?(:required_fields)
|
||||
const_get(mod.to_s.classify).required_fields(klass).each do |field|
|
||||
failed_attributes << field unless instance.respond_to?(field)
|
||||
end
|
||||
else
|
||||
ActiveSupport::Deprecation.warn "The module #{mod} doesn't implement self.required_fields(klass). Devise uses required_fields to warn developers of any missing fields in their models. Please implement #{mod}.required_fields(klass) that returns an array of symbols with the required fields."
|
||||
end
|
||||
end
|
||||
|
||||
if failed_attributes.any?
|
||||
|
|
|
@ -108,6 +108,27 @@ class ActiveRecordTest < ActiveSupport::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def swap_module_method_existence(klass, method)
|
||||
klass.module_eval %Q[
|
||||
class << self
|
||||
alias #{method}_referenced #{method}
|
||||
undef #{method}
|
||||
end
|
||||
]
|
||||
|
||||
begin
|
||||
yield if block_given?
|
||||
ensure
|
||||
|
||||
klass.module_eval %Q[
|
||||
class << self
|
||||
alias #{method} #{method}_referenced
|
||||
undef #{method}_referenced
|
||||
end
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
class CheckFieldsTest < ActiveSupport::TestCase
|
||||
test 'checks if the class respond_to the required fields' do
|
||||
Player = Class.new do
|
||||
|
@ -157,4 +178,21 @@ class CheckFieldsTest < ActiveSupport::TestCase
|
|||
Devise::Models.check_fields!(Magician)
|
||||
end
|
||||
end
|
||||
|
||||
test "doesn't raises a NoMethodError exception when the module doesn't have a required_field(klass) class method" do
|
||||
Driver = Class.new do
|
||||
extend Devise::Models
|
||||
|
||||
def self.before_validation(instance)
|
||||
end
|
||||
|
||||
devise :database_authenticatable
|
||||
end
|
||||
|
||||
swap_module_method_existence Devise::Models::DatabaseAuthenticatable, :required_fields do
|
||||
assert_deprecated "DEPRECATION WARNING: The module database_authenticatable doesn't implement self.required_fields(klass). Devise uses required_fields to warn developers of any missing fields in their models. Please implement database_authenticatable.required_fields(klass) that returns an array of symbols with the required fields." do
|
||||
Devise::Models.check_fields!(Driver)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue