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
-
#can_edit? ⇒ Boolean
--- Actions ---.
- #can_revoke? ⇒ Boolean
- #expires_at ⇒ Object
-
#full_token ⇒ Object
The actual token string for admin clipboard copy.
-
#initialize(record) ⇒ TokenPresenter
constructor
A new instance of TokenPresenter.
-
#last_used_at ⇒ Object
--- Timestamps ---.
-
#masked_token ⇒ Object
Masked display: first 4...last 4.
-
#name ⇒ Object
--- Name ---.
- #oauth? ⇒ Boolean
-
#service_labels ⇒ Object
--- Services ---.
-
#status ⇒ Object
--- Status ---.
- #status_badge_class ⇒ Object
- #type_badge_class ⇒ Object
-
#type_label ⇒ Object
--- Type ---.
Constructor Details
#initialize(record) ⇒ TokenPresenter
Returns a new instance of TokenPresenter.
14 15 16 |
# File 'app/presenters/crm/token_presenter.rb', line 14 def initialize(record) @record = record end |
Instance Attribute Details
#record ⇒ Object (readonly)
Returns the value of attribute record.
10 11 12 |
# File 'app/presenters/crm/token_presenter.rb', line 10 def record @record end |
Instance Method Details
#active? ⇒ Boolean
98 99 100 |
# File 'app/presenters/crm/token_presenter.rb', line 98 def active? status == 'active' end |
#can_edit? ⇒ Boolean
--- Actions ---
116 117 118 |
# File 'app/presenters/crm/token_presenter.rb', line 116 def can_edit? !oauth? && active? end |
#can_revoke? ⇒ Boolean
120 121 122 |
# File 'app/presenters/crm/token_presenter.rb', line 120 def can_revoke? active? end |
#created_at ⇒ Object
Alias for Record#created_at
12 |
# File 'app/presenters/crm/token_presenter.rb', line 12 delegate :created_at, to: :record |
#expired? ⇒ Object
Alias for Record#expired?
112 |
# File 'app/presenters/crm/token_presenter.rb', line 112 delegate :revoked_at, :revoked?, :expired?, to: :record |
#expires_at ⇒ Object
108 109 110 |
# File 'app/presenters/crm/token_presenter.rb', line 108 def expires_at record.expires_at if record.respond_to?(:expires_at) end |
#full_token ⇒ Object
The actual token string for admin clipboard copy
45 46 47 48 49 50 51 |
# File 'app/presenters/crm/token_presenter.rb', line 45 def full_token if oauth? record.token else record.api_authentication_token end end |
#last_used_at ⇒ Object
--- Timestamps ---
104 105 106 |
# File 'app/presenters/crm/token_presenter.rb', line 104 def last_used_at record.last_used_at if record.respond_to?(:last_used_at) end |
#masked_token ⇒ Object
Masked display: first 4...last 4
54 55 56 57 58 59 60 |
# File 'app/presenters/crm/token_presenter.rb', line 54 def masked_token token = full_token return nil if token.blank? return token if token.length <= 12 "#{token[0..3]}...#{token[-4..]}" end |
#name ⇒ Object
--- Name ---
34 35 36 37 38 39 40 |
# File 'app/presenters/crm/token_presenter.rb', line 34 def name if oauth? record.application&.name || 'OAuth Token' else record.name.presence || 'Unnamed Token' end end |
#oauth? ⇒ Boolean
28 29 30 |
# File 'app/presenters/crm/token_presenter.rb', line 28 def oauth? record.is_a?(Doorkeeper::AccessToken) end |
#revoked? ⇒ Object
Alias for Record#revoked?
112 |
# File 'app/presenters/crm/token_presenter.rb', line 112 delegate :revoked_at, :revoked?, :expired?, to: :record |
#revoked_at ⇒ Object
Alias for Record#revoked_at
112 |
# File 'app/presenters/crm/token_presenter.rb', line 112 delegate :revoked_at, :revoked?, :expired?, to: :record |
#service_labels ⇒ Object
--- Services ---
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/presenters/crm/token_presenter.rb', line 64 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 ⇒ Object
--- Status ---
79 80 81 82 83 84 85 86 87 88 |
# File 'app/presenters/crm/token_presenter.rb', line 79 def status if oauth? return 'revoked' if record.revoked? return 'expired' if record.expired? 'active' else record.status end end |
#status_badge_class ⇒ Object
90 91 92 93 94 95 96 |
# File 'app/presenters/crm/token_presenter.rb', line 90 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 ⇒ Object
24 25 26 |
# File 'app/presenters/crm/token_presenter.rb', line 24 def type_badge_class oauth? ? 'bg-info-subtle text-info' : 'bg-primary-subtle text-primary' end |
#type_label ⇒ Object
--- Type ---
20 21 22 |
# File 'app/presenters/crm/token_presenter.rb', line 20 def type_label oauth? ? 'OAuth' : 'API Token' end |