Class: Masks::Adapters::ActiveRecord

Inherits:
Object
  • Object
show all
Includes:
Masks::Adapter
Defined in:
app/models/masks/adapters/active_record.rb

Overview

ActiveRecord adapter for masks.

Although this is designed for masks built-in models, any models adhering to the same interface can be used. It is also possible to override and extend the models used in the configuration.

See Also:

Instance Method Summary collapse

Methods included from Masks::Adapter

#initialize

Instance Method Details

#build_actor(session, **opts) ⇒ Object



56
57
58
59
60
61
62
# File 'app/models/masks/adapters/active_record.rb', line 56

def build_actor(session, **opts)
  opts[:session] = session
  record =
    session.mask.actor_scope.new(session:, nickname: opts[:nickname])
  record.emails.build(email: opts[:email]) if opts[:email]
  record
end

#build_recovery(session, **opts) ⇒ Object



80
81
82
# File 'app/models/masks/adapters/active_record.rb', line 80

def build_recovery(session, **opts)
  @config.model(:recovery).new(configuration: @config, session:, **opts)
end

#expire_actorsObject



64
65
66
# File 'app/models/masks/adapters/active_record.rb', line 64

def expire_actors
  @config.model(:actor).expired.destroy_all
end

#expire_recoveriesObject



68
69
70
# File 'app/models/masks/adapters/active_record.rb', line 68

def expire_recoveries
  @config.model(:recovery).expired.destroy_all
end

#find_actor(session, **opts) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/masks/adapters/active_record.rb', line 40

def find_actor(session, **opts)
  if opts[:email]
    session
      .mask
      .actor_scope
      .includes(:emails)
      .find_by(emails: { email: opts[:email]&.downcase, verified: true })
  elsif opts[:nickname]
    session.mask.actor_scope.find_by(nickname: opts[:nickname])
  end
end

#find_actors(session, ids) ⇒ Object



52
53
54
# File 'app/models/masks/adapters/active_record.rb', line 52

def find_actors(session, ids)
  session.mask.actor_scope.where(nickname: ids).to_a
end

#find_device(session, actor: nil, key: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/masks/adapters/active_record.rb', line 19

def find_device(session, actor: nil, key: nil)
  device = session.device
  actor ||= session.actor

  return unless device.known? && actor

  key ||=
    Digest::SHA512.hexdigest(
      [
        device.name,
        device.os_name,
        device.device_name,
        device.device_type
      ].compact.join("-")
    )

  record = Masks::Rails::Device.find_or_initialize_by(actor:, key:)
  record.session = session
  record
end

#find_key(_session, secret:) ⇒ Object



15
16
17
# File 'app/models/masks/adapters/active_record.rb', line 15

def find_key(_session, secret:)
  Masks::Rails::Key.find_by(sha: Masks::Rails::Key.sha(secret))
end

#find_recovery(_session, **opts) ⇒ Object



72
73
74
75
76
77
78
# File 'app/models/masks/adapters/active_record.rb', line 72

def find_recovery(_session, **opts)
  if opts[:token]
    @config.model(:recovery).recent.find_by(token: opts[:token])
  elsif opts[:id]
    @config.model(:recovery).recent.find_by(id: opts[:id])
  end
end