Class: Orthoses::ActiveModel::HasSecurePassword

Inherits:
Object
  • Object
show all
Defined in:
lib/orthoses/active_model/has_secure_password.rb

Overview

< 6.0

def has_secure_password(options = {})

>= 6.0

def has_secure_password(attribute = :password, validations: true)

Instance Method Summary collapse

Constructor Details

#initialize(loader, if: nil) ⇒ HasSecurePassword

Returns a new instance of HasSecurePassword.



10
11
12
13
# File 'lib/orthoses/active_model/has_secure_password.rb', line 10

def initialize(loader, if: nil)
  @loader = loader
  @if = binding.local_variable_get(:if)
end

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/orthoses/active_model/has_secure_password.rb', line 15

def call
  call_tracer = CallTracer::Lazy.new

  store = call_tracer.trace('ActiveModel::SecurePassword::ClassMethods#has_secure_password') do
    @loader.call
  end

  call_tracer.captures.each do |capture|
    next unless capture.method.receiver.kind_of?(Class)
    base_name = Utils.module_name(capture.method.receiver)
    next unless base_name

    attribute = capture.argument[:attribute] || :password
    full_name = if ::ActiveModel::VERSION::MAJOR < 6
      "ActiveModel::SecurePassword::InstanceMethodsOnActivation"
    else
      "#{base_name}::ActiveModel_SecurePassword_InstanceMethodsOnActivation_#{attribute}"
    end

    lines = []
    lines << "attr_reader #{attribute}: String?"
    lines << "def #{attribute}=: (String) -> String"
    lines << "def #{attribute}_confirmation=: (String) -> String"
    lines << "def authenticate_#{attribute}: (String) -> (#{base_name} | false)"
    if attribute == :password
      lines << "alias authenticate authenticate_password"
    end
    store[full_name].header = "module #{full_name}"
    store[full_name].concat(lines)
    store[base_name] << "include #{full_name}"
  end

  store
end