lib/devise/models.rb in devise-2.0.6 vs lib/devise/models.rb in devise-2.1.0.rc

- old
+ new

@@ -1,7 +1,17 @@ module Devise module Models + class MissingAttribute < StandardError + def initialize(attributes) + @attributes = attributes + end + + def message + "The following attribute(s) is (are) missing on your model: #{@attributes.join(", ")}" + end + end + # Creates configuration values for Devise and for the given module. # # Devise::Models.config(Devise::Authenticatable, :stretches, 10) # # The line above creates: @@ -37,10 +47,30 @@ end METHOD end end + def self.check_fields!(klass) + failed_attributes = [] + + 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? + fail Devise::Models::MissingAttribute.new(failed_attributes) + end + end + # Include the chosen devise modules in your model: # # devise :database_authenticatable, :confirmable, :recoverable # # You can also give any of the devise configuration values in form of a hash, @@ -64,11 +94,11 @@ extend class_mod if class_mod.respond_to?(:available_configs) available_configs = class_mod.available_configs available_configs.each do |config| - next unless options.key?(config) + next unless options.key?(config) send(:"#{config}=", options.delete(config)) end end end @@ -86,6 +116,6 @@ yield end end end -require 'devise/models/authenticatable' \ No newline at end of file +require 'devise/models/authenticatable'