Support for polymorphic id_method_name (#17)

* fix id method bugs, add specs
* Use SecureRandom.uuid
This commit is contained in:
Aubrey Holland 2019-12-03 13:04:04 -05:00 committed by Stas
parent 2d92ab4cf9
commit 1d7c18f5da
4 changed files with 46 additions and 7 deletions

View File

@ -130,6 +130,7 @@ module FastJsonapi
subclass.cached = cached
subclass.set_type(subclass.reflected_record_type) if subclass.reflected_record_type
subclass.meta_to_serialize = meta_to_serialize
subclass.record_id = record_id
end
def reflected_record_type
@ -241,12 +242,15 @@ module FastJsonapi
base_key_sym = name
id_postfix = '_id'
end
polymorphic = fetch_polymorphic_option(options)
Relationship.new(
key: options[:key] || run_key_transform(base_key),
name: name,
id_method_name: compute_id_method_name(
options[:id_method_name],
"#{base_serialization_key}#{id_postfix}".to_sym,
polymorphic,
block
),
record_type: options[:record_type] || run_key_transform(base_key_sym),
@ -255,7 +259,7 @@ module FastJsonapi
serializer: compute_serializer_name(options[:serializer] || base_key_sym),
relationship_type: relationship_type,
cached: options[:cached],
polymorphic: fetch_polymorphic_option(options),
polymorphic: polymorphic,
conditional_proc: options[:if],
transform_method: @transform_method,
links: options[:links],
@ -263,8 +267,8 @@ module FastJsonapi
)
end
def compute_id_method_name(custom_id_method_name, id_method_name_from_relationship, block)
if block.present?
def compute_id_method_name(custom_id_method_name, id_method_name_from_relationship, polymorphic, block)
if block.present? || polymorphic
custom_id_method_name || :id
else
custom_id_method_name || id_method_name_from_relationship

View File

@ -78,7 +78,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] ||= run_key_transform(record.class.name.demodulize.underscore)
id_hash(record.id, associated_record_type)
id_hash(record.public_send(id_method_name), associated_record_type)
end
def ids_hash(ids)

View File

@ -20,7 +20,7 @@ describe FastJsonapi::ObjectSerializer do
end
class User
attr_accessor :id, :first_name, :last_name
attr_accessor :id, :first_name, :last_name, :uuid
attr_accessor :address_ids, :country_id
@ -39,6 +39,7 @@ describe FastJsonapi::ObjectSerializer do
class UserSerializer
include FastJsonapi::ObjectSerializer
set_type :user
set_id :uuid
attributes :first_name, :last_name
attribute :full_name do |user, params|
@ -125,6 +126,14 @@ describe FastJsonapi::ObjectSerializer do
EmployeeSerializer
expect(UserSerializer.attributes_to_serialize).not_to have_key(:location)
end
it 'inherits the id source' do
e = Employee.new
e.id = 2
e.uuid = SecureRandom.uuid
id = EmployeeSerializer.new(e).serializable_hash[:data][:id]
expect(id).to eq(e.uuid)
end
end
context 'when testing inheritance of relationship' do
@ -158,11 +167,9 @@ describe FastJsonapi::ObjectSerializer do
end
context 'when test inheritence of other attributes' do
it 'inherits the tranform method' do
EmployeeSerializer
expect(UserSerializer.transform_method).to eq EmployeeSerializer.transform_method
end
end
end

View File

@ -13,6 +13,10 @@ describe FastJsonapi::ObjectSerializer do
attr_accessor :id, :model, :year
end
class Animal
attr_accessor :id, :uuid, :species
end
class ListSerializer
include FastJsonapi::ObjectSerializer
set_type :list
@ -21,6 +25,13 @@ describe FastJsonapi::ObjectSerializer do
has_many :items, polymorphic: true
end
class ZooSerializer
include FastJsonapi::ObjectSerializer
set_type :list
attributes :name
has_many :items, polymorphic: true, id_method_name: :uuid
end
let(:car) do
car = Car.new
car.id = 1
@ -36,6 +47,14 @@ describe FastJsonapi::ObjectSerializer do
checklist_item
end
let(:animal) do
animal = Animal.new
animal.id = 1
animal.species = 'Mellivora capensis'
animal.uuid = SecureRandom.uuid
animal
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
@ -47,5 +66,14 @@ describe FastJsonapi::ObjectSerializer do
record_type = list_hash[:data][:relationships][:items][:data][1][:type]
expect(record_type).to eq 'car'.to_sym
end
it 'should use the correct id method on associated objects' do
list = List.new
list.id = 1
list.items = [animal]
list_hash = ZooSerializer.new(list).to_hash
id = list_hash[:data][:relationships][:items][:data][0][:id]
expect(id).to eq animal.uuid
end
end
end