models.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. module Devise
  2. module Models
  3. class MissingAttribute < StandardError
  4. def initialize(attributes)
  5. @attributes = attributes
  6. end
  7. def message
  8. "The following attribute(s) is (are) missing on your model: #{@attributes.join(", ")}"
  9. end
  10. end
  11. # Creates configuration values for Devise and for the given module.
  12. #
  13. # Devise::Models.config(Devise::Authenticatable, :stretches, 10)
  14. #
  15. # The line above creates:
  16. #
  17. # 1) An accessor called Devise.stretches, which value is used by default;
  18. #
  19. # 2) Some class methods for your model Model.stretches and Model.stretches=
  20. # which have higher priority than Devise.stretches;
  21. #
  22. # 3) And an instance method stretches.
  23. #
  24. # To add the class methods you need to have a module ClassMethods defined
  25. # inside the given class.
  26. #
  27. def self.config(mod, *accessors) #:nodoc:
  28. class << mod; attr_accessor :available_configs; end
  29. mod.available_configs = accessors
  30. accessors.each do |accessor|
  31. mod.class_eval <<-METHOD, __FILE__, __LINE__ + 1
  32. def #{accessor}
  33. if defined?(@#{accessor})
  34. @#{accessor}
  35. elsif superclass.respond_to?(:#{accessor})
  36. superclass.#{accessor}
  37. else
  38. Devise.#{accessor}
  39. end
  40. end
  41. def #{accessor}=(value)
  42. @#{accessor} = value
  43. end
  44. METHOD
  45. end
  46. end
  47. def self.check_fields!(klass)
  48. failed_attributes = []
  49. instance = klass.new
  50. klass.devise_modules.each do |mod|
  51. constant = const_get(mod.to_s.classify)
  52. if constant.respond_to?(:required_fields)
  53. constant.required_fields(klass).each do |field|
  54. failed_attributes << field unless instance.respond_to?(field)
  55. end
  56. else
  57. ActiveSupport::Deprecation.warn "The module #{mod} doesn't implement self.required_fields(klass). " \
  58. "Devise uses required_fields to warn developers of any missing fields in their models. " \
  59. "Please implement #{mod}.required_fields(klass) that returns an array of symbols with the required fields."
  60. end
  61. end
  62. if failed_attributes.any?
  63. fail Devise::Models::MissingAttribute.new(failed_attributes)
  64. end
  65. end
  66. # Include the chosen devise modules in your model:
  67. #
  68. # devise :database_authenticatable, :confirmable, :recoverable
  69. #
  70. # You can also give any of the devise configuration values in form of a hash,
  71. # with specific values for this model. Please check your Devise initializer
  72. # for a complete description on those values.
  73. #
  74. def devise(*modules)
  75. options = modules.extract_options!.dup
  76. selected_modules = modules.map(&:to_sym).uniq.sort_by do |s|
  77. Devise::ALL.index(s) || -1 # follow Devise::ALL order
  78. end
  79. devise_modules_hook! do
  80. include Devise::Models::Authenticatable
  81. selected_modules.each do |m|
  82. if m == :encryptable && !(defined?(Devise::Models::Encryptable))
  83. warn "[DEVISE] You're trying to include :encryptable in your model but it is not bundled with the Devise gem anymore. Please add `devise-encryptable` to your Gemfile to proceed.\n"
  84. end
  85. mod = Devise::Models.const_get(m.to_s.classify)
  86. if mod.const_defined?("ClassMethods")
  87. class_mod = mod.const_get("ClassMethods")
  88. extend class_mod
  89. if class_mod.respond_to?(:available_configs)
  90. available_configs = class_mod.available_configs
  91. available_configs.each do |config|
  92. next unless options.key?(config)
  93. send(:"#{config}=", options.delete(config))
  94. end
  95. end
  96. end
  97. include mod
  98. end
  99. self.devise_modules |= selected_modules
  100. options.each { |key, value| send(:"#{key}=", value) }
  101. end
  102. end
  103. # The hook which is called inside devise.
  104. # So your ORM can include devise compatibility stuff.
  105. def devise_modules_hook!
  106. yield
  107. end
  108. end
  109. end
  110. require 'devise/models/authenticatable'