transforms type for polymorphic relationships too
This commit is contained in:
parent
5a70b1a686
commit
fced516356
@ -195,7 +195,7 @@ module FastJsonapi
|
||||
self.relationships_to_serialize = {} if relationships_to_serialize.nil?
|
||||
self.cachable_relationships_to_serialize = {} if cachable_relationships_to_serialize.nil?
|
||||
self.uncachable_relationships_to_serialize = {} if uncachable_relationships_to_serialize.nil?
|
||||
|
||||
|
||||
if !relationship.cached
|
||||
self.uncachable_relationships_to_serialize[relationship.name] = relationship
|
||||
else
|
||||
@ -249,7 +249,8 @@ module FastJsonapi
|
||||
relationship_type: relationship_type,
|
||||
cached: options[:cached],
|
||||
polymorphic: fetch_polymorphic_option(options),
|
||||
conditional_proc: options[:if]
|
||||
conditional_proc: options[:if],
|
||||
transform_method: @transform_method
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
module FastJsonapi
|
||||
class Relationship
|
||||
attr_reader :key, :name, :id_method_name, :record_type, :object_method_name, :object_block, :serializer, :relationship_type, :cached, :polymorphic, :conditional_proc
|
||||
attr_reader :key, :name, :id_method_name, :record_type, :object_method_name, :object_block, :serializer, :relationship_type, :cached, :polymorphic, :conditional_proc, :transform_method
|
||||
|
||||
def initialize(
|
||||
key:,
|
||||
@ -13,7 +13,8 @@ module FastJsonapi
|
||||
relationship_type:,
|
||||
cached: false,
|
||||
polymorphic:,
|
||||
conditional_proc:
|
||||
conditional_proc:,
|
||||
transform_method:
|
||||
)
|
||||
@key = key
|
||||
@name = name
|
||||
@ -26,6 +27,7 @@ module FastJsonapi
|
||||
@cached = cached
|
||||
@polymorphic = polymorphic
|
||||
@conditional_proc = conditional_proc
|
||||
@transform_method = transform_method
|
||||
end
|
||||
|
||||
def serialize(record, serialization_params, output_hash)
|
||||
@ -68,7 +70,7 @@ module FastJsonapi
|
||||
|
||||
def id_hash_from_record(record, record_types)
|
||||
# memoize the record type within the record_types dictionary, then assigning to record_type:
|
||||
associated_record_type = record_types[record.class] ||= record.class.name.demodulize.underscore.to_sym
|
||||
associated_record_type = record_types[record.class] ||= run_key_transform(record.class.name.demodulize.underscore)
|
||||
id_hash(record.id, associated_record_type)
|
||||
end
|
||||
|
||||
@ -93,5 +95,13 @@ module FastJsonapi
|
||||
end
|
||||
record.public_send(id_method_name)
|
||||
end
|
||||
|
||||
def run_key_transform(input)
|
||||
if self.transform_method.present?
|
||||
input.to_s.send(*self.transform_method).to_sym
|
||||
else
|
||||
input.to_sym
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
51
spec/lib/object_serializer_polymorphic_spec.rb
Normal file
51
spec/lib/object_serializer_polymorphic_spec.rb
Normal file
@ -0,0 +1,51 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe FastJsonapi::ObjectSerializer do
|
||||
class List
|
||||
attr_accessor :id, :name, :items
|
||||
end
|
||||
|
||||
class ChecklistItem
|
||||
attr_accessor :id, :name
|
||||
end
|
||||
|
||||
class Car
|
||||
attr_accessor :id, :model, :year
|
||||
end
|
||||
|
||||
class ListSerializer
|
||||
include FastJsonapi::ObjectSerializer
|
||||
set_type :list
|
||||
attributes :name
|
||||
set_key_transform :dash
|
||||
has_many :items, polymorphic: true
|
||||
end
|
||||
|
||||
let(:car) do
|
||||
car = Car.new
|
||||
car.id = 1
|
||||
car.model = 'Toyota Corolla'
|
||||
car.year = 1987
|
||||
car
|
||||
end
|
||||
|
||||
let(:checklist_item) do
|
||||
checklist_item = ChecklistItem.new
|
||||
checklist_item.id = 2
|
||||
checklist_item.name = 'Do this action!'
|
||||
checklist_item
|
||||
end
|
||||
|
||||
context 'when serializing id and type of polymorphic relationships' do
|
||||
it 'should return correct type when transform_method is specified' do
|
||||
list = List.new
|
||||
list.id = 1
|
||||
list.items = [checklist_item, car]
|
||||
list_hash = ListSerializer.new(list).to_hash
|
||||
record_type = list_hash[:data][:relationships][:items][:data][0][:type]
|
||||
expect(record_type).to eq 'checklist-item'.to_sym
|
||||
record_type = list_hash[:data][:relationships][:items][:data][1][:type]
|
||||
expect(record_type).to eq 'car'.to_sym
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user