Class: Api::V5::BaseSerializer

Inherits:
ApplicationSerializer show all
Defined in:
app/serializers/api/v5/base_serializer.rb

Overview

typed: ignore

Instance Method Summary collapse

Methods inherited from ApplicationSerializer

decorate_attributes, decorator, #object_cache_key

Instance Method Details

#full_attachment_url(attachment_url) ⇒ Object



6
7
8
9
10
11
12
# File 'app/serializers/api/v5/base_serializer.rb', line 6

def full_attachment_url(attachment_url)
  ElasticAPM.with_span('full_attachment_url') do |_span|
    return if attachment_url.blank?

    "#{CarrierWave::Uploader::Base.asset_host.call}/#{attachment_url}"
  end
end

#safe_translation_attr(object, attribute, locale = nil) ⇒ String?

Safe translation attribute accessor for models with column-based translations Falls back to default locale if requested locale column doesn't exist

Parameters:

  • object (ActiveRecord::Base)

    The model instance

  • attribute (String)

    The base attribute name (e.g., 'title', 'description')

  • locale (String, Symbol) (defaults to: nil)

    The locale (defaults to current normalized locale)

Returns:

  • (String, nil)

    The translated value or nil



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/serializers/api/v5/base_serializer.rb', line 20

def safe_translation_attr(object, attribute, locale = nil)
  locale ||= MyLocaleManager.normalize_locale
  default_locale = MyLocaleManager.default_locale

  # Try requested locale
  method_name = "#{attribute}_#{locale}"
  if object.respond_to?(method_name)
    value = object.send(method_name)
    return value if value.present?
  end

  # Fall back to default locale if requested locale returned nil/blank
  fallback_method = "#{attribute}_#{default_locale}"
  if object.respond_to?(fallback_method)
    value = object.send(fallback_method)
    return value if value.present?
  end

  # Last resort: try base attribute or return empty string
  object.respond_to?(attribute) ? object.send(attribute) : ''
end