Evaluate ids via the specified ‘id_method_name’ when relationships are evaluated via a block

This commit is contained in:
Manoj M J 2018-07-18 11:11:47 +05:30
parent 5ff3fa97da
commit 9c659839e4
4 changed files with 40 additions and 9 deletions

View File

@ -240,10 +240,15 @@ module FastJsonapi
relationship_type: relationship_type, relationship_type: relationship_type,
cached: options[:cached], cached: options[:cached],
polymorphic: fetch_polymorphic_option(options), polymorphic: fetch_polymorphic_option(options),
conditional_proc: options[:if] conditional_proc: options[:if],
id_method_name_for_inferred_objects: compute_object_method_name_for_inferred_objects(options[:id_method_name], block)
) )
end end
def compute_object_method_name_for_inferred_objects(id_method_name, block)
(id_method_name.present? && block.present?) ? id_method_name : :id
end
def compute_serializer_name(serializer_key) def compute_serializer_name(serializer_key)
return serializer_key unless serializer_key.is_a? Symbol return serializer_key unless serializer_key.is_a? Symbol
namespace = self.name.gsub(/()?\w+Serializer$/, '') namespace = self.name.gsub(/()?\w+Serializer$/, '')

View File

@ -1,6 +1,6 @@
module FastJsonapi module FastJsonapi
class Relationship 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, :id_method_name_for_inferred_objects
def initialize( def initialize(
key:, key:,
@ -13,7 +13,8 @@ module FastJsonapi
relationship_type:, relationship_type:,
cached: false, cached: false,
polymorphic:, polymorphic:,
conditional_proc: conditional_proc:,
id_method_name_for_inferred_objects:
) )
@key = key @key = key
@name = name @name = name
@ -26,6 +27,7 @@ module FastJsonapi
@cached = cached @cached = cached
@polymorphic = polymorphic @polymorphic = polymorphic
@conditional_proc = conditional_proc @conditional_proc = conditional_proc
@id_method_name_for_inferred_objects = id_method_name_for_inferred_objects
end end
def serialize(record, serialization_params, output_hash) def serialize(record, serialization_params, output_hash)
@ -86,13 +88,11 @@ module FastJsonapi
end end
def fetch_id(record, params) def fetch_id(record, params)
unless object_block.nil? if object_block.present?
object = object_block.call(record, params) object = object_block.call(record, params)
return object.map { |item| item.public_send(id_method_name_for_inferred_objects) } if object.respond_to? :map
return object.map(&:id) if object.respond_to? :map return object.try(id_method_name_for_inferred_objects)
return object.try(:id)
end end
record.public_send(id_method_name) record.public_send(id_method_name)
end end
end end

View File

@ -87,6 +87,31 @@ describe FastJsonapi::ObjectSerializer do
end end
end end
describe '#has_many with block and id_method_name' do
before do
MovieSerializer.has_many(:awards, id_method_name: :imdb_award_id) do |movie|
movie.actors.map(&:awards).flatten
end
end
after do
MovieSerializer.relationships_to_serialize.delete(:awards)
end
context 'awards is not included' do
subject(:hash) { MovieSerializer.new(movie).serializable_hash }
it 'returns correct hash where id is obtained from the method specified via `id_method_name`' do
expected_award_data = movie.actors.map(&:awards).flatten.map do |actor|
{ id: actor.imdb_award_id.to_s, type: actor.class.name.downcase.to_sym }
end
serialized_award_data = hash[:data][:relationships][:awards][:data]
expect(serialized_award_data).to eq(expected_award_data)
end
end
end
describe '#belongs_to' do describe '#belongs_to' do
subject(:relationship) { MovieSerializer.relationships_to_serialize[:area] } subject(:relationship) { MovieSerializer.relationships_to_serialize[:area] }

View File

@ -80,6 +80,7 @@ RSpec.shared_context 'movie class' do
a.id = i a.id = i
a.title = "Test Award #{i}" a.title = "Test Award #{i}"
a.actor_id = id a.actor_id = id
a.imdb_award_id = i * 10
end end
end end
end end
@ -110,7 +111,7 @@ RSpec.shared_context 'movie class' do
end end
class Award class Award
attr_accessor :id, :title, :actor_id attr_accessor :id, :title, :actor_id, :imdb_award_id
end end
class State class State