scinote-web/config/initializers/extends/report_extends.rb

217 lines
7.3 KiB
Ruby
Raw Normal View History

2017-03-08 21:14:03 +08:00
#########################################################
# EXTENDS METHODS. Here you can extend the arrays, #
# hashes,.. which is used in methods. Please specify #
# the method name and location! #
#########################################################
module ReportExtends
# path: app/controllers/concerns/ReportActions
# method: generate_module_contents_json
# ModuleElement struct creates an argument objects which is needed in
# generate_module_contents_json method. It takes 3 parameters a Proc and
# additional options wich can be extended.
# :values => name of the hook/identifier for specific module element state
2017-03-08 21:14:03 +08:00
# :element => name of module element in plural
# :children => bolean if element has children elements in report
# :locals => an array of names of local variables which are passed in the view
# :coll => a procedure which the my_module is passed and have to return a
# collection of elements
# :singular => true by defaut; change the enum type to singular - needed when
# querying partials by name
# :has_many => false by default; whether the element can have many manifestations,
# and its id will be appended.
2017-03-08 21:14:03 +08:00
ModuleElement = Struct.new(:values,
:element,
2017-03-08 21:14:03 +08:00
:children,
:locals,
2017-03-09 18:48:50 +08:00
:coll,
:singular,
:has_many) do
def initialize(values, element, children, locals, coll = nil, singular = true, has_many = false)
super(values, element, children, locals, coll, singular, has_many)
2017-03-08 21:14:03 +08:00
end
def collection(my_module, params2)
coll.call(my_module, params2) if coll
2017-03-08 21:14:03 +08:00
end
def parse_locals(values)
container = {}
locals.each_with_index do |local, index|
container[local] = values[index]
end
container
end
2017-03-09 18:48:50 +08:00
def file_name
return element.to_s unless singular
element.to_s.singularize
end
2017-03-08 21:14:03 +08:00
end
# Module contents element
MODULE_CONTENTS = [
ModuleElement.new(%i(completed_steps uncompleted_steps),
:steps,
2017-03-08 21:14:03 +08:00
true,
[:step],
proc do |my_module, params2|
steps = []
steps << true if params2["module_completed_steps"] == '1'
steps << false if params2["module_uncompleted_steps"] == '1'
my_module.protocol.steps.where(completed: steps).order(:position)
2017-03-08 21:14:03 +08:00
end),
ModuleElement.new([:result_assets],
:result_assets,
2017-03-08 21:14:03 +08:00
true,
[:result],
proc do |my_module|
my_module.results.select { |r| r.is_asset && r.active? }
end),
ModuleElement.new([:result_tables],
:result_tables,
2017-03-08 21:14:03 +08:00
true,
[:result],
proc do |my_module|
my_module.results.select { |r| r.is_table && r.active? }
end),
ModuleElement.new([:result_texts],
:result_texts,
2017-03-08 21:14:03 +08:00
true,
[:result],
proc do |my_module|
my_module.results.select { |r| r.is_text && r.active? }
end),
ModuleElement.new([:activity],
:activity,
2017-03-08 21:14:03 +08:00
false,
[:my_module, :order]),
ModuleElement.new([:samples],
:samples,
2017-03-08 21:14:03 +08:00
false,
[:my_module, :order]),
ModuleElement.new([:repository],
:repository,
false,
[:my_module, :order],
nil,
true,
true)
2017-03-08 21:14:03 +08:00
]
# path: app/helpers/reports_helpers.rb
# method: render_report_element
# adds :order local to listed elements views
2017-03-09 17:51:54 +08:00
# ADD REPORT ELEMENT TYPE WHICH YOU WANT TO PASS 'ORDER' LOCAL IN THE PARTIAL
2017-03-08 21:14:03 +08:00
SORTED_ELEMENTS = %w(my_module_activity
my_module_samples
step_comments
result_comments)
# sets local :my_module to the listed my_module child elements
MY_MODULE_ELEMENTS = %w(my_module my_module_activity my_module_samples)
# sets local name to first element of the listed elements
2017-03-09 23:02:52 +08:00
FIRST_PART_ELEMENTS = %w(result_comments
result_text
result_asset
result_table
project_header
step_comments)
2017-03-08 21:14:03 +08:00
# path: app/models/report_element.rb
# method: set_element_reference
ElementReference = Struct.new(:checker, :element) do
def initialize(checker, element = :element_reference_needed!)
super(checker, element)
end
def check(report_element)
checker.call(report_element)
end
end
SET_ELEMENT_REFERENCES_LIST = [
ElementReference.new(
proc do |report_element|
report_element.project_header? ||
report_element.project_activity? ||
report_element.project_samples?
end,
'project_id'
),
ElementReference.new(proc(&:experiment?), 'experiment_id'),
ElementReference.new(
proc do |report_element|
report_element.my_module? ||
report_element.my_module_activity? ||
report_element.my_module_samples?
end,
'my_module_id'
),
ElementReference.new(
proc do |report_element|
report_element.step? || report_element.step_comments?
end,
'step_id'
),
ElementReference.new(
proc do |report_element|
report_element.result_asset? ||
report_element.result_table? ||
report_element.result_text? ||
report_element.result_comments?
end,
'result_id'
),
ElementReference.new(proc(&:step_checklist?), 'checklist_id'),
ElementReference.new(proc(&:step_asset?), 'asset_id'),
ElementReference.new(proc(&:step_table?), 'table_id')
]
# path: app/models/report_element.rb
# method: element_reference
ELEMENT_REFERENCES = [
ElementReference.new(
proc do |report_element|
report_element.project_header? ||
report_element.project_activity? ||
report_element.project_samples?
end,
'project_id'
),
ElementReference.new(proc(&:experiment?), 'experiment_id'),
ElementReference.new(
proc do |report_element|
report_element.my_module? ||
report_element.my_module_activity? ||
report_element.my_module_samples?
end,
'my_module_id'
),
ElementReference.new(
proc do |report_element|
report_element.step? ||
report_element.step_comments?
end,
'step_id'
),
ElementReference.new(
proc do |report_element|
report_element.result_asset? ||
report_element.result_table? ||
report_element.result_text? ||
report_element.result_comments?
end,
'result_id'
),
ElementReference.new(proc(&:step_checklist?), 'checklist_id'),
ElementReference.new(proc(&:step_asset?), 'asset_id'),
ElementReference.new(proc(&:step_table?), 'table_id')
]
end