From fced516356fd4ccb07b1bf17810c71f2bd0cc36e Mon Sep 17 00:00:00 2001 From: Shishir Kakaraddi Date: Wed, 19 Sep 2018 20:16:32 -0700 Subject: [PATCH] transforms type for polymorphic relationships too --- lib/fast_jsonapi/object_serializer.rb | 5 +- lib/fast_jsonapi/relationship.rb | 18 +++++-- .../lib/object_serializer_polymorphic_spec.rb | 51 +++++++++++++++++++ 3 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 spec/lib/object_serializer_polymorphic_spec.rb diff --git a/lib/fast_jsonapi/object_serializer.rb b/lib/fast_jsonapi/object_serializer.rb index 183d622..a5436f4 100644 --- a/lib/fast_jsonapi/object_serializer.rb +++ b/lib/fast_jsonapi/object_serializer.rb @@ -195,7 +195,7 @@ module FastJsonapi self.relationships_to_serialize = {} if relationships_to_serialize.nil? self.cachable_relationships_to_serialize = {} if cachable_relationships_to_serialize.nil? self.uncachable_relationships_to_serialize = {} if uncachable_relationships_to_serialize.nil? - + if !relationship.cached self.uncachable_relationships_to_serialize[relationship.name] = relationship else @@ -249,7 +249,8 @@ module FastJsonapi relationship_type: relationship_type, cached: options[:cached], polymorphic: fetch_polymorphic_option(options), - conditional_proc: options[:if] + conditional_proc: options[:if], + transform_method: @transform_method ) end diff --git a/lib/fast_jsonapi/relationship.rb b/lib/fast_jsonapi/relationship.rb index 6acc0a3..e06b07f 100644 --- a/lib/fast_jsonapi/relationship.rb +++ b/lib/fast_jsonapi/relationship.rb @@ -1,6 +1,6 @@ module FastJsonapi class Relationship - attr_reader :key, :name, :id_method_name, :record_type, :object_method_name, :object_block, :serializer, :relationship_type, :cached, :polymorphic, :conditional_proc + attr_reader :key, :name, :id_method_name, :record_type, :object_method_name, :object_block, :serializer, :relationship_type, :cached, :polymorphic, :conditional_proc, :transform_method def initialize( key:, @@ -13,7 +13,8 @@ module FastJsonapi relationship_type:, cached: false, polymorphic:, - conditional_proc: + conditional_proc:, + transform_method: ) @key = key @name = name @@ -26,6 +27,7 @@ module FastJsonapi @cached = cached @polymorphic = polymorphic @conditional_proc = conditional_proc + @transform_method = transform_method end def serialize(record, serialization_params, output_hash) @@ -68,7 +70,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] ||= record.class.name.demodulize.underscore.to_sym + associated_record_type = record_types[record.class] ||= run_key_transform(record.class.name.demodulize.underscore) id_hash(record.id, associated_record_type) end @@ -93,5 +95,13 @@ module FastJsonapi end record.public_send(id_method_name) end + + def run_key_transform(input) + if self.transform_method.present? + input.to_s.send(*self.transform_method).to_sym + else + input.to_sym + end + end end -end \ No newline at end of file +end diff --git a/spec/lib/object_serializer_polymorphic_spec.rb b/spec/lib/object_serializer_polymorphic_spec.rb new file mode 100644 index 0000000..f889141 --- /dev/null +++ b/spec/lib/object_serializer_polymorphic_spec.rb @@ -0,0 +1,51 @@ +require 'spec_helper' + +describe FastJsonapi::ObjectSerializer do + class List + attr_accessor :id, :name, :items + end + + class ChecklistItem + attr_accessor :id, :name + end + + class Car + attr_accessor :id, :model, :year + end + + class ListSerializer + include FastJsonapi::ObjectSerializer + set_type :list + attributes :name + set_key_transform :dash + has_many :items, polymorphic: true + end + + let(:car) do + car = Car.new + car.id = 1 + car.model = 'Toyota Corolla' + car.year = 1987 + car + end + + let(:checklist_item) do + checklist_item = ChecklistItem.new + checklist_item.id = 2 + checklist_item.name = 'Do this action!' + checklist_item + 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 + list.id = 1 + list.items = [checklist_item, car] + list_hash = ListSerializer.new(list).to_hash + record_type = list_hash[:data][:relationships][:items][:data][0][:type] + expect(record_type).to eq 'checklist-item'.to_sym + record_type = list_hash[:data][:relationships][:items][:data][1][:type] + expect(record_type).to eq 'car'.to_sym + end + end +end