Class: ActiveModel::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/freedom_patches/ams_include_without_root.rb

Instance Method Summary collapse

Instance Method Details

#include!(name, options = {}) ⇒ Object

This method is copied over verbatim from the AMS version, except for silently ignoring associations that cannot be embedded without a root instead of raising an exception.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/freedom_patches/ams_include_without_root.rb', line 13

def include!(name, options = {})
  unique_values =
    if hash = options[:hash]
      if @options[:hash] == hash
        @options[:unique_values] ||= {}
      else
        {}
      end
    else
      hash = @options[:hash]
      @options[:unique_values] ||= {}
    end

  node = options[:node] ||= @node
  value = options[:value]

  if options[:include] == nil
    if @options.key?(:include)
      options[:include] = @options[:include].include?(name)
    elsif @options.include?(:exclude)
      options[:include] = !@options[:exclude].include?(name)
    end
  end

  association_class =
    if klass = _associations[name]
      klass
    elsif value.respond_to?(:to_ary)
      Associations::HasMany
    else
      Associations::HasOne
    end

  association = association_class.new(name, self, options)

  if association.embed_ids?
    node[association.key] = association.serialize_ids

    if association.embed_in_root? && hash.nil?
      # Don't raise an error!
    elsif association.embed_in_root? && association.embeddable?
      merge_association hash, association.root, association.serializables, unique_values
    end
  elsif association.embed_objects?
    node[association.key] = association.serialize
  end
end