Skip to content

Commit 5c82069

Browse files
TrevorHinesleyshishirmk
authored andcommitted
Split attribute serialization into its own class
1 parent f1df3f4 commit 5c82069

5 files changed

Lines changed: 40 additions & 24 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module FastJsonapi
2+
class AttributeSerializer
3+
attr_reader :key, :method, :conditional_proc
4+
5+
def initialize(key:, method:, options: {})
6+
@key = key
7+
@method = method
8+
@conditional_proc = options[:if]
9+
end
10+
11+
def serialize(record, serialization_params, output_hash)
12+
if include_attribute?(record, serialization_params)
13+
output_hash[key] = if method.is_a?(Proc)
14+
method.arity == 1 ? method.call(record) : method.call(record, serialization_params)
15+
else
16+
record.public_send(method)
17+
end
18+
end
19+
end
20+
21+
def include_attribute?(record, serialization_params)
22+
if conditional_proc.present?
23+
conditional_proc.call(record, serialization_params)
24+
else
25+
true
26+
end
27+
end
28+
end
29+
end

lib/fast_jsonapi/object_serializer.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'active_support/core_ext/object'
44
require 'active_support/concern'
55
require 'active_support/inflector'
6+
require 'fast_jsonapi/attribute_serializer'
67
require 'fast_jsonapi/serialization_core'
78

89
module FastJsonapi
@@ -151,16 +152,15 @@ def attributes(*attributes_list, &block)
151152
attributes_list = attributes_list.first if attributes_list.first.class.is_a?(Array)
152153
options = attributes_list.last.is_a?(Hash) ? attributes_list.pop : {}
153154
self.attributes_to_serialize = {} if self.attributes_to_serialize.nil?
154-
self.optional_attributes_to_serialize = {} if self.optional_attributes_to_serialize.nil?
155155

156156
attributes_list.each do |attr_name|
157157
method_name = attr_name
158158
key = run_key_transform(method_name)
159-
if options[:if].present?
160-
optional_attributes_to_serialize[key] = [method_name, options[:if]]
161-
else
162-
attributes_to_serialize[key] = block || method_name
163-
end
159+
attributes_to_serialize[key] = AttributeSerializer.new(
160+
key: key,
161+
method: block || method_name,
162+
options: options
163+
)
164164
end
165165
end
166166

lib/fast_jsonapi/serialization_core.rb

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ module SerializationCore
1212
included do
1313
class << self
1414
attr_accessor :attributes_to_serialize,
15-
:optional_attributes_to_serialize,
1615
:relationships_to_serialize,
1716
:cachable_relationships_to_serialize,
1817
:uncachable_relationships_to_serialize,
@@ -74,21 +73,9 @@ def links_hash(record, params = {})
7473
end
7574

7675
def attributes_hash(record, params = {})
77-
attributes = attributes_to_serialize.each_with_object({}) do |(key, method), attr_hash|
78-
attr_hash[key] = if method.is_a?(Proc)
79-
method.arity == 1 ? method.call(record) : method.call(record, params)
80-
else
81-
record.public_send(method)
82-
end
76+
attributes_to_serialize.each_with_object({}) do |(key, attribute), attr_hash|
77+
attribute.serialize(record, params, attr_hash)
8378
end
84-
85-
self.optional_attributes_to_serialize = {} if self.optional_attributes_to_serialize.nil?
86-
optional_attributes_to_serialize.each do |key, details|
87-
method_name, check_proc = details
88-
attributes[key] = record.send(method_name) if check_proc.call(record, params)
89-
end
90-
91-
attributes
9279
end
9380

9481
def relationships_hash(record, relationships = nil, params = {})

spec/lib/object_serializer_inheritance_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class EmployeeSerializer < UserSerializer
113113
end
114114

115115
it 'includes child attributes' do
116-
expect(EmployeeSerializer.attributes_to_serialize[:location]).to eq(:location)
116+
expect(EmployeeSerializer.attributes_to_serialize[:location].method).to eq(:location)
117117
end
118118

119119
it 'doesnt change parent class attributes' do

spec/lib/serialization_core_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
attributes_hash = MovieSerializer.send(:attributes_hash, movie)
4040
attribute_names = attributes_hash.keys.sort
4141
expect(attribute_names).to eq MovieSerializer.attributes_to_serialize.keys.sort
42-
MovieSerializer.attributes_to_serialize.each do |key, method_name|
42+
MovieSerializer.attributes_to_serialize.each do |key, attribute|
4343
value = attributes_hash[key]
44-
expect(value).to eq movie.send(method_name)
44+
expect(value).to eq movie.send(attribute.method)
4545
end
4646
end
4747

0 commit comments

Comments
 (0)