|
1 | 1 | module ActiveModel
|
2 | 2 | class Serializer
|
3 | 3 | class Adapter
|
4 |
| - class NestedJson < Json |
| 4 | + class NestedJson < Adapter |
5 | 5 | cattr_accessor :default_limit_depth
|
6 |
| - @@default_limit_depth = 5 |
| 6 | + self.default_limit_depth = 5 |
7 | 7 |
|
8 | 8 | def serializable_hash options = {}
|
9 | 9 | @current_depth = options[:_current_depth] || 0
|
10 | 10 | @limit_depth = options[:limit_depth] || default_limit_depth
|
11 | 11 | check_depth!
|
12 | 12 |
|
13 |
| - if serializer.respond_to?(:each) |
14 |
| - @result = serialize_collection(serializer, options) |
15 |
| - else |
16 |
| - @hash = {} |
17 |
| - |
18 |
| - @core = cache_check(serializer) do |
19 |
| - serializer.attributes(options) |
20 |
| - end |
21 |
| - |
22 |
| - serializer.associations.each do |association| |
23 |
| - serializer = association.serializer |
24 |
| - opts = association.options |
25 |
| - |
26 |
| - if serializer.respond_to?(:each) |
27 |
| - @hash[association.key] = serialize_collection(serializer, opts) |
28 |
| - elsif serializer && serializer.object |
29 |
| - @hash[association.key] = serialize_object(serializer, opts) |
30 |
| - else |
31 |
| - @hash[association.key] = opts[:virtual_value] |
32 |
| - end |
33 |
| - end |
34 |
| - @result = @core.merge @hash |
35 |
| - end |
| 13 | + @result = |
| 14 | + serialize_collection(serializer, options) || |
| 15 | + serialize_attributes(options).merge(serialize_associations) |
| 16 | + end |
36 | 17 |
|
37 |
| - @result |
| 18 | + def fragment_cache(cached_hash, non_cached_hash) |
| 19 | + Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash) |
38 | 20 | end
|
39 | 21 |
|
| 22 | + private |
40 | 23 | def serialize_object serializer, options = {}
|
41 |
| - options = options.merge(_current_depth: @current_depth + 1, limit_depth: @limit_depth) |
42 |
| - self.class.new(serializer).serializable_hash(options) |
| 24 | + if serializer.try(:object) |
| 25 | + options = options.merge(_current_depth: @current_depth + 1, limit_depth: @limit_depth) |
| 26 | + self.class.new(serializer).serializable_hash(options) |
| 27 | + end |
43 | 28 | end
|
44 | 29 |
|
45 | 30 | def serialize_collection serializers, options = {}
|
46 |
| - serializers.map { |s| serialize_object(s, options) } |
| 31 | + if serializers.respond_to?(:each) |
| 32 | + serializers.map { |s| serialize_object(s, options) } |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + def serialize_attributes options |
| 37 | + cache_check(serializer) do |
| 38 | + serializer.attributes(options) |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + def serialize_associations |
| 43 | + hash = {} |
| 44 | + serializer.associations.each do |association| |
| 45 | + serializer = association.serializer |
| 46 | + opts = association.options |
| 47 | + hash[association.key] = |
| 48 | + serialize_collection(serializer, opts) || |
| 49 | + serialize_object(serializer, opts) || |
| 50 | + opts[:virtual_value] |
| 51 | + end |
| 52 | + hash |
47 | 53 | end
|
48 | 54 |
|
49 | 55 | def check_depth!
|
50 | 56 | if @current_depth > @limit_depth
|
51 |
| - fail "associations are too deep." |
| 57 | + fail 'Too deep associations.' |
52 | 58 | end
|
53 | 59 | end
|
54 | 60 | end
|
|
0 commit comments