Module: Revise::Models

Defined in:
lib/revise.rb,
lib/revise/models.rb,
lib/revise/models/invitable.rb,
lib/revise/models/confirmable.rb,
lib/revise/models/recoverable.rb,
lib/revise/models/authenticatable.rb,
lib/revise/models/database_authenticatable.rb

Defined Under Namespace

Modules: Authenticatable, Confirmable, DatabaseAuthenticatable, Invitable, Recoverable Classes: MissingAttribute

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_fields!(klass) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/revise/models.rb', line 36

def self.check_fields!(klass)
  failed_attributes = []
  instance = klass.new

  klass.revise_modules.each do |mod|
    constant = const_get(mod.to_s.classify)

    if constant.respond_to?(:required_fields)
      constant.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 Revise::Models::MissingAttribute.new(failed_attributes)
  end
end

.config(mod, *accessors) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/revise/models.rb', line 13

def self.config(mod, *accessors)
  class << mod; attr_accessor :available_configs; end
  mod.available_configs = accessors

  accessors.each do |accessor|
    mod.class_eval <<-METHOD, __FILE__, __LINE__ + 1
      def #{accessor}
        if defined?(@#{accessor})
          @#{accessor}
        elsif superclass.respond_to?(:#{accessor})
          superclass.#{accessor}
        else
          Revise.#{accessor}
        end
      end

      def #{accessor}=(value)
        @#{accessor} = value
      end
    METHOD
  end
end

Instance Method Details

#revise(*modules) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/revise/models.rb', line 59

def revise(*modules)
  options = modules.extract_options!.dup

  plural_name = self.model_name.plural.to_sym
  Revise::MODULES[plural_name] = []

  revise_modules_hook! do
    include Revise::Models::Authenticatable

    modules.each do |m|
      mod = Revise::Models.const_get(m.to_s.classify)

      if mod.const_defined?("ClassMethods")
        class_mod = mod.const_get("ClassMethods")
        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)
            send(:"#{config}=", options.delete(config))
          end
        end
      end

      include mod

      Revise::MODULES[plural_name] << m.to_s.classify
    end

    options.each { |key, value| send(:"#{key}=", value) }
  end
end

#revise_modules_hook!Object



93
94
95
# File 'lib/revise/models.rb', line 93

def revise_modules_hook!
  yield
end