Class: Crm::TokenPresenter
- Inherits:
-
Object
- Object
- Crm::TokenPresenter
- Defined in:
- app/presenters/crm/token_presenter.rb
Overview
Unified presenter wrapping either an ApiAuthentication or Doorkeeper::AccessToken
for display in the CRM API tokens index table.
Provides a common interface so the view doesn't need to know which
underlying token type it's rendering.
Instance Attribute Summary collapse
-
#record ⇒ Object
readonly
Returns the value of attribute record.
Delegated Instance Attributes collapse
-
#created_at ⇒ Object
Alias for Record#created_at.
-
#expired? ⇒ Object
Alias for Record#expired?.
-
#revoked? ⇒ Object
Alias for Record#revoked?.
-
#revoked_at ⇒ Object
Alias for Record#revoked_at.
Instance Method Summary collapse
-
#active? ⇒ Boolean
Whether the token is currently active.
-
#can_edit? ⇒ Boolean
--- Actions --- Whether the token can be edited (API tokens only, while active).
-
#can_revoke? ⇒ Boolean
Whether the token can be revoked.
-
#expires_at ⇒ ActiveSupport::TimeWithZone?
Token expiry time, when applicable.
-
#full_token ⇒ String?
The actual token string for admin clipboard copy.
-
#initialize(record) ⇒ TokenPresenter
constructor
A new instance of TokenPresenter.
-
#last_used_at ⇒ ActiveSupport::TimeWithZone?
--- Timestamps --- Last time the token was used, when tracked.
-
#masked_token ⇒ String?
Masked display: first 4...last 4.
-
#name ⇒ String
--- Name --- Display name for the token.
-
#oauth? ⇒ Boolean
Whether the record is a Doorkeeper OAuth access token.
-
#service_labels ⇒ Array<String>
--- Services --- Labels of upstream services the token may access.
-
#status ⇒ String
--- Status --- Token status normalized across both record types.
-
#status_badge_class ⇒ String?
Badge CSS class for the current status.
-
#type_badge_class ⇒ String
Badge CSS classes for the token kind.
-
#type_label ⇒ String
--- Type --- Human label for the token kind.
Constructor Details
#initialize(record) ⇒ TokenPresenter
Returns a new instance of TokenPresenter.
16 17 18 |
# File 'app/presenters/crm/token_presenter.rb', line 16 def initialize(record) @record = record end |
Instance Attribute Details
#record ⇒ Object (readonly)
Returns the value of attribute record.
11 12 13 |
# File 'app/presenters/crm/token_presenter.rb', line 11 def record @record end |
Instance Method Details
#active? ⇒ Boolean
Whether the token is currently active.
114 115 116 |
# File 'app/presenters/crm/token_presenter.rb', line 114 def active? status == 'active' end |
#can_edit? ⇒ Boolean
--- Actions ---
Whether the token can be edited (API tokens only, while active).
136 137 138 |
# File 'app/presenters/crm/token_presenter.rb', line 136 def can_edit? !oauth? && active? end |
#can_revoke? ⇒ Boolean
Whether the token can be revoked.
142 143 144 |
# File 'app/presenters/crm/token_presenter.rb', line 142 def can_revoke? active? end |
#created_at ⇒ Object
Alias for Record#created_at
13 |
# File 'app/presenters/crm/token_presenter.rb', line 13 delegate :created_at, to: :record |
#expired? ⇒ Object
Alias for Record#expired?
131 |
# File 'app/presenters/crm/token_presenter.rb', line 131 delegate :revoked_at, :revoked?, :expired?, to: :record |
#expires_at ⇒ ActiveSupport::TimeWithZone?
Token expiry time, when applicable.
127 128 129 |
# File 'app/presenters/crm/token_presenter.rb', line 127 def expires_at record.expires_at if record.respond_to?(:expires_at) end |
#full_token ⇒ String?
The actual token string for admin clipboard copy
54 55 56 57 58 59 60 |
# File 'app/presenters/crm/token_presenter.rb', line 54 def full_token if oauth? record.token else record.api_authentication_token end end |
#last_used_at ⇒ ActiveSupport::TimeWithZone?
--- Timestamps ---
Last time the token was used, when tracked.
121 122 123 |
# File 'app/presenters/crm/token_presenter.rb', line 121 def last_used_at record.last_used_at if record.respond_to?(:last_used_at) end |
#masked_token ⇒ String?
Masked display: first 4...last 4
64 65 66 67 68 69 70 |
# File 'app/presenters/crm/token_presenter.rb', line 64 def masked_token token = full_token return nil if token.blank? return token if token.length <= 12 "#{token[0..3]}...#{token[-4..]}" end |
#name ⇒ String
--- Name ---
Display name for the token.
42 43 44 45 46 47 48 |
# File 'app/presenters/crm/token_presenter.rb', line 42 def name if oauth? record.application&.name || 'OAuth Token' else record.name.presence || 'Unnamed Token' end end |
#oauth? ⇒ Boolean
Whether the record is a Doorkeeper OAuth access token.
35 36 37 |
# File 'app/presenters/crm/token_presenter.rb', line 35 def oauth? record.is_a?(Doorkeeper::AccessToken) end |
#revoked? ⇒ Object
Alias for Record#revoked?
131 |
# File 'app/presenters/crm/token_presenter.rb', line 131 delegate :revoked_at, :revoked?, :expired?, to: :record |
#revoked_at ⇒ Object
Alias for Record#revoked_at
131 |
# File 'app/presenters/crm/token_presenter.rb', line 131 delegate :revoked_at, :revoked?, :expired?, to: :record |
#service_labels ⇒ Array<String>
--- Services ---
Labels of upstream services the token may access.
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/presenters/crm/token_presenter.rb', line 75 def service_labels if oauth? app_services = record.application&.permitted_services.presence if app_services.present? app_services.filter_map { |key| ApiAuthentication::UPSTREAM_SERVICES.dig(key, :label) } else ['All Services'] end else record.service_labels end end |
#status ⇒ String
--- Status ---
Token status normalized across both record types.
91 92 93 94 95 96 97 98 99 100 |
# File 'app/presenters/crm/token_presenter.rb', line 91 def status if oauth? return 'revoked' if record.revoked? return 'expired' if record.expired? 'active' else record.status end end |
#status_badge_class ⇒ String?
Badge CSS class for the current status.
104 105 106 107 108 109 110 |
# File 'app/presenters/crm/token_presenter.rb', line 104 def status_badge_class case status when 'active' then 'bg-success' when 'expired' then 'bg-warning' when 'revoked' then 'bg-danger' end end |
#type_badge_class ⇒ String
Badge CSS classes for the token kind.
29 30 31 |
# File 'app/presenters/crm/token_presenter.rb', line 29 def type_badge_class oauth? ? 'bg-info-subtle text-info' : 'bg-primary-subtle text-primary' end |
#type_label ⇒ String
--- Type ---
Human label for the token kind.
23 24 25 |
# File 'app/presenters/crm/token_presenter.rb', line 23 def type_label oauth? ? 'OAuth' : 'API Token' end |