scinote-web/app/models/report_element.rb

106 lines
2.9 KiB
Ruby
Raw Normal View History

2019-05-09 23:10:02 +08:00
# frozen_string_literal: true
2017-06-23 21:19:08 +08:00
class ReportElement < ApplicationRecord
enum type_of: Extends::REPORT_ELEMENT_TYPES
2016-02-12 23:52:43 +08:00
# This is only used by certain elements
enum sort_order: {
asc: 0,
desc: 1
}
validates :position, presence: true
validates :report, presence: true
validates :type_of, presence: true
validate :has_one_of_referenced_elements
2019-05-09 23:10:02 +08:00
belongs_to :report, inverse_of: :report_elements
2016-02-12 23:52:43 +08:00
# Hierarchical structure representation
2017-03-09 17:51:54 +08:00
has_many :children,
-> { order(:position) },
class_name: 'ReportElement',
foreign_key: 'parent_id',
dependent: :destroy
2017-06-28 21:21:32 +08:00
belongs_to :parent, class_name: 'ReportElement', optional: true
2016-02-12 23:52:43 +08:00
# References to various report entities
2017-06-28 21:21:32 +08:00
belongs_to :project, inverse_of: :report_elements, optional: true
belongs_to :experiment, inverse_of: :report_elements, optional: true
belongs_to :my_module, inverse_of: :report_elements, optional: true
belongs_to :step, inverse_of: :report_elements, optional: true
belongs_to :result, inverse_of: :report_elements, optional: true
belongs_to :checklist, inverse_of: :report_elements, optional: true
belongs_to :asset, inverse_of: :report_elements, optional: true
belongs_to :table, inverse_of: :report_elements, optional: true
belongs_to :repository, inverse_of: :report_elements, optional: true,
foreign_key: :repository_id, class_name: 'RepositoryBase'
2016-02-12 23:52:43 +08:00
def has_children?
children.length > 0
end
def result?
result_asset? or result_table? or result_text?
end
def comments?
step_comments? or result_comments?
end
# Get the referenced elements (previously, element's type_of must be set)
def element_references
2017-03-08 21:14:03 +08:00
ReportExtends::ELEMENT_REFERENCES.each do |el_ref|
2017-06-08 00:22:15 +08:00
if el_ref.check(self)
return el_ref.elements.map { |el| eval(el.gsub('_id', '')) }
end
2016-02-12 23:52:43 +08:00
end
end
# Set the element references (previously, element's type_of must be set)
def set_element_references(ref_ids)
2017-03-08 21:14:03 +08:00
ReportExtends::SET_ELEMENT_REFERENCES_LIST.each do |el_ref|
check = el_ref.check(self)
next unless check
el_ref.elements.each do |element|
public_send("#{element}=", ref_ids[element])
end
2017-03-09 23:02:52 +08:00
break
2016-02-12 23:52:43 +08:00
end
end
2016-07-21 19:11:15 +08:00
# removes element that are archived or deleted
def clean_removed_or_archived_elements
parent_model = ''
2017-06-08 00:22:15 +08:00
%w(project
experiment
my_module
step
result
checklist
asset
table
repository)
.each do |el|
parent_model = el if send el
2016-07-21 19:11:15 +08:00
end
if parent_model == 'experiment'
destroy unless send(parent_model).project == report.project
2016-07-21 19:11:15 +08:00
else
destroy unless (send(parent_model).active? rescue send(parent_model))
2016-07-21 19:11:15 +08:00
end
end
2016-02-12 23:52:43 +08:00
private
def has_one_of_referenced_elements
element_references.each do |el|
2017-06-08 00:22:15 +08:00
next unless el.nil?
errors.add(:base,
'Report element doesn\'t have correct element references.')
break
2016-02-12 23:52:43 +08:00
end
end
2016-07-21 19:11:15 +08:00
end