2019-10-14 20:50:05 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-08 21:14:03 +08:00
|
|
|
module ReportActions
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
def in_params?(val)
|
|
|
|
params.include? val and params[val] == '1'
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_new_el(hide)
|
|
|
|
el = {}
|
|
|
|
el[:html] = render_to_string(
|
|
|
|
partial: 'reports/elements/new_element.html.erb',
|
|
|
|
locals: { hide: hide }
|
|
|
|
)
|
|
|
|
el[:children] = []
|
|
|
|
el[:new_element] = true
|
|
|
|
el
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_el(partial, locals)
|
|
|
|
el = {}
|
|
|
|
el[:html] = render_to_string(
|
|
|
|
partial: partial,
|
|
|
|
locals: locals
|
|
|
|
)
|
|
|
|
el[:children] = []
|
|
|
|
el[:new_element] = false
|
|
|
|
el
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_project_contents_json
|
|
|
|
res = []
|
|
|
|
if params.include? :modules
|
2019-10-14 20:50:05 +08:00
|
|
|
module_ids = (params[:modules].select { |_, p| p == '1' }).keys.collect(&:to_i)
|
2017-03-08 21:14:03 +08:00
|
|
|
|
|
|
|
# Get unique experiments from given modules
|
2019-10-14 20:50:05 +08:00
|
|
|
experiments = @project.experiments.distinct.joins(:my_modules).where('my_modules.id': module_ids)
|
2017-03-08 21:14:03 +08:00
|
|
|
experiments.each do |experiment|
|
|
|
|
res << generate_new_el(false)
|
|
|
|
el = generate_el(
|
|
|
|
'reports/elements/experiment_element.html.erb',
|
|
|
|
experiment: experiment
|
|
|
|
)
|
2019-10-17 19:30:14 +08:00
|
|
|
selected_modules = experiment.my_modules.includes(:tags).where(id: module_ids)
|
2019-10-14 20:50:05 +08:00
|
|
|
el[:children] = generate_experiment_contents_json(selected_modules)
|
2017-03-08 21:14:03 +08:00
|
|
|
res << el
|
|
|
|
end
|
|
|
|
end
|
|
|
|
res << generate_new_el(false)
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2019-10-14 20:50:05 +08:00
|
|
|
def generate_experiment_contents_json(selected_modules)
|
2017-03-08 21:14:03 +08:00
|
|
|
res = []
|
2019-10-14 20:50:05 +08:00
|
|
|
selected_modules.order(:workflow_order).each do |my_module|
|
2017-03-08 21:14:03 +08:00
|
|
|
res << generate_new_el(false)
|
|
|
|
el = generate_el(
|
|
|
|
'reports/elements/my_module_element.html.erb',
|
|
|
|
my_module: my_module
|
|
|
|
)
|
|
|
|
el[:children] = generate_module_contents_json(my_module)
|
|
|
|
res << el
|
|
|
|
end
|
|
|
|
res << generate_new_el(false)
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_module_contents_json(my_module)
|
|
|
|
res = []
|
2019-04-25 22:16:43 +08:00
|
|
|
|
2017-03-08 21:14:03 +08:00
|
|
|
ReportExtends::MODULE_CONTENTS.each do |contents|
|
2017-06-02 19:36:06 +08:00
|
|
|
elements = []
|
2017-05-17 19:11:42 +08:00
|
|
|
contents.values.each do |element|
|
2017-06-02 19:36:06 +08:00
|
|
|
if contents.has_many
|
2017-06-08 00:26:32 +08:00
|
|
|
elements = params.select { |k| k.starts_with?("module_#{element}") }
|
2019-10-14 20:50:05 +08:00
|
|
|
elements = elements.select { |_, v| v == '1' }.keys
|
|
|
|
elements.map! { |el| el.gsub('module_', '') }.map! { |el| el.split('_') }
|
2017-06-08 00:26:32 +08:00
|
|
|
elements.map! { |el| [el[0].to_sym, el[1].to_i] }
|
2017-06-08 00:22:15 +08:00
|
|
|
break unless elements.empty?
|
2017-06-02 19:36:06 +08:00
|
|
|
else
|
2019-10-14 20:50:05 +08:00
|
|
|
present = in_params?("module_#{element}".to_sym) || in_params?(element.to_sym)
|
2017-06-02 19:36:06 +08:00
|
|
|
if present
|
|
|
|
elements << [element.to_sym, nil]
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2017-05-17 19:11:42 +08:00
|
|
|
end
|
2017-06-08 00:22:15 +08:00
|
|
|
next if elements.empty?
|
2017-05-17 19:11:42 +08:00
|
|
|
|
2017-06-08 00:22:15 +08:00
|
|
|
elements.each do |_, el_id|
|
2017-06-02 19:36:06 +08:00
|
|
|
if contents.children
|
|
|
|
contents.collection(my_module, params).each do |report_el|
|
|
|
|
res << generate_new_el(false)
|
|
|
|
locals = contents.parse_locals([report_el])
|
2017-06-08 00:22:15 +08:00
|
|
|
locals[:element_id] = el_id if el_id
|
2017-06-02 19:36:06 +08:00
|
|
|
el = generate_el(
|
|
|
|
"reports/elements/my_module_#{contents.element.to_s.singularize}"\
|
|
|
|
"_element.html.erb",
|
|
|
|
locals
|
|
|
|
)
|
|
|
|
if :step.in? contents.locals
|
|
|
|
el[:children] = generate_step_contents_json(report_el)
|
|
|
|
elsif :result.in? contents.locals
|
|
|
|
el[:children] = generate_result_contents_json(report_el)
|
|
|
|
end
|
|
|
|
res << el
|
|
|
|
end
|
|
|
|
else
|
|
|
|
file_name = contents.file_name
|
|
|
|
file_name = contents.element if contents.element == :samples
|
2017-05-12 22:04:19 +08:00
|
|
|
res << generate_new_el(false)
|
2017-06-02 19:36:06 +08:00
|
|
|
locals = contents.parse_locals([my_module, :asc])
|
2017-06-08 00:22:15 +08:00
|
|
|
locals[:element_id] = el_id if el_id
|
2017-06-02 19:36:06 +08:00
|
|
|
res << generate_el(
|
|
|
|
"reports/elements/my_module_#{file_name}_element.html.erb",
|
|
|
|
locals
|
2017-03-08 21:14:03 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
res << generate_new_el(false)
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_step_contents_json(step)
|
|
|
|
res = []
|
2017-03-09 23:02:52 +08:00
|
|
|
if in_params? :step_checklists
|
2017-05-04 21:17:34 +08:00
|
|
|
step.checklists.asc.each do |checklist|
|
2017-03-08 21:14:03 +08:00
|
|
|
res << generate_new_el(false)
|
|
|
|
res << generate_el(
|
2019-10-14 20:50:05 +08:00
|
|
|
'reports/elements/step_checklist_element.html.erb', checklist: checklist
|
2017-03-08 21:14:03 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2017-03-09 23:02:52 +08:00
|
|
|
if in_params? :step_assets
|
2017-03-08 21:14:03 +08:00
|
|
|
step.assets.each do |asset|
|
|
|
|
res << generate_new_el(false)
|
|
|
|
res << generate_el(
|
2019-10-14 20:50:05 +08:00
|
|
|
'reports/elements/step_asset_element.html.erb', asset: asset
|
2017-03-08 21:14:03 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2017-03-09 23:02:52 +08:00
|
|
|
if in_params? :step_tables
|
2017-03-08 21:14:03 +08:00
|
|
|
step.tables.each do |table|
|
|
|
|
res << generate_new_el(false)
|
|
|
|
res << generate_el(
|
2019-10-14 20:50:05 +08:00
|
|
|
'reports/elements/step_table_element.html.erb', table: table
|
2017-03-08 21:14:03 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2017-03-09 23:02:52 +08:00
|
|
|
if in_params? :step_comments
|
2017-03-08 21:14:03 +08:00
|
|
|
res << generate_new_el(false)
|
|
|
|
res << generate_el(
|
2019-10-14 20:50:05 +08:00
|
|
|
'reports/elements/step_comments_element.html.erb', step: step, order: :asc
|
2017-03-08 21:14:03 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
res << generate_new_el(false)
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_result_contents_json(result)
|
|
|
|
res = []
|
|
|
|
if in_params? :result_comments
|
|
|
|
res << generate_new_el(true)
|
|
|
|
res << generate_el(
|
2019-10-14 20:50:05 +08:00
|
|
|
'reports/elements/result_comments_element.html.erb', result: result, order: :asc
|
2017-03-08 21:14:03 +08:00
|
|
|
)
|
|
|
|
else
|
|
|
|
res << generate_new_el(false)
|
|
|
|
end
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
|
|
|
def elements_empty?(elements)
|
2019-10-14 20:50:05 +08:00
|
|
|
return true if elements.blank? || elements.count.zero?
|
2017-03-08 21:14:03 +08:00
|
|
|
|
|
|
|
if elements.count == 1
|
|
|
|
el = elements[0]
|
|
|
|
return true if el.include?(:new_element) && el[:new_element]
|
2019-10-14 20:50:05 +08:00
|
|
|
|
2017-03-08 21:14:03 +08:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|