Skip to content

Commit 4523508

Browse files
committed
inherits attributes, relationships and other settings from parent serializer
1 parent f4f289a commit 4523508

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

lib/fast_jsonapi/object_serializer.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ def is_collection?(resource)
9696
end
9797

9898
class_methods do
99+
100+
def inherited(subclass)
101+
super(subclass)
102+
subclass.attributes_to_serialize = attributes_to_serialize.dup if attributes_to_serialize.present?
103+
subclass.relationships_to_serialize = relationships_to_serialize.dup if relationships_to_serialize.present?
104+
subclass.cachable_relationships_to_serialize = cachable_relationships_to_serialize.dup if cachable_relationships_to_serialize.present?
105+
subclass.uncachable_relationships_to_serialize = uncachable_relationships_to_serialize.dup if uncachable_relationships_to_serialize.present?
106+
subclass.transform_method = transform_method
107+
subclass.cache_length = cache_length
108+
subclass.race_condition_ttl = race_condition_ttl
109+
subclass.cached = cached
110+
end
111+
99112
def reflected_record_type
100113
return @reflected_record_type if defined?(@reflected_record_type)
101114

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
require 'spec_helper'
2+
3+
describe FastJsonapi::ObjectSerializer do
4+
5+
after(:all) do
6+
classes_to_remove = %i[
7+
User
8+
UserSerializer
9+
Country
10+
CountrySerializer
11+
Employee
12+
EmployeeSerializer
13+
]
14+
classes_to_remove.each do |klass_name|
15+
Object.send(:remove_const, klass_name) if Object.constants.include?(klass_name)
16+
end
17+
end
18+
19+
class User
20+
attr_accessor :id, :first_name, :last_name
21+
22+
attr_accessor :address_ids, :country_id, :photo_id
23+
end
24+
25+
class UserSerializer
26+
include FastJsonapi::ObjectSerializer
27+
set_type :user
28+
attributes :first_name, :last_name
29+
30+
attribute :full_name do |user, params|
31+
"#{user.first_name} #{user.last_name}"
32+
end
33+
34+
has_many :addresses, cached: true
35+
belongs_to :country
36+
has_one :photo
37+
end
38+
39+
class Country
40+
attr_accessor :id, :name
41+
end
42+
43+
class CountrySerializer
44+
include FastJsonapi::ObjectSerializer
45+
attributes :name
46+
end
47+
48+
class Employee < User
49+
attr_accessor :id, :location, :compensation
50+
attr_accessor :account_id
51+
end
52+
53+
class EmployeeSerializer < UserSerializer
54+
include FastJsonapi::ObjectSerializer
55+
attributes :location
56+
attributes :compensation
57+
58+
has_one :account
59+
end
60+
61+
context 'when testing inheritance of attributes' do
62+
63+
it 'includes parent attributes' do
64+
subclass_attributes = EmployeeSerializer.attributes_to_serialize
65+
superclass_attributes = UserSerializer.attributes_to_serialize
66+
expect(subclass_attributes).to include(superclass_attributes)
67+
end
68+
69+
it 'returns inherited attribute with a block correctly' do
70+
e = Employee.new
71+
e.id = 1
72+
e.first_name = 'S'
73+
e.last_name = 'K'
74+
attributes_hash = EmployeeSerializer.new(e).serializable_hash[:data][:attributes]
75+
expect(attributes_hash).to include(full_name: 'S K')
76+
end
77+
78+
it 'includes child attributes' do
79+
expect(EmployeeSerializer.attributes_to_serialize[:location]).to eq(:location)
80+
end
81+
82+
it 'doesnt change parent class attributes' do
83+
EmployeeSerializer
84+
expect(UserSerializer.attributes_to_serialize).not_to have_key(:location)
85+
end
86+
end
87+
88+
context 'when testing inheritance of relationship' do
89+
it 'includes parent relationships' do
90+
subclass_relationships = EmployeeSerializer.relationships_to_serialize
91+
superclass_relationships = UserSerializer.relationships_to_serialize
92+
expect(subclass_relationships).to include(superclass_relationships)
93+
end
94+
95+
it 'returns inherited relationship correctly' do
96+
e = Employee.new
97+
e.country_id = 1
98+
relationships_hash = EmployeeSerializer.new(e).serializable_hash[:data][:relationships][:country]
99+
expect(relationships_hash).to include(data: { id: "1", type: :country })
100+
end
101+
102+
it 'includes child relationships' do
103+
expect(EmployeeSerializer.relationships_to_serialize.keys).to include(:account)
104+
end
105+
106+
it 'doesnt change parent class attributes' do
107+
EmployeeSerializer
108+
expect(UserSerializer.relationships_to_serialize.keys).not_to include(:account)
109+
end
110+
111+
it 'includes parent cached relationships' do
112+
subclass_relationships = EmployeeSerializer.cachable_relationships_to_serialize
113+
superclass_relationships = UserSerializer.cachable_relationships_to_serialize
114+
expect(subclass_relationships).to include(superclass_relationships)
115+
end
116+
end
117+
118+
context 'when test inheritence of other attributes' do
119+
120+
it 'inherits the tranform method' do
121+
EmployeeSerializer
122+
expect(UserSerializer.transform_method).to eq EmployeeSerializer.transform_method
123+
end
124+
125+
end
126+
end

0 commit comments

Comments
 (0)