Skip to content

Commit 5dbd7c7

Browse files
committed
Add support for Json adapter.
1 parent 4721584 commit 5dbd7c7

File tree

4 files changed

+39
-42
lines changed

4 files changed

+39
-42
lines changed

lib/active_model/serializer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require 'active_model/serializer/configuration'
77
require 'active_model/serializer/fieldset'
88
require 'active_model/serializer/lint'
9+
require 'active_model/serializer/utils'
910

1011
module ActiveModel
1112
class Serializer

lib/active_model/serializer/adapter/attributes.rb

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,39 @@ def initialize(serializer, options = {})
77
@include_tree = IncludeTree.from_include_args(options[:include] || '*')
88
end
99

10+
def serializable_hash_for_collection(options)
11+
serializer.map { |s| Attributes.new(s, instance_options).serializable_hash(options) }
12+
end
13+
14+
def serializable_hash_for_single_resource(options)
15+
hash = {}
16+
17+
core = cache_check(serializer) do
18+
serializer.attributes(options)
19+
end
20+
21+
serializer.associations(@include_tree).each do |association|
22+
hash[association.key] =
23+
if association.options[:virtual_value]
24+
association.options[:virtual_value]
25+
elsif association.serializer && association.serializer.object
26+
Attributes.new(association.serializer, association.options.merge(include: @include_tree[association.key]))
27+
.serializable_hash(options)
28+
end
29+
end
30+
result = core.merge hash
31+
32+
result
33+
end
34+
1035
def serializable_hash(options = nil)
1136
options ||= {}
37+
1238
if serializer.respond_to?(:each)
13-
result = serializer.map { |s| Attributes.new(s, instance_options).serializable_hash(options) }
39+
serializable_hash_for_collection(options)
1440
else
15-
hash = {}
16-
17-
core = cache_check(serializer) do
18-
serializer.attributes(options)
19-
end
20-
21-
serializer.associations(@include_tree).each do |association|
22-
serializer = association.serializer
23-
association_options = association.options
24-
25-
if serializer.respond_to?(:each)
26-
array_serializer = serializer
27-
hash[association.key] = array_serializer.map do |item|
28-
cache_check(item) do
29-
item.attributes(association_options)
30-
end
31-
end
32-
else
33-
hash[association.key] =
34-
if serializer && serializer.object
35-
cache_check(serializer) do
36-
serializer.attributes(options)
37-
end
38-
elsif association_options[:virtual_value]
39-
association_options[:virtual_value]
40-
end
41-
end
42-
end
43-
result = core.merge hash
41+
serializable_hash_for_single_resource(options)
4442
end
45-
result
4643
end
4744

4845
def fragment_cache(cached_hash, non_cached_hash)

lib/active_model/serializer/adapter/json.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ class Json < Base
55
extend ActiveSupport::Autoload
66
autoload :FragmentCache
77

8-
def initialize(serializer, options = {})
9-
super
10-
@included = ActiveModel::Serializer::Utils.include_args_to_hash(instance_options[:include] || '*')
11-
end
12-
138
def serializable_hash(options = nil)
149
options ||= {}
1510

test/adapter/json_api/nested_serializers_test.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module ActiveModel
44
class Serializer
5-
class Adapter
5+
module Adapter
66
class JsonApi
77
class NestedSerializersTest < Minitest::Test
88
def setup
@@ -18,11 +18,15 @@ def setup
1818
end
1919

2020
def test_nested_serializers
21-
hash = with_adapter :json_api do
22-
ActiveModel::SerializableResource.new(@tweet).serializable_hash
23-
end
24-
25-
assert_equal(nil, hash)
21+
actual = ActiveModel::SerializableResource.new(@tweet, adapter: :json_api).serializable_hash
22+
expected = {
23+
data: {
24+
id: '1', type: 'tweets', attributes: { body: 'Tweet 1', date: 'Jan 15' },
25+
relationships: { author: { data: { id: '1', type: 'authors' } },
26+
shares: { data: [{ id: '1', type: 'shares' }] } }
27+
}
28+
}
29+
assert_equal(expected, actual)
2630
end
2731
end
2832
end

0 commit comments

Comments
 (0)