Skip to content

Commit ceef214

Browse files
committed
FlattenJson adapter no longer inherits Json adapter
1 parent 24a5f38 commit ceef214

File tree

3 files changed

+47
-39
lines changed

3 files changed

+47
-39
lines changed

lib/active_model/serializer/adapter.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,14 @@ def cache_check(serializer)
112112
end
113113
end
114114

115+
private
116+
115117
def meta
116118
serializer.meta if serializer.respond_to?(:meta)
117119
end
118120

119121
def meta_key
120-
serializer.meta_key || 'meta'
122+
serializer.meta_key || 'meta'.freeze
121123
end
122124

123125
def root

lib/active_model/serializer/adapter/flatten_json.rb

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
11
class ActiveModel::Serializer::Adapter::FlattenJson < ActiveModel::Serializer::Adapter::Json
2-
def serializable_hash(options = {})
3-
super.each_value.first
2+
def serializable_hash(options = nil)
3+
options ||= {}
4+
if serializer.respond_to?(:each)
5+
result = serializer.map { |s| FlattenJson.new(s).serializable_hash(options) }
6+
else
7+
hash = {}
8+
9+
core = cache_check(serializer) do
10+
serializer.attributes(options)
11+
end
12+
13+
serializer.associations.each do |association|
14+
serializer = association.serializer
15+
association_options = association.options
16+
17+
if serializer.respond_to?(:each)
18+
array_serializer = serializer
19+
hash[association.key] = array_serializer.map do |item|
20+
cache_check(item) do
21+
item.attributes(association_options)
22+
end
23+
end
24+
else
25+
hash[association.key] =
26+
if serializer && serializer.object
27+
cache_check(serializer) do
28+
serializer.attributes(options)
29+
end
30+
elsif association_options[:virtual_value]
31+
association_options[:virtual_value]
32+
end
33+
end
34+
end
35+
result = core.merge hash
36+
end
37+
result
38+
end
39+
40+
def fragment_cache(cached_hash, non_cached_hash)
41+
Json::FragmentCache.new.fragment_cache(cached_hash, non_cached_hash)
442
end
543

644
private

lib/active_model/serializer/adapter/json.rb

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,12 @@ class ActiveModel::Serializer::Adapter::Json < ActiveModel::Serializer::Adapter
44

55
def serializable_hash(options = nil)
66
options ||= {}
7-
if serializer.respond_to?(:each)
8-
result = serializer.map { |s| FlattenJson.new(s).serializable_hash(options) }
9-
else
10-
hash = {}
11-
12-
core = cache_check(serializer) do
13-
serializer.attributes(options)
14-
end
15-
16-
serializer.associations.each do |association|
17-
serializer = association.serializer
18-
association_options = association.options
19-
20-
if serializer.respond_to?(:each)
21-
array_serializer = serializer
22-
hash[association.key] = array_serializer.map do |item|
23-
cache_check(item) do
24-
item.attributes(association_options)
25-
end
26-
end
27-
else
28-
hash[association.key] =
29-
if serializer && serializer.object
30-
cache_check(serializer) do
31-
serializer.attributes(options)
32-
end
33-
elsif association_options[:virtual_value]
34-
association_options[:virtual_value]
35-
end
36-
end
37-
end
38-
result = core.merge hash
39-
end
40-
41-
{ root => result }
7+
{ root => FlattenJson.new(serializer).serializable_hash(options) }
428
end
439

10+
private
11+
4412
def fragment_cache(cached_hash, non_cached_hash)
45-
ActiveModel::Serializer::Adapter::Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash)
13+
ActiveModel::Serializer::Adapter::Json::FragmentCache.new.fragment_cache(cached_hash, non_cached_hash)
4614
end
4715
end

0 commit comments

Comments
 (0)