Class: ActiveModelSerializers::SerializableResource

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/active_model_serializers/serializable_resource.rb

Constant Summary collapse

ADAPTER_OPTION_KEYS =
Set.new([:include, :fields, :adapter, :meta, :meta_key, :links, :serialization_context, :key_transform])

Constants included from Logging

Logging::RENDER_EVENT

Instance Method Summary collapse

Methods included from Logging

#notify_render, #notify_render_payload

Constructor Details

#initialize(resource, options = {}) ⇒ Object

Primary interface to composing a resource with a serializer and adapter.



15
16
17
18
19
# File 'lib/active_model_serializers/serializable_resource.rb', line 15

def initialize(resource, options = {})
  @resource = resource
  @adapter_opts, @serializer_opts =
    options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
end

Instance Method Details

#adapterObject Also known as: adapter_instance

NOTE: if no adapter is available, returns the resource itself. (i.e. adapter is a no-op)



34
35
36
# File 'lib/active_model_serializers/serializable_resource.rb', line 34

def adapter
  @adapter ||= find_adapter
end

#find_adapterObject



39
40
41
42
43
44
45
# File 'lib/active_model_serializers/serializable_resource.rb', line 39

def find_adapter
  return resource unless serializer?
  adapter = catch :no_serializer do
    ActiveModelSerializers::Adapter.create(serializer_instance, adapter_opts)
  end
  adapter || resource
end

#serialization_scopeObject



25
26
27
# File 'lib/active_model_serializers/serializable_resource.rb', line 25

def serialization_scope
  serializer_opts[:scope]
end

#serialization_scope=(scope) ⇒ Object



21
22
23
# File 'lib/active_model_serializers/serializable_resource.rb', line 21

def serialization_scope=(scope)
  serializer_opts[:scope] = scope
end

#serialization_scope_name=(scope_name) ⇒ Object



29
30
31
# File 'lib/active_model_serializers/serializable_resource.rb', line 29

def serialization_scope_name=(scope_name)
  serializer_opts[:scope_name] = scope_name
end

#serializerObject Also known as: serializer_class

Get serializer either explicitly :serializer or implicitly from resource Remove :serializer key from serializer_opts Remove :each_serializer if present and set as :serializer key



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/active_model_serializers/serializable_resource.rb', line 54

def serializer
  @serializer ||=
    begin
      @serializer = serializer_opts.delete(:serializer)
      @serializer ||= ActiveModel::Serializer.serializer_for(resource, serializer_opts)

      if serializer_opts.key?(:each_serializer)
        serializer_opts[:serializer] = serializer_opts.delete(:each_serializer)
      end
      @serializer
    end
end

#serializer?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/active_model_serializers/serializable_resource.rb', line 74

def serializer?
  use_adapter? && !serializer.nil?
end

#serializer_instanceObject



47
48
49
# File 'lib/active_model_serializers/serializable_resource.rb', line 47

def serializer_instance
  @serializer_instance ||= serializer.new(resource, serializer_opts)
end

#use_adapter?Boolean

True when no explicit adapter given, or explicit appear is truthy (non-nil) False when explicit adapter is falsy (nil or false)

Returns:

  • (Boolean)


70
71
72
# File 'lib/active_model_serializers/serializable_resource.rb', line 70

def use_adapter?
  !(adapter_opts.key?(:adapter) && !adapter_opts[:adapter])
end