-
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathexternal_registry_credential.rb
More file actions
59 lines (53 loc) · 1.72 KB
/
Copy pathexternal_registry_credential.rb
File metadata and controls
59 lines (53 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
49
50
51
52
53
54
55
56
57
58
59
# == Schema Information
#
# Table name: external_registry_credentials
#
# id :integer not null, primary key
# access_token :string
# access_token_expires_at :datetime
# info_hash :jsonb
# refresh_token :string
# type :string not null
# created_at :datetime not null
# updated_at :datetime not null
# app_id :string
#
# Indexes
#
# index_external_registry_credentials_on_type (type)
#
class ExternalRegistryCredential < ApplicationRecord
validates :type, uniqueness: true
validates :app_id, uniqueness: {scope: :type}
attr_reader :api
# The list of acceptable STI types.
#
# Return an array containing each ExternalRegistryCredential subclass name,
# stringified.
def self.types
subclasses = %w[
Project529Credential
StopHelingCredential
]
subclasses
.map { |subclass_name| [to_s, subclass_name] }
.map { |namespace, subclass| "#{namespace}::#{subclass}" }
end
# The registry name for receiver record. Inferred from the class name.
def registry_name
credential_classname = self.class.to_s.split("::").last
credential_classname.chomp("Credential")
end
# Return true if the access token expiration is set and the access_token has
# not yet expired.
def access_token_valid?
return false if access_token_expires_at.blank?
Time.current < access_token_expires_at
end
# Set an error and return false if access_token is not yet expired.
def access_token_can_be_reset?
return true unless access_token_expires_at.present? && access_token_valid?
errors.add(:access_token, "not expired")
false
end
end