Class: Crm::TokenPresenter

Inherits:
Object
  • Object
show all
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

Delegated Instance Attributes collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ TokenPresenter

Returns a new instance of TokenPresenter.

Parameters:



16
17
18
# File 'app/presenters/crm/token_presenter.rb', line 16

def initialize(record)
  @record = record
end

Instance Attribute Details

#recordObject (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.

Returns:

  • (Boolean)

    true when status is '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).

Returns:

  • (Boolean)

    true when editable



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.

Returns:

  • (Boolean)

    true when revocable



142
143
144
# File 'app/presenters/crm/token_presenter.rb', line 142

def can_revoke?
  active?
end

#created_atObject

Alias for Record#created_at

Returns:

  • (Object)

    Record#created_at

See Also:



13
# File 'app/presenters/crm/token_presenter.rb', line 13

delegate :created_at, to: :record

#expired?Object

Alias for Record#expired?

Returns:

  • (Object)

    Record#expired?

See Also:



131
# File 'app/presenters/crm/token_presenter.rb', line 131

delegate :revoked_at, :revoked?, :expired?, to: :record

#expires_atActiveSupport::TimeWithZone?

Token expiry time, when applicable.

Returns:

  • (ActiveSupport::TimeWithZone, nil)

    expiry timestamp



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_tokenString?

The actual token string for admin clipboard copy

Returns:

  • (String, nil)

    the raw token



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_atActiveSupport::TimeWithZone?

--- Timestamps ---
Last time the token was used, when tracked.

Returns:

  • (ActiveSupport::TimeWithZone, nil)

    last use timestamp



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_tokenString?

Masked display: first 4...last 4

Returns:

  • (String, nil)

    masked token, or nil when blank



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

#nameString

--- Name ---
Display name for the token.

Returns:

  • (String)

    application name, token name, or a fallback



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.

Returns:

  • (Boolean)

    true for OAuth tokens



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?

Returns:

  • (Object)

    Record#revoked?

See Also:



131
# File 'app/presenters/crm/token_presenter.rb', line 131

delegate :revoked_at, :revoked?, :expired?, to: :record

#revoked_atObject

Alias for Record#revoked_at

Returns:

  • (Object)

    Record#revoked_at

See Also:



131
# File 'app/presenters/crm/token_presenter.rb', line 131

delegate :revoked_at, :revoked?, :expired?, to: :record

#service_labelsArray<String>

--- Services ---
Labels of upstream services the token may access.

Returns:

  • (Array<String>)

    service labels



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

#statusString

--- Status ---
Token status normalized across both record types.

Returns:

  • (String)

    'active', 'expired', or 'revoked'



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_classString?

Badge CSS class for the current status.

Returns:

  • (String, nil)

    Bootstrap background class



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_classString

Badge CSS classes for the token kind.

Returns:

  • (String)

    Bootstrap badge classes



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_labelString

--- Type ---
Human label for the token kind.

Returns:

  • (String)

    'OAuth' or 'API Token'



23
24
25
# File 'app/presenters/crm/token_presenter.rb', line 23

def type_label
  oauth? ? 'OAuth' : 'API Token'
end