Deprecation warning on module doesn't have a required_fields method

This commit is contained in:
Rodrigo Flores 2012-03-03 14:30:53 -03:00
parent ba80074b7b
commit ecfc7d752a
3 changed files with 46 additions and 4 deletions

View File

@ -161,4 +161,4 @@ MESSAGE
super
end
end
end
end

View File

@ -55,8 +55,12 @@ module Devise
klass.devise_modules.each do |mod|
instance = klass.new
const_get(mod.to_s.classify).required_fields(klass).each do |field|
failed_attributes << field unless instance.respond_to?(field)
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
@ -114,4 +118,4 @@ module Devise
end
end
require 'devise/models/authenticatable'
require 'devise/models/authenticatable'

View File

@ -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