Refactor the records generation method [8007]

Co-Authored-By: okriuchykhin <oleksii@scinote.net>
This commit is contained in:
sboursen-scinote 2023-03-15 09:14:01 +01:00
parent d2ceeb9662
commit dcbe1615cf

View file

@ -40,21 +40,16 @@ class ProtocolLinkedChildrenDatatable < CustomDatatable
# Query database for records (this will be later paginated and filtered) # Query database for records (this will be later paginated and filtered)
# after that "data" function will return json # after that "data" function will return json
def get_raw_records def get_raw_records
linked_protocols = Protocol if params[:version].present?
.joins(my_module: { experiment: :project }) records = @protocol.published_versions_with_original
.includes(my_module: { experiment: :project }) .find_by!(version_number: params[:version])
.references(my_module: { experiment: :project }) .linked_children
.where(protocol_type: Protocol.protocol_types[:linked]) else
records = Protocol.where(protocol_type: Protocol.protocol_types[:linked])
records = linked_protocols records = records.where(parent_id: @protocol.published_versions)
.where(parent: @protocol) .or(records.where(parent_id: @protocol.id))
.or( end
linked_protocols.where(parent: Protocol.where(parent: @protocol)) records.preload(my_module: { experiment: :project }).distinct
)
records = filter_child_records(records)
records.distinct
end end
# Helper methods # Helper methods
@ -83,11 +78,4 @@ class ProtocolLinkedChildrenDatatable < CustomDatatable
res += '</ol>' res += '</ol>'
res res
end end
def filter_child_records(records)
if params[:version].present?
records = records.left_outer_joins(:parent).where(parent: { version_number: params[:version] })
end
records
end
end end