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,
cached: options[:cached],
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
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)
return serializer_key unless serializer_key.is_a? Symbol
namespace = self.name.gsub(/()?\w+Serializer$/, '')

View File

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

View File

@ -87,6 +87,31 @@ describe FastJsonapi::ObjectSerializer do
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
subject(:relationship) { MovieSerializer.relationships_to_serialize[:area] }

View File

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