Class: ActiveModel::Serializer::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model/serializer/association.rb,
lib/active_model/serializer/association/has_one.rb,
lib/active_model/serializer/association/has_many.rb

Direct Known Subclasses

HasMany, HasOne

Defined Under Namespace

Classes: HasMany, HasOne

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Association

Returns a new instance of Association.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_model/serializer/association.rb', line 8

def initialize(name, options={})
  if options.has_key?(:include)
    ActiveSupport::Deprecation.warn <<-WARN
** Notice: include was renamed to embed_in_root. **
    WARN
  end

  @name          = name.to_s
  @options       = options
  self.embed     = options.fetch(:embed) { CONFIG.embed }
  @polymorphic   = options.fetch(:polymorphic, false)
  @embed_in_root = options.fetch(:embed_in_root) { options.fetch(:include) { CONFIG.embed_in_root } }
  @key_format    = options.fetch(:key_format) { CONFIG.key_format }
  @embed_key     = options[:embed_key] || :id
  @key           = options[:key]
  @embedded_key  = options[:root] || name
  @embed_in_root_key = options.fetch(:embed_in_root_key) { CONFIG.embed_in_root_key }
  @embed_namespace = options.fetch(:embed_namespace) { CONFIG.embed_namespace }

  serializer = @options[:serializer]
  @serializer_from_options = serializer.is_a?(String) ? serializer.constantize : serializer
end

Instance Attribute Details

#embed_idsObject (readonly) Also known as: embed_ids?

Returns the value of attribute embed_ids.



31
32
33
# File 'lib/active_model/serializer/association.rb', line 31

def embed_ids
  @embed_ids
end

#embed_in_rootObject Also known as: embed_in_root?

Returns the value of attribute embed_in_root.



32
33
34
# File 'lib/active_model/serializer/association.rb', line 32

def embed_in_root
  @embed_in_root
end

#embed_in_root_keyObject Also known as: embed_in_root_key?

Returns the value of attribute embed_in_root_key.



32
33
34
# File 'lib/active_model/serializer/association.rb', line 32

def embed_in_root_key
  @embed_in_root_key
end

#embed_keyObject

Returns the value of attribute embed_key.



32
33
34
# File 'lib/active_model/serializer/association.rb', line 32

def embed_key
  @embed_key
end

#embed_namespaceObject Also known as: embed_namespace?

Returns the value of attribute embed_namespace.



32
33
34
# File 'lib/active_model/serializer/association.rb', line 32

def embed_namespace
  @embed_namespace
end

#embed_objectsObject (readonly) Also known as: embed_objects?

Returns the value of attribute embed_objects.



31
32
33
# File 'lib/active_model/serializer/association.rb', line 31

def embed_objects
  @embed_objects
end

#embedded_keyObject

Returns the value of attribute embedded_key.



32
33
34
# File 'lib/active_model/serializer/association.rb', line 32

def embedded_key
  @embedded_key
end

#keyObject

Returns the value of attribute key.



32
33
34
# File 'lib/active_model/serializer/association.rb', line 32

def key
  @key
end

#key_formatObject

Returns the value of attribute key_format.



32
33
34
# File 'lib/active_model/serializer/association.rb', line 32

def key_format
  @key_format
end

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/active_model/serializer/association.rb', line 31

def name
  @name
end

#optionsObject

Returns the value of attribute options.



32
33
34
# File 'lib/active_model/serializer/association.rb', line 32

def options
  @options
end

#polymorphicObject (readonly) Also known as: polymorphic?

Returns the value of attribute polymorphic.



31
32
33
# File 'lib/active_model/serializer/association.rb', line 31

def polymorphic
  @polymorphic
end

#root_keyObject

Returns the value of attribute root_key.



32
33
34
# File 'lib/active_model/serializer/association.rb', line 32

def root_key
  @root_key
end

#serializer_from_optionsObject

Returns the value of attribute serializer_from_options.



32
33
34
# File 'lib/active_model/serializer/association.rb', line 32

def serializer_from_options
  @serializer_from_options
end

Instance Method Details

#build_serializer(object, options = {}) ⇒ Object



53
54
55
# File 'lib/active_model/serializer/association.rb', line 53

def build_serializer(object, options = {})
  serializer_class(object, options).new(object, options.merge(self.options))
end

#default_serializerObject



49
50
51
# File 'lib/active_model/serializer/association.rb', line 49

def default_serializer
  DefaultSerializer
end

#embed=(embed) ⇒ Object



40
41
42
43
# File 'lib/active_model/serializer/association.rb', line 40

def embed=(embed)
  @embed_ids     = embed == :id || embed == :ids
  @embed_objects = embed == :object || embed == :objects
end

#serializer_from_object(object, options = {}) ⇒ Object



45
46
47
# File 'lib/active_model/serializer/association.rb', line 45

def serializer_from_object(object, options = {})
  Serializer.serializer_for(object, options)
end