Skip to content

Commit fced516

Browse files
committed
transforms type for polymorphic relationships too
1 parent 5a70b1a commit fced516

3 files changed

Lines changed: 68 additions & 6 deletions

File tree

lib/fast_jsonapi/object_serializer.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def add_relationship(relationship)
195195
self.relationships_to_serialize = {} if relationships_to_serialize.nil?
196196
self.cachable_relationships_to_serialize = {} if cachable_relationships_to_serialize.nil?
197197
self.uncachable_relationships_to_serialize = {} if uncachable_relationships_to_serialize.nil?
198-
198+
199199
if !relationship.cached
200200
self.uncachable_relationships_to_serialize[relationship.name] = relationship
201201
else
@@ -249,7 +249,8 @@ def create_relationship(base_key, relationship_type, options, block)
249249
relationship_type: relationship_type,
250250
cached: options[:cached],
251251
polymorphic: fetch_polymorphic_option(options),
252-
conditional_proc: options[:if]
252+
conditional_proc: options[:if],
253+
transform_method: @transform_method
253254
)
254255
end
255256

lib/fast_jsonapi/relationship.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module FastJsonapi
22
class Relationship
3-
attr_reader :key, :name, :id_method_name, :record_type, :object_method_name, :object_block, :serializer, :relationship_type, :cached, :polymorphic, :conditional_proc
3+
attr_reader :key, :name, :id_method_name, :record_type, :object_method_name, :object_block, :serializer, :relationship_type, :cached, :polymorphic, :conditional_proc, :transform_method
44

55
def initialize(
66
key:,
@@ -13,7 +13,8 @@ def initialize(
1313
relationship_type:,
1414
cached: false,
1515
polymorphic:,
16-
conditional_proc:
16+
conditional_proc:,
17+
transform_method:
1718
)
1819
@key = key
1920
@name = name
@@ -26,6 +27,7 @@ def initialize(
2627
@cached = cached
2728
@polymorphic = polymorphic
2829
@conditional_proc = conditional_proc
30+
@transform_method = transform_method
2931
end
3032

3133
def serialize(record, serialization_params, output_hash)
@@ -68,7 +70,7 @@ def ids_hash_from_record_and_relationship(record, params = {})
6870

6971
def id_hash_from_record(record, record_types)
7072
# memoize the record type within the record_types dictionary, then assigning to record_type:
71-
associated_record_type = record_types[record.class] ||= record.class.name.demodulize.underscore.to_sym
73+
associated_record_type = record_types[record.class] ||= run_key_transform(record.class.name.demodulize.underscore)
7274
id_hash(record.id, associated_record_type)
7375
end
7476

@@ -93,5 +95,13 @@ def fetch_id(record, params)
9395
end
9496
record.public_send(id_method_name)
9597
end
98+
99+
def run_key_transform(input)
100+
if self.transform_method.present?
101+
input.to_s.send(*self.transform_method).to_sym
102+
else
103+
input.to_sym
104+
end
105+
end
96106
end
97-
end
107+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require 'spec_helper'
2+
3+
describe FastJsonapi::ObjectSerializer do
4+
class List
5+
attr_accessor :id, :name, :items
6+
end
7+
8+
class ChecklistItem
9+
attr_accessor :id, :name
10+
end
11+
12+
class Car
13+
attr_accessor :id, :model, :year
14+
end
15+
16+
class ListSerializer
17+
include FastJsonapi::ObjectSerializer
18+
set_type :list
19+
attributes :name
20+
set_key_transform :dash
21+
has_many :items, polymorphic: true
22+
end
23+
24+
let(:car) do
25+
car = Car.new
26+
car.id = 1
27+
car.model = 'Toyota Corolla'
28+
car.year = 1987
29+
car
30+
end
31+
32+
let(:checklist_item) do
33+
checklist_item = ChecklistItem.new
34+
checklist_item.id = 2
35+
checklist_item.name = 'Do this action!'
36+
checklist_item
37+
end
38+
39+
context 'when serializing id and type of polymorphic relationships' do
40+
it 'should return correct type when transform_method is specified' do
41+
list = List.new
42+
list.id = 1
43+
list.items = [checklist_item, car]
44+
list_hash = ListSerializer.new(list).to_hash
45+
record_type = list_hash[:data][:relationships][:items][:data][0][:type]
46+
expect(record_type).to eq 'checklist-item'.to_sym
47+
record_type = list_hash[:data][:relationships][:items][:data][1][:type]
48+
expect(record_type).to eq 'car'.to_sym
49+
end
50+
end
51+
end

0 commit comments

Comments
 (0)