Class: MagicLinks::MagicToken

Inherits:
ApplicationRecord show all
Defined in:
app/models/magic_links/magic_token.rb

Constant Summary collapse

TOKEN_STRENGTHS =
{
  mild: 8,
  moderate: 16,
  strong: 32,
}.with_indifferent_access.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for(authenticatable:, target_path:, action_scope:, strength:, expiry: nil) ⇒ Object



30
31
32
33
34
# File 'app/models/magic_links/magic_token.rb', line 30

def for(authenticatable:, target_path:, action_scope:, strength:, expiry: nil)
  send(strength, authenticatable, target_path, action_scope).tap do |token|
    token.expire_in(expiry) if expiry.present?
  end
end

Instance Method Details

#expire_in(duration) ⇒ Object



41
42
43
44
# File 'app/models/magic_links/magic_token.rb', line 41

def expire_in(duration)
  update_attribute :expires_at, (Time.zone.now + duration)
  self
end

#expired?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/magic_links/magic_token.rb', line 37

def expired?
  expires_at&.past?
end

#mild?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/models/magic_links/magic_token.rb', line 46

def mild?
  strength == :mild
end

#moderate?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/models/magic_links/magic_token.rb', line 50

def moderate?
  strength == :moderate
end

#scopeObject



58
59
60
61
62
# File 'app/models/magic_links/magic_token.rb', line 58

def scope
  return unless magic_token_authenticatable.present?

  magic_token_authenticatable.model_name.singular.to_sym
end

#strong?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/models/magic_links/magic_token.rb', line 54

def strong?
  strength == :strong
end