Class: Spree::Api::V3::BaseSerializer

Inherits:
Object
  • Object
show all
Includes:
Alba::Resource, Typelizer::DSL
Defined in:
app/serializers/spree/api/v3/base_serializer.rb

Instance Method Summary collapse

Instance Method Details

#current_currencyObject



24
25
26
# File 'app/serializers/spree/api/v3/base_serializer.rb', line 24

def current_currency
  params[:currency]
end

#current_localeObject



32
33
34
# File 'app/serializers/spree/api/v3/base_serializer.rb', line 32

def current_locale
  params[:locale]
end

#current_storeObject

Context accessors



20
21
22
# File 'app/serializers/spree/api/v3/base_serializer.rb', line 20

def current_store
  params[:store]
end

#current_userObject



28
29
30
# File 'app/serializers/spree/api/v3/base_serializer.rb', line 28

def current_user
  params[:user]
end

#expand?(name) ⇒ Boolean

Check if an association should be expanded Supports dot notation: expand?(‘variants’) matches both ‘variants’ and ‘variants.media’

Returns:

  • (Boolean)


42
43
44
45
# File 'app/serializers/spree/api/v3/base_serializer.rb', line 42

def expand?(name)
  name = name.to_s
  expands.any? { |e| e == name || e.start_with?("#{name}.") }
end

#expandsObject



36
37
38
# File 'app/serializers/spree/api/v3/base_serializer.rb', line 36

def expands
  @expands ||= Array(params[:expand] || [])
end

#nested_expands_for(parent) ⇒ Object

Get nested expands for a given parent



48
49
50
51
# File 'app/serializers/spree/api/v3/base_serializer.rb', line 48

def nested_expands_for(parent)
  prefix = "#{parent}."
  expands.select { |i| i.start_with?(prefix) }.map { |i| i.sub(prefix, '') }
end

#nested_params(parent) ⇒ Object

Build nested params for child serializers with depth limit (max 4 levels)



54
55
56
57
58
# File 'app/serializers/spree/api/v3/base_serializer.rb', line 54

def nested_params(parent)
  depth = params.fetch(:_expand_depth, 0)
  nested = depth < 4 ? nested_expands_for(parent) : []
  params.merge(expand: nested, _expand_depth: depth + 1)
end