Class: ActiveModel::Serializer::Associations::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model/serializer/associations.rb

Overview

:nodoc:

Direct Known Subclasses

HasMany, HasOne

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, source, options = {}) ⇒ Config

Returns a new instance of Config.



33
34
35
36
37
# File 'lib/active_model/serializer/associations.rb', line 33

def initialize(name, source, options={})
  @name = name
  @source = source
  @options = options
end

Class Method Details

.refine(name, class_options) ⇒ Object



7
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/associations.rb', line 7

def self.refine(name, class_options)
  current_class = self

  Class.new(self) do
    singleton_class.class_eval do
      define_method(:to_s) do
        "(subclass of #{current_class.name})"
      end

      alias inspect to_s
    end

    self.options = class_options

    # cache the root so we can reuse it without falling back on a per-instance basis
    begin
      self.options[:root] ||= self.new(name, nil).root
    rescue
      # this could fail if it needs a valid source, for example a polymorphic association
    end

  end
end

Instance Method Details

#associated_objectObject



70
71
72
# File 'lib/active_model/serializer/associations.rb', line 70

def associated_object
  option(:value) || source_serializer.send(name)
end

#embed_ids?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/active_model/serializer/associations.rb', line 74

def embed_ids?
  [:id, :ids].include? option(:embed, source_serializer._embed)
end

#embed_in_root?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/active_model/serializer/associations.rb', line 82

def embed_in_root?
  option(:include, source_serializer._root_embed)
end

#embed_objects?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/active_model/serializer/associations.rb', line 78

def embed_objects?
  [:object, :objects].include? option(:embed, source_serializer._embed)
end

#embeddable?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/active_model/serializer/associations.rb', line 86

def embeddable?
  !associated_object.nil?
end

#keyObject



58
59
60
# File 'lib/active_model/serializer/associations.rb', line 58

def key
  option(:key) || @name
end

#nameObject



66
67
68
# File 'lib/active_model/serializer/associations.rb', line 66

def name
  option(:name) || @name
end

#option(key, default = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/active_model/serializer/associations.rb', line 39

def option(key, default=nil)
  if @options.key?(key)
    @options[key]
  elsif self.class.options.key?(key)
    self.class.options[key]
  else
    default
  end
end

#rootObject



62
63
64
# File 'lib/active_model/serializer/associations.rb', line 62

def root
  option(:root) || @name
end

#source_serializerObject



54
55
56
# File 'lib/active_model/serializer/associations.rb', line 54

def source_serializer
  @source
end

#target_serializerObject



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

def target_serializer
  serializer = option(:serializer)
  serializer.is_a?(String) ? serializer.constantize : serializer
end