forked from rails-api/active_model_serializers
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathassociation.rb
62 lines (53 loc) · 2.26 KB
/
association.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
60
61
62
# frozen_string_literal: true
require 'active_model/default_serializer'
require 'active_model/serializer/association/has_one'
require 'active_model/serializer/association/has_many'
module ActiveModel
class Serializer
class Association
def initialize(name, options={})
if options.has_key?(:include)
puts '-' * 40
puts caller
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
attr_reader :name, :embed_ids, :embed_objects, :polymorphic
attr_accessor :embed_in_root, :embed_key, :key, :embedded_key, :root_key, :serializer_from_options, :options, :key_format, :embed_in_root_key, :embed_namespace
alias embed_ids? embed_ids
alias embed_objects? embed_objects
alias embed_in_root? embed_in_root
alias embed_in_root_key? embed_in_root_key
alias embed_namespace? embed_namespace
alias polymorphic? polymorphic
def embed=(embed)
@embed_ids = embed == :id || embed == :ids
@embed_objects = embed == :object || embed == :objects
end
def serializer_from_object(object, options = {})
Serializer.serializer_for(object, options)
end
def default_serializer
DefaultSerializer
end
def build_serializer(object, options = {})
serializer_class(object, options).new(object, options.merge(self.options))
end
end
end
end