Skip to content

fix id method bugs #446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/fast_jsonapi/object_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def inherited(subclass)
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
Expand Down Expand Up @@ -241,12 +242,15 @@ def create_relationship(base_key, relationship_type, options, block)
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),
Expand All @@ -255,16 +259,16 @@ def create_relationship(base_key, relationship_type, options, block)
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],
lazy_load_data: options[:lazy_load_data]
)
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
Expand Down
2 changes: 1 addition & 1 deletion lib/fast_jsonapi/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def ids_hash_from_record_and_relationship(record, params = {})
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)
Expand Down
13 changes: 10 additions & 3 deletions spec/lib/object_serializer_inheritance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
end

class User
attr_accessor :id, :first_name, :last_name
attr_accessor :id, :first_name, :last_name, :uuid

attr_accessor :address_ids, :country_id

Expand All @@ -39,6 +39,7 @@ def photo_id
class UserSerializer
include FastJsonapi::ObjectSerializer
set_type :user
set_id :uuid
attributes :first_name, :last_name

attribute :full_name do |user, params|
Expand Down Expand Up @@ -125,6 +126,14 @@ class EmployeeSerializer < UserSerializer
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 = 'dfsdfsd'
id = EmployeeSerializer.new(e).serializable_hash[:data][:id]
expect(id).to eq('dfsdfsd')
end
end

context 'when testing inheritance of relationship' do
Expand Down Expand Up @@ -158,11 +167,9 @@ class EmployeeSerializer < UserSerializer
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
28 changes: 28 additions & 0 deletions spec/lib/object_serializer_polymorphic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class Car
attr_accessor :id, :model, :year
end

class Animal
attr_accessor :id, :uuid, :species
end

class ListSerializer
include FastJsonapi::ObjectSerializer
set_type :list
Expand All @@ -21,6 +25,13 @@ class ListSerializer
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
Expand All @@ -36,6 +47,14 @@ class ListSerializer
checklist_item
end

let(:animal) do
animal = Animal.new
animal.id = 1
animal.species = 'Mellivora capensis'
animal.uuid = 'sdfsdfds'
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
Expand All @@ -47,5 +66,14 @@ class ListSerializer
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