Support for polymorphic id_method_name (#17)
* fix id method bugs, add specs * Use SecureRandom.uuid
This commit is contained in:
parent
2d92ab4cf9
commit
1d7c18f5da
@ -130,6 +130,7 @@ module FastJsonapi
|
|||||||
subclass.cached = cached
|
subclass.cached = cached
|
||||||
subclass.set_type(subclass.reflected_record_type) if subclass.reflected_record_type
|
subclass.set_type(subclass.reflected_record_type) if subclass.reflected_record_type
|
||||||
subclass.meta_to_serialize = meta_to_serialize
|
subclass.meta_to_serialize = meta_to_serialize
|
||||||
|
subclass.record_id = record_id
|
||||||
end
|
end
|
||||||
|
|
||||||
def reflected_record_type
|
def reflected_record_type
|
||||||
@ -241,12 +242,15 @@ module FastJsonapi
|
|||||||
base_key_sym = name
|
base_key_sym = name
|
||||||
id_postfix = '_id'
|
id_postfix = '_id'
|
||||||
end
|
end
|
||||||
|
polymorphic = fetch_polymorphic_option(options)
|
||||||
|
|
||||||
Relationship.new(
|
Relationship.new(
|
||||||
key: options[:key] || run_key_transform(base_key),
|
key: options[:key] || run_key_transform(base_key),
|
||||||
name: name,
|
name: name,
|
||||||
id_method_name: compute_id_method_name(
|
id_method_name: compute_id_method_name(
|
||||||
options[:id_method_name],
|
options[:id_method_name],
|
||||||
"#{base_serialization_key}#{id_postfix}".to_sym,
|
"#{base_serialization_key}#{id_postfix}".to_sym,
|
||||||
|
polymorphic,
|
||||||
block
|
block
|
||||||
),
|
),
|
||||||
record_type: options[:record_type] || run_key_transform(base_key_sym),
|
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),
|
serializer: compute_serializer_name(options[:serializer] || base_key_sym),
|
||||||
relationship_type: relationship_type,
|
relationship_type: relationship_type,
|
||||||
cached: options[:cached],
|
cached: options[:cached],
|
||||||
polymorphic: fetch_polymorphic_option(options),
|
polymorphic: polymorphic,
|
||||||
conditional_proc: options[:if],
|
conditional_proc: options[:if],
|
||||||
transform_method: @transform_method,
|
transform_method: @transform_method,
|
||||||
links: options[:links],
|
links: options[:links],
|
||||||
@ -263,8 +267,8 @@ module FastJsonapi
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def compute_id_method_name(custom_id_method_name, id_method_name_from_relationship, block)
|
def compute_id_method_name(custom_id_method_name, id_method_name_from_relationship, polymorphic, block)
|
||||||
if block.present?
|
if block.present? || polymorphic
|
||||||
custom_id_method_name || :id
|
custom_id_method_name || :id
|
||||||
else
|
else
|
||||||
custom_id_method_name || id_method_name_from_relationship
|
custom_id_method_name || id_method_name_from_relationship
|
||||||
|
|||||||
@ -78,7 +78,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] ||= run_key_transform(record.class.name.demodulize.underscore)
|
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
|
end
|
||||||
|
|
||||||
def ids_hash(ids)
|
def ids_hash(ids)
|
||||||
|
|||||||
@ -20,7 +20,7 @@ describe FastJsonapi::ObjectSerializer do
|
|||||||
end
|
end
|
||||||
|
|
||||||
class User
|
class User
|
||||||
attr_accessor :id, :first_name, :last_name
|
attr_accessor :id, :first_name, :last_name, :uuid
|
||||||
|
|
||||||
attr_accessor :address_ids, :country_id
|
attr_accessor :address_ids, :country_id
|
||||||
|
|
||||||
@ -39,6 +39,7 @@ describe FastJsonapi::ObjectSerializer do
|
|||||||
class UserSerializer
|
class UserSerializer
|
||||||
include FastJsonapi::ObjectSerializer
|
include FastJsonapi::ObjectSerializer
|
||||||
set_type :user
|
set_type :user
|
||||||
|
set_id :uuid
|
||||||
attributes :first_name, :last_name
|
attributes :first_name, :last_name
|
||||||
|
|
||||||
attribute :full_name do |user, params|
|
attribute :full_name do |user, params|
|
||||||
@ -125,6 +126,14 @@ describe FastJsonapi::ObjectSerializer do
|
|||||||
EmployeeSerializer
|
EmployeeSerializer
|
||||||
expect(UserSerializer.attributes_to_serialize).not_to have_key(:location)
|
expect(UserSerializer.attributes_to_serialize).not_to have_key(:location)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
context 'when testing inheritance of relationship' do
|
context 'when testing inheritance of relationship' do
|
||||||
@ -158,11 +167,9 @@ describe FastJsonapi::ObjectSerializer do
|
|||||||
end
|
end
|
||||||
|
|
||||||
context 'when test inheritence of other attributes' do
|
context 'when test inheritence of other attributes' do
|
||||||
|
|
||||||
it 'inherits the tranform method' do
|
it 'inherits the tranform method' do
|
||||||
EmployeeSerializer
|
EmployeeSerializer
|
||||||
expect(UserSerializer.transform_method).to eq EmployeeSerializer.transform_method
|
expect(UserSerializer.transform_method).to eq EmployeeSerializer.transform_method
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -13,6 +13,10 @@ describe FastJsonapi::ObjectSerializer do
|
|||||||
attr_accessor :id, :model, :year
|
attr_accessor :id, :model, :year
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Animal
|
||||||
|
attr_accessor :id, :uuid, :species
|
||||||
|
end
|
||||||
|
|
||||||
class ListSerializer
|
class ListSerializer
|
||||||
include FastJsonapi::ObjectSerializer
|
include FastJsonapi::ObjectSerializer
|
||||||
set_type :list
|
set_type :list
|
||||||
@ -21,6 +25,13 @@ describe FastJsonapi::ObjectSerializer do
|
|||||||
has_many :items, polymorphic: true
|
has_many :items, polymorphic: true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class ZooSerializer
|
||||||
|
include FastJsonapi::ObjectSerializer
|
||||||
|
set_type :list
|
||||||
|
attributes :name
|
||||||
|
has_many :items, polymorphic: true, id_method_name: :uuid
|
||||||
|
end
|
||||||
|
|
||||||
let(:car) do
|
let(:car) do
|
||||||
car = Car.new
|
car = Car.new
|
||||||
car.id = 1
|
car.id = 1
|
||||||
@ -36,6 +47,14 @@ describe FastJsonapi::ObjectSerializer do
|
|||||||
checklist_item
|
checklist_item
|
||||||
end
|
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
|
context 'when serializing id and type of polymorphic relationships' do
|
||||||
it 'should return correct type when transform_method is specified' do
|
it 'should return correct type when transform_method is specified' do
|
||||||
list = List.new
|
list = List.new
|
||||||
@ -47,5 +66,14 @@ describe FastJsonapi::ObjectSerializer do
|
|||||||
record_type = list_hash[:data][:relationships][:items][:data][1][:type]
|
record_type = list_hash[:data][:relationships][:items][:data][1][:type]
|
||||||
expect(record_type).to eq 'car'.to_sym
|
expect(record_type).to eq 'car'.to_sym
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user