Skip to content

WIP: make belongs_to polymorphic use id_method_name #384

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
7 changes: 7 additions & 0 deletions lib/fast_jsonapi/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ def ids_hash_from_record_and_relationship(record, params = {})

return unless associated_object = fetch_associated_object(record, params)

if relationship_type == :belongs_to
return {
id: fetch_id(record, params),
type: run_key_transform(associated_object.class.name.demodulize.underscore)
}
end

return associated_object.map do |object|
id_hash_from_record object, polymorphic
end if associated_object.respond_to? :map
Expand Down
48 changes: 47 additions & 1 deletion spec/lib/object_serializer_polymorphic_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
require 'spec_helper'

describe FastJsonapi::ObjectSerializer do
class ListOwner
attr_accessor :id, :uuid
end

class List
attr_accessor :id, :name, :items
attr_accessor :id, :name, :items, :owner, :owner_id, :owner_type, :owner_uuid
end

class ChecklistItem
Expand All @@ -19,6 +23,13 @@ class ListSerializer
attributes :name
set_key_transform :dash
has_many :items, polymorphic: true
belongs_to :owner, polymorphic: true, id_method_name: :owner_uuid
belongs_to :owner_block, polymorphic: true, id_method_name: :uuid do
owner = ListOwner.new
owner.id = 2
owner.uuid = 234234234
owner
end
end

let(:car) do
Expand All @@ -36,6 +47,13 @@ class ListSerializer
checklist_item
end

let(:owner) do
owner = ListOwner.new
owner.id = 1
owner.uuid = 123123123
owner
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 +65,33 @@ class ListSerializer
record_type = list_hash[:data][:relationships][:items][:data][1][:type]
expect(record_type).to eq 'car'.to_sym
end

it 'should return correct id for belongs_to when id_method_name is specified' do
list = List.new
list.id = 1
list.owner = owner
list.owner_id = owner.id
list.owner_type = owner.class.name
list.owner_uuid = owner.uuid
list_hash = ListSerializer.new(list).to_hash
record_uuid = list_hash[:data][:relationships][:owner][:data][:id]
expect(record_uuid).to eq list.owner_uuid
end

it 'should return nil for belongs_to when association is nil' do
list = List.new
list.id = 1
list_hash = ListSerializer.new(list).to_hash
owner_relationship = list_hash[:data][:relationships][:owner][:data]
expect(owner_relationship).to be_nil
end

it 'should return correct data for belongs_to with object_block when id_method_name is specified' do
list = List.new
list.id = 1
list_hash = ListSerializer.new(list).to_hash
owner_relationship = list_hash[:data][:relationships][:'owner-block'][:data]
expect(owner_relationship).to eq(id: 234234234, type: :'list-owner')
end
end
end