Enable to use block to define relationship
This commit is contained in:
parent
4523508c5b
commit
e39de8c8c4
@ -181,21 +181,21 @@ module FastJsonapi
|
|||||||
self.relationships_to_serialize[name] = relationship
|
self.relationships_to_serialize[name] = relationship
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_many(relationship_name, options = {})
|
def has_many(relationship_name, options = {}, &block)
|
||||||
name = relationship_name.to_sym
|
name = relationship_name.to_sym
|
||||||
hash = create_relationship_hash(relationship_name, :has_many, options)
|
hash = create_relationship_hash(relationship_name, :has_many, options, block)
|
||||||
add_relationship(name, hash)
|
add_relationship(name, hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_one(relationship_name, options = {})
|
def has_one(relationship_name, options = {}, &block)
|
||||||
name = relationship_name.to_sym
|
name = relationship_name.to_sym
|
||||||
hash = create_relationship_hash(relationship_name, :has_one, options)
|
hash = create_relationship_hash(relationship_name, :has_one, options, block)
|
||||||
add_relationship(name, hash)
|
add_relationship(name, hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
alias belongs_to has_one
|
alias belongs_to has_one
|
||||||
|
|
||||||
def create_relationship_hash(base_key, relationship_type, options)
|
def create_relationship_hash(base_key, relationship_type, options, block)
|
||||||
name = base_key.to_sym
|
name = base_key.to_sym
|
||||||
if relationship_type == :has_many
|
if relationship_type == :has_many
|
||||||
base_serialization_key = base_key.to_s.singularize
|
base_serialization_key = base_key.to_s.singularize
|
||||||
@ -212,6 +212,7 @@ module FastJsonapi
|
|||||||
id_method_name: options[:id_method_name] || "#{base_serialization_key}#{id_postfix}".to_sym,
|
id_method_name: options[:id_method_name] || "#{base_serialization_key}#{id_postfix}".to_sym,
|
||||||
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,
|
||||||
serializer: compute_serializer_name(options[:serializer] || base_key_sym),
|
serializer: compute_serializer_name(options[:serializer] || base_key_sym),
|
||||||
relationship_type: relationship_type,
|
relationship_type: relationship_type,
|
||||||
cached: options[:cached] || false,
|
cached: options[:cached] || false,
|
||||||
|
@ -48,11 +48,11 @@ module FastJsonapi
|
|||||||
polymorphic = relationship[:polymorphic]
|
polymorphic = relationship[:polymorphic]
|
||||||
|
|
||||||
return ids_hash(
|
return ids_hash(
|
||||||
record.public_send(relationship[:id_method_name]),
|
fetch_id(record, relationship),
|
||||||
relationship[:record_type]
|
relationship[:record_type]
|
||||||
) unless polymorphic
|
) unless polymorphic
|
||||||
|
|
||||||
return unless associated_object = record.send(relationship[:object_method_name])
|
return unless associated_object = fetch_associated_object(record, relationship)
|
||||||
|
|
||||||
return associated_object.map do |object|
|
return associated_object.map do |object|
|
||||||
id_hash_from_record object, polymorphic
|
id_hash_from_record object, polymorphic
|
||||||
@ -117,9 +117,7 @@ module FastJsonapi
|
|||||||
|
|
||||||
def get_included_records(record, includes_list, known_included_objects)
|
def get_included_records(record, includes_list, known_included_objects)
|
||||||
includes_list.each_with_object([]) do |item, included_records|
|
includes_list.each_with_object([]) do |item, included_records|
|
||||||
included_objects = record.send(
|
included_objects = fetch_associated_object(record, @relationships_to_serialize[item])
|
||||||
@relationships_to_serialize[item][:object_method_name]
|
|
||||||
)
|
|
||||||
next if included_objects.blank?
|
next if included_objects.blank?
|
||||||
|
|
||||||
record_type = @relationships_to_serialize[item][:record_type]
|
record_type = @relationships_to_serialize[item][:record_type]
|
||||||
@ -134,6 +132,22 @@ module FastJsonapi
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def fetch_associated_object(record, relationship)
|
||||||
|
return relationship[:object_block].call(record) unless relationship[:object_block].nil?
|
||||||
|
record.send(relationship[:object_method_name])
|
||||||
|
end
|
||||||
|
|
||||||
|
def fetch_id(record, relationship)
|
||||||
|
unless relationship[:object_block].nil?
|
||||||
|
object = relationship[:object_block].call(record)
|
||||||
|
|
||||||
|
return object.map(&:id) if object.respond_to? :map
|
||||||
|
return object.id
|
||||||
|
end
|
||||||
|
|
||||||
|
record.public_send(relationship[:id_method_name])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user