Skip to content

Commit 8482abf

Browse files
committed
Move id and json_api_type methods from Serializer to JsonApi.
1 parent 64168cb commit 8482abf

File tree

3 files changed

+22
-30
lines changed

3 files changed

+22
-30
lines changed

lib/active_model/serializer.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,6 @@ def json_key
146146
@root || object.class.model_name.to_s.underscore
147147
end
148148

149-
def id
150-
object.id if object
151-
end
152-
153-
def json_api_type
154-
if config.jsonapi_resource_type == :plural
155-
object.class.model_name.plural
156-
else
157-
object.class.model_name.singular
158-
end
159-
end
160-
161149
def attributes(options = {})
162150
attributes =
163151
if options[:fields]
@@ -166,8 +154,6 @@ def attributes(options = {})
166154
self.class._attributes.dup
167155
end
168156

169-
attributes += options[:required_fields] if options[:required_fields]
170-
171157
attributes.each_with_object({}) do |name, hash|
172158
unless self.class._fragmented
173159
hash[name] = send(name)

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,34 @@ def fragment_cache(cached_hash, non_cached_hash)
4444

4545
private
4646

47+
def resource_identifier(serializer)
48+
type = if ActiveModel::Serializer.config.jsonapi_resource_type == :plural
49+
serializer.object.class.model_name.plural
50+
else
51+
serializer.object.class.model_name.singular
52+
end
53+
id = serializer.object.id.to_s
54+
55+
{ id: id, type: type }
56+
end
57+
4758
def add_relationships(resource, name, serializers)
4859
resource[:relationships] ||= {}
4960
resource[:relationships][name] ||= { data: [] }
50-
resource[:relationships][name][:data] += serializers.map { |serializer| { type: serializer.json_api_type, id: serializer.id.to_s } }
61+
resource[:relationships][name][:data] += serializers.map { |serializer| resource_identifier(serializer) }
5162
end
5263

5364
def add_relationship(resource, name, serializer, val=nil)
5465
resource[:relationships] ||= {}
55-
resource[:relationships][name] = { data: val }
5666

57-
if serializer && serializer.object
58-
resource[:relationships][name][:data] = { type: serializer.json_api_type, id: serializer.id.to_s }
59-
end
67+
resource[:relationships][name] ||= {}
68+
resource[:relationships][name][:data] = if val
69+
val
70+
elsif serializer && serializer.object
71+
resource_identifier(serializer)
72+
else
73+
nil
74+
end
6075
end
6176

6277
def add_included(resource_name, serializers, parent = nil)
@@ -100,15 +115,12 @@ def attributes_for_serializer(serializer, options)
100115

101116
def resource_object_for(serializer, options)
102117
options[:fields] = @fieldset && @fieldset.fields_for(serializer)
103-
options[:required_fields] = [:id, :json_api_type]
104118

105119
cache_check(serializer) do
106120
attributes = serializer.attributes(options)
121+
attributes.delete(:id)
107122

108-
result = {
109-
id: attributes.delete(:id).to_s,
110-
type: attributes.delete(:json_api_type)
111-
}
123+
result = resource_identifier(serializer)
112124

113125
result[:attributes] = attributes if attributes.any?
114126
result

test/serializers/attributes_test.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ def test_attributes_with_fields_option
2323
@profile_serializer.attributes(fields: [:name]))
2424
end
2525

26-
def test_required_fields
27-
assert_equal({name: 'Name 1', description: 'Description 1'},
28-
@profile_serializer.attributes(fields: [:name, :description], required_fields: [:name]))
29-
30-
end
31-
3226
def test_attributes_inheritance_definition
3327
assert_equal([:id, :body], @serializer_klass._attributes)
3428
end

0 commit comments

Comments
 (0)