transforms type for polymorphic relationships too

This commit is contained in:
Shishir Kakaraddi 2018-09-19 20:16:32 -07:00 committed by Shishir Kakaraddi
parent 5a70b1a686
commit fced516356
3 changed files with 68 additions and 6 deletions

View File

@ -249,7 +249,8 @@ module FastJsonapi
relationship_type: relationship_type, relationship_type: relationship_type,
cached: options[:cached], cached: options[:cached],
polymorphic: fetch_polymorphic_option(options), polymorphic: fetch_polymorphic_option(options),
conditional_proc: options[:if] conditional_proc: options[:if],
transform_method: @transform_method
) )
end end

View File

@ -1,6 +1,6 @@
module FastJsonapi module FastJsonapi
class Relationship 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( def initialize(
key:, key:,
@ -13,7 +13,8 @@ module FastJsonapi
relationship_type:, relationship_type:,
cached: false, cached: false,
polymorphic:, polymorphic:,
conditional_proc: conditional_proc:,
transform_method:
) )
@key = key @key = key
@name = name @name = name
@ -26,6 +27,7 @@ module FastJsonapi
@cached = cached @cached = cached
@polymorphic = polymorphic @polymorphic = polymorphic
@conditional_proc = conditional_proc @conditional_proc = conditional_proc
@transform_method = transform_method
end end
def serialize(record, serialization_params, output_hash) def serialize(record, serialization_params, output_hash)
@ -68,7 +70,7 @@ module FastJsonapi
def id_hash_from_record(record, record_types) def id_hash_from_record(record, record_types)
# memoize the record type within the record_types dictionary, then assigning to record_type: # 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) id_hash(record.id, associated_record_type)
end end
@ -93,5 +95,13 @@ module FastJsonapi
end end
record.public_send(id_method_name) record.public_send(id_method_name)
end 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 end

View 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