Skip to content

Commit 6581201

Browse files
committed
support serializer options in multimodel_jsonapi
1 parent bb9a36e commit 6581201

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

lib/rails_jsonapi/railtie.rb

+21-10
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,18 @@ def register_multimodel_jsonapi_renderer
165165
#
166166
# @yieldparam [Object,Array<Object>] resource
167167
# @yieldparam [Hash] options
168-
# @option options [Hash<Class>] :multimodel_jsonapi
168+
# @option options [Hash<Class>] :multimodel_options
169169
# - serializer_class_proc [Class,Proc]
170-
# each class may have the following keys:
171-
# - :type [Symbol]
172-
# - :id_attr [Symbol]
173-
# - :serializer_class [Class]
170+
# - klass, each class may have the following keys:
171+
# - :sort_type [Symbol]
172+
# - :sort_id_attr [Symbol]
173+
# - :options
174174
# *any other options for the serializer class
175175
ActionController::Renderers.add(:multimodel_jsonapi) do |resource, options|
176+
176177
multimodel_options = options.delete(:multimodel_options) || {}
178+
179+
# root defaults
177180
multimodel_options[:serializer_class_proc] ||= ->(klass_name){ "#{klass_name}Serializer" }
178181

179182
payload = {data: [], included: []}
@@ -183,24 +186,32 @@ def register_multimodel_jsonapi_renderer
183186

184187
# serialize data and add it to the payload
185188
grouped_records.each do |klass_name, records|
189+
190+
# by klass defaults
191+
klass_options = multimodel_options[klass_name.safe_constantize] ||= {}
192+
klass_options[:sort_type] ||= klass_name.underscore
193+
klass_options[:sort_id_attr] ||= :id
194+
klass_options[:options] ||= {}
195+
186196
serialized_data = multimodel_options[:serializer_class_proc].call(klass_name)
187-
.safe_constantize.new(records, options)
197+
.safe_constantize.new(records, klass_options[:options])
188198
.serializable_hash
189199

190200
payload[:data].concat(serialized_data[:data]) if serialized_data[:data]
191201
payload[:included].concat(serialized_data[:included]) if serialized_data[:included]
192202
end
193203

194204
# sort the data based on original resource
205+
cache = {}
195206
payload[:data].sort! do |obj|
196-
obj_type = obj[:type].to_s.underscore.singularize
207+
obj_type = cache[obj[:type]] ||= obj[:type].to_s.underscore.singularize
197208
obj_id = obj[:id].to_s
198209

199210
resource.index do |r|
200-
name = multimodel_options.dig(r.class, :type)&.to_s || r.class.name.underscore
201-
id_attr = multimodel_options.dig(r.class, :id_attr) || :id
211+
sort_type = multimodel_options.dig(r.class, :sort_type)
212+
sort_id_attr = multimodel_options.dig(r.class, :sort_id_attr)
202213

203-
r.public_send(id_attr).to_s == obj_id && name == obj_type
214+
r.public_send(sort_id_attr).to_s == obj_id && sort_type.to_s == obj_type
204215
end
205216
end
206217

lib/rails_jsonapi/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module RailsJSONAPI
2-
VERSION = '0.1.8'
2+
VERSION = '0.1.9'
33
end

0 commit comments

Comments
 (0)