discourse/app/serializers/api_key_scope_serializer.rb
Sam f8666b60b0
DEV: Tidy plugin API key scope resource names (#38640)
Rename plugin API key scope resources to use shorter,
cleaner names:

- automations_trigger → automation (action: post → trigger_automation)
- discourse_ai → ai
- discourse_data_explorer → data_explorer

Includes reversible migrations to update existing
api_key_scopes records in the database and updated locale
keys to match the new resource names.

Also adds safety improvements:
- ApiKeyScope#permits? returns false when mapping is nil,
  gracefully handling renamed or removed scopes
- ApiKeyScopeSerializer uses safe navigation to avoid
  errors for missing scope mappings
- Scope resources are now sorted alphabetically in both
  the API response and the admin UI
2026-03-17 13:03:42 +11:00

21 lines
499 B
Ruby

# frozen_string_literal: true
class ApiKeyScopeSerializer < ApplicationSerializer
attributes :resource, :action, :parameters, :urls, :allowed_parameters, :key
def parameters
ApiKeyScope.scope_mappings.dig(object.resource.to_sym, object.action.to_sym)&.dig(:params).to_a
end
def urls
ApiKeyScope.scope_mappings.dig(object.resource.to_sym, object.action.to_sym)&.dig(:urls).to_a
end
def action
object.action.to_s.gsub("_", " ")
end
def key
object.action
end
end