Minor code refactor
This commit is contained in:
parent
9c659839e4
commit
0c367d2574
@ -232,7 +232,11 @@ module FastJsonapi
|
||||
Relationship.new(
|
||||
key: options[:key] || run_key_transform(base_key),
|
||||
name: name,
|
||||
id_method_name: options[:id_method_name] || "#{base_serialization_key}#{id_postfix}".to_sym,
|
||||
id_method_name: compute_id_method_name(
|
||||
options[:id_method_name],
|
||||
"#{base_serialization_key}#{id_postfix}".to_sym,
|
||||
block
|
||||
),
|
||||
record_type: options[:record_type] || run_key_transform(base_key_sym),
|
||||
object_method_name: options[:object_method_name] || name,
|
||||
object_block: block,
|
||||
@ -240,13 +244,16 @@ module FastJsonapi
|
||||
relationship_type: relationship_type,
|
||||
cached: options[:cached],
|
||||
polymorphic: fetch_polymorphic_option(options),
|
||||
conditional_proc: options[:if],
|
||||
id_method_name_for_inferred_objects: compute_object_method_name_for_inferred_objects(options[:id_method_name], block)
|
||||
conditional_proc: options[:if]
|
||||
)
|
||||
end
|
||||
|
||||
def compute_object_method_name_for_inferred_objects(id_method_name, block)
|
||||
(id_method_name.present? && block.present?) ? id_method_name : :id
|
||||
def compute_id_method_name(custom_id_method_name, id_method_name_from_relationship, block)
|
||||
if block.present?
|
||||
custom_id_method_name || :id
|
||||
else
|
||||
custom_id_method_name || id_method_name_from_relationship
|
||||
end
|
||||
end
|
||||
|
||||
def compute_serializer_name(serializer_key)
|
||||
|
||||
@ -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, :id_method_name_for_inferred_objects
|
||||
attr_reader :key, :name, :id_method_name, :record_type, :object_method_name, :object_block, :serializer, :relationship_type, :cached, :polymorphic, :conditional_proc
|
||||
|
||||
def initialize(
|
||||
key:,
|
||||
@ -13,8 +13,7 @@ module FastJsonapi
|
||||
relationship_type:,
|
||||
cached: false,
|
||||
polymorphic:,
|
||||
conditional_proc:,
|
||||
id_method_name_for_inferred_objects:
|
||||
conditional_proc:
|
||||
)
|
||||
@key = key
|
||||
@name = name
|
||||
@ -27,7 +26,6 @@ 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)
|
||||
@ -90,8 +88,8 @@ module FastJsonapi
|
||||
def fetch_id(record, params)
|
||||
if object_block.present?
|
||||
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.try(id_method_name_for_inferred_objects)
|
||||
return object.map { |item| item.public_send(id_method_name) } if object.respond_to? :map
|
||||
return object.try(id_method_name)
|
||||
end
|
||||
record.public_send(id_method_name)
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user