Skip to content

Commit 0e43a6e

Browse files
committed
Refactor NestedJson adapter.
1 parent e6d6c5e commit 0e43a6e

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

lib/active_model/serializer/adapter/nested_json.rb

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,60 @@
11
module ActiveModel
22
class Serializer
33
class Adapter
4-
class NestedJson < Json
4+
class NestedJson < Adapter
55
cattr_accessor :default_limit_depth
6-
@@default_limit_depth = 5
6+
self.default_limit_depth = 5
77

88
def serializable_hash options = {}
99
@current_depth = options[:_current_depth] || 0
1010
@limit_depth = options[:limit_depth] || default_limit_depth
1111
check_depth!
1212

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
3617

37-
@result
18+
def fragment_cache(cached_hash, non_cached_hash)
19+
Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash)
3820
end
3921

22+
private
4023
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
4328
end
4429

4530
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
4753
end
4854

4955
def check_depth!
5056
if @current_depth > @limit_depth
51-
fail "associations are too deep."
57+
fail 'Too deep associations.'
5258
end
5359
end
5460
end

0 commit comments

Comments
 (0)