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