diff --git a/app/controllers/concerns/report_actions.rb b/app/controllers/concerns/report_actions.rb new file mode 100644 index 000000000..50deeeedc --- /dev/null +++ b/app/controllers/concerns/report_actions.rb @@ -0,0 +1,169 @@ +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 + modules = (params[:modules].select { |_, p| p == '1' }) + .keys + .collect(&:to_i) + + # Get unique experiments from given modules + experiments = MyModule.where(id: modules).map(&:experiment).uniq + experiments.each do |experiment| + res << generate_new_el(false) + el = generate_el( + 'reports/elements/experiment_element.html.erb', + experiment: experiment + ) + el[:children] = generate_experiment_contents_json(experiment, modules) + res << el + end + end + res << generate_new_el(false) + res + end + + def generate_experiment_contents_json(experiment, selected_modules) + res = [] + experiment.my_modules.each do |my_module| + next unless selected_modules.include?(my_module.id) + + 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 = [] + ReportExtends::MODULE_CONTENTS.each do |contents| + protocol = contents.element == :step ? my_module.protocol.present? : true + next unless in_params?("module_#{contents.element}".to_sym) && protocol + res << generate_new_el(false) + if contents.children + contents.collection(my_module).each do |report_el| + el = generate_el( + "reports/elements/my_module_#{contents + .element + .to_s + .singularize}_element.html.erb", + contents.parse_locals([report_el]) + ) + if contents.element == :step + el[:children] = generate_step_contents_json(report_el) + elsif contents.element == :result + 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 + res << generate_el( + "reports/elements/my_module_#{file_name}_element.html.erb", + contents.parse_locals([my_module, :asc]) + ) + end + end + res << generate_new_el(false) + res + end + + def generate_step_contents_json(step) + res = [] + if in_params? :step_checklists + step.checklists.each do |checklist| + res << generate_new_el(false) + res << generate_el( + 'reports/elements/step_checklist_element.html.erb', + { checklist: checklist } + ) + end + end + if in_params? :step_assets + step.assets.each do |asset| + res << generate_new_el(false) + res << generate_el( + 'reports/elements/step_asset_element.html.erb', + { asset: asset } + ) + end + end + if in_params? :step_tables + step.tables.each do |table| + res << generate_new_el(false) + res << generate_el( + 'reports/elements/step_table_element.html.erb', + { table: table } + ) + end + end + if in_params? :step_comments + res << generate_new_el(false) + res << generate_el( + 'reports/elements/step_comments_element.html.erb', + { step: step, order: :asc } + ) + 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( + 'reports/elements/result_comments_element.html.erb', + { result: result, order: :asc } + ) + else + res << generate_new_el(false) + end + res + end + + def elements_empty?(elements) + return true if elements.blank? || elements.count == 0 + + if elements.count == 1 + el = elements[0] + return true if el.include?(:new_element) && el[:new_element] + return false + end + false + end +end diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index 1f5cb5ef8..9c33a4512 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -1,8 +1,9 @@ class ReportsController < ApplicationController include TeamsHelper + include ReportActions # Ignore CSRF protection just for PDF generation (because it's # used via target='_blank') - protect_from_forgery with: :exception, :except => :generate + protect_from_forgery with: :exception, except: :generate before_action :load_vars, only: [ :edit, @@ -28,7 +29,7 @@ class ReportsController < ApplicationController :result_contents ] - before_action :check_view_permissions, only: [:index] + before_action :check_view_permissions, only: :index before_action :check_create_permissions, only: [ :new, :create, @@ -46,9 +47,9 @@ class ReportsController < ApplicationController :step_contents, :result_contents ] - before_action :check_destroy_permissions, only: [:destroy] + before_action :check_destroy_permissions, only: :destroy - layout "fluid" + layout 'fluid' # Index showing all reports of a single project def index @@ -73,7 +74,7 @@ class ReportsController < ApplicationController @report.user = current_user @report.last_modified_by = current_user - if continue and @report.save_with_contents(report_contents) + if continue && @report.save_with_contents(report_contents) # record an activity Activity.create( type_of: :create_report, @@ -86,15 +87,15 @@ class ReportsController < ApplicationController ) ) respond_to do |format| - format.json { + format.json do render json: { url: project_reports_path(@project) }, status: :ok - } + end end else respond_to do |format| - format.json { + format.json do render json: @report.errors, status: :unprocessable_entity - } + end end end end @@ -118,7 +119,7 @@ class ReportsController < ApplicationController @report.last_modified_by = current_user @report.assign_attributes(report_params) - if continue and @report.save_with_contents(report_contents) + if continue && @report.save_with_contents(report_contents) # record an activity Activity.create( type_of: :edit_report, @@ -131,25 +132,21 @@ class ReportsController < ApplicationController ) ) respond_to do |format| - format.json { + format.json do render json: { url: project_reports_path(@project) }, status: :ok - } + end end else respond_to do |format| - format.json { + format.json do render json: @report.errors, status: :unprocessable_entity - } + end end end end # Destroy multiple entries action def destroy - unless params.include? :report_ids - render_404 - end - begin report_ids = JSON.parse(params[:report_ids]) rescue @@ -158,21 +155,19 @@ class ReportsController < ApplicationController report_ids.each do |report_id| report = Report.find_by_id(report_id) - - if report.present? - # record an activity - Activity.create( - type_of: :delete_report, - project: report.project, - user: current_user, - message: I18n.t( - 'activities.delete_report', - user: current_user.full_name, - report: report.name - ) + next unless report.present? + # record an activity + Activity.create( + type_of: :delete_report, + project: report.project, + user: current_user, + message: I18n.t( + 'activities.delete_report', + user: current_user.full_name, + report: report.name ) - report.destroy - end + ) + report.destroy end redirect_to project_reports_path(@project) @@ -182,15 +177,13 @@ class ReportsController < ApplicationController # Currently, only .PDF is supported def generate respond_to do |format| - format.pdf { + format.pdf do @html = params[:html] - if @html.blank? then - @html = "

No content

" - end - render pdf: "report", + @html = '

No content

' if @html.blank? + render pdf: 'report', header: { right: '[page] of [topage]' }, - template: "reports/report.pdf.erb" - } + template: 'reports/report.pdf.erb' + end end end @@ -209,33 +202,32 @@ class ReportsController < ApplicationController @url = project_report_path(@project, @report, format: :json) end - if !params.include? :contents - render_403 and return - end + render_403 and return unless params.include? :contents + @report_contents = params[:contents] respond_to do |format| - format.json { + format.json do render json: { - html: render_to_string({ - partial: "reports/new/modal/save.html.erb" - }) + html: render_to_string( + partial: 'reports/new/modal/save.html.erb' + ) } - } + end end end # Modal for adding contents into project element def project_contents_modal respond_to do |format| - format.json { + format.json do render json: { - html: render_to_string({ - partial: "reports/new/modal/project_contents.html.erb", + html: render_to_string( + partial: 'reports/new/modal/project_contents.html.erb', locals: { project: @project } - }) + ) } - } + end end end @@ -274,7 +266,7 @@ class ReportsController < ApplicationController format.json do render json: { html: render_to_string( - partial: "reports/new/modal/module_contents.html.erb", + partial: 'reports/new/modal/module_contents.html.erb', locals: { project: @project, my_module: my_module } ) } @@ -289,18 +281,18 @@ class ReportsController < ApplicationController respond_to do |format| if step.blank? - format.json { + format.json do render json: {}, status: :not_found - } + end else - format.json { + format.json do render json: { - html: render_to_string({ - partial: "reports/new/modal/step_contents.html.erb", + html: render_to_string( + partial: 'reports/new/modal/step_contents.html.erb', locals: { project: @project, step: step } - }) + ) } - } + end end end end @@ -311,18 +303,18 @@ class ReportsController < ApplicationController respond_to do |format| if result.blank? - format.json { + format.json do render json: {}, status: :not_found - } + end else - format.json { + format.json do render json: { - html: render_to_string({ - partial: "reports/new/modal/result_contents.html.erb", + html: render_to_string( + partial: 'reports/new/modal/result_contents.html.erb', locals: { project: @project, result: result } - }) + ) } - } + end end end end @@ -384,12 +376,12 @@ class ReportsController < ApplicationController if elements_empty? elements format.json { render json: {}, status: :no_content } else - format.json { + format.json do render json: { status: :ok, elements: elements } - } + end end end end @@ -442,242 +434,31 @@ class ReportsController < ApplicationController private - 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 - modules = (params[:modules].select { |_, p| p == '1' }) - .keys - .collect(&:to_i) - - # Get unique experiments from given modules - experiments = MyModule.where(id: modules).map(&:experiment).uniq - experiments.each do |experiment| - res << generate_new_el(false) - el = generate_el( - 'reports/elements/experiment_element.html.erb', - experiment: experiment - ) - el[:children] = generate_experiment_contents_json(experiment, modules) - res << el - end - end - res << generate_new_el(false) - res - end - - def generate_experiment_contents_json(experiment, selected_modules) - res = [] - experiment.my_modules.each do |my_module| - next unless selected_modules.include?(my_module.id) - - 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 = [] - if (in_params? :module_steps) && my_module.protocol.present? then - my_module.protocol.completed_steps.each do |step| - res << generate_new_el(false) - el = generate_el( - "reports/elements/step_element.html.erb", - { step: step } - ) - el[:children] = generate_step_contents_json(step) - res << el - end - end - if in_params? :module_result_assets then - (my_module.results.select { |r| r.is_asset && r.active? }).each do |result_asset| - res << generate_new_el(false) - el = generate_el( - "reports/elements/result_asset_element.html.erb", - { result: result_asset } - ) - el[:children] = generate_result_contents_json(result_asset) - res << el - end - end - if in_params? :module_result_tables then - (my_module.results.select { |r| r.is_table && r.active? }).each do |result_table| - res << generate_new_el(false) - el = generate_el( - "reports/elements/result_table_element.html.erb", - { result: result_table } - ) - el[:children] = generate_result_contents_json(result_table) - res << el - end - end - if in_params? :module_result_texts then - (my_module.results.select { |r| r.is_text && r.active? }).each do |result_text| - res << generate_new_el(false) - el = generate_el( - "reports/elements/result_text_element.html.erb", - result: result_text - ) - el[:children] = generate_result_contents_json(result_text) - res << el - end - end - if in_params? :module_activity then - res << generate_new_el(false) - res << generate_el( - "reports/elements/my_module_activity_element.html.erb", - { my_module: my_module, order: :asc } - ) - end - if in_params? :module_samples then - res << generate_new_el(false) - res << generate_el( - "reports/elements/my_module_samples_element.html.erb", - { my_module: my_module, order: :asc } - ) - end - res << generate_new_el(false) - res - end - - def generate_step_contents_json(step) - res = [] - if in_params? :step_checklists then - step.checklists.each do |checklist| - res << generate_new_el(false) - res << generate_el( - "reports/elements/step_checklist_element.html.erb", - { checklist: checklist } - ) - end - end - if in_params? :step_assets then - step.assets.each do |asset| - res << generate_new_el(false) - res << generate_el( - "reports/elements/step_asset_element.html.erb", - { asset: asset } - ) - end - end - if in_params? :step_tables then - step.tables.each do |table| - res << generate_new_el(false) - res << generate_el( - "reports/elements/step_table_element.html.erb", - { table: table } - ) - end - end - if in_params? :step_comments then - res << generate_new_el(false) - res << generate_el( - "reports/elements/step_comments_element.html.erb", - { step: step, order: :asc } - ) - end - res << generate_new_el(false) - res - end - - def generate_result_contents_json(result) - res = [] - if in_params? :result_comments then - res << generate_new_el(true) - res << generate_el( - "reports/elements/result_comments_element.html.erb", - { result: result, order: :asc } - ) - else - res << generate_new_el(false) - end - res - end - - def elements_empty?(elements) - if elements.blank? - return true - elsif elements.count == 0 then - return true - elsif elements.count == 1 - el = elements[0] - if el.include? :new_element and el[:new_element] - return true - else - return false - end - end - return false - end - def load_vars @report = Report.find_by_id(params[:id]) - - unless @report - render_404 - end + render_404 unless @report end def load_vars_nested @project = Project.find_by_id(params[:project_id]) - - unless @project - render_404 - end + render_404 unless @project end def check_view_permissions - unless can_view_reports(@project) - render_403 - end + render_403 unless can_view_reports(@project) end def check_create_permissions - unless can_create_new_report(@project) - render_403 - end + render_403 unless can_create_new_report(@project) end def check_destroy_permissions - unless can_delete_reports(@project) - render_403 - end + render_403 unless can_delete_reports(@project) + render_404 unless params.include? :report_ids end def report_params - params.require(:report).permit(:name, :description, :grouped_by, :report_contents) + params.require(:report) + .permit(:name, :description, :grouped_by, :report_contents) end - end diff --git a/app/helpers/reports_helper.rb b/app/helpers/reports_helper.rb index 091fe00d8..9c15582b7 100644 --- a/app/helpers/reports_helper.rb +++ b/app/helpers/reports_helper.rb @@ -1,99 +1,95 @@ module ReportsHelper + def render_new_element(hide) + render partial: 'reports/elements/new_element.html.erb', + locals: { hide: hide } + end -def render_new_element(hide) - render partial: "reports/elements/new_element.html.erb", - locals: { hide: hide } -end + def render_report_element(element, provided_locals = nil) + children_html = ''.html_safe -def render_report_element(element, provided_locals = nil) - children_html = ''.html_safe - - # First, recursively render element's children - if element.comments? || element.project_header? - # Render no children - elsif element.result? - # Special handling for result comments - if element.has_children? - children_html.safe_concat render_new_element(true) - element.children.each do |child| - children_html.safe_concat render_report_element(child, provided_locals) + # First, recursively render element's children + if element.comments? || element.project_header? + # Render no children + elsif element.result? + # Special handling for result comments + if element.has_children? + children_html.safe_concat render_new_element(true) + element.children.each do |child| + children_html + .safe_concat render_report_element(child, provided_locals) + end + else + children_html.safe_concat render_new_element(false) end else + if element.has_children? + element.children.each do |child| + children_html.safe_concat render_new_element(false) + children_html + .safe_concat render_report_element(child, provided_locals) + end + end children_html.safe_concat render_new_element(false) end - else - if element.has_children? - element.children.each do |child| - children_html.safe_concat render_new_element(false) - children_html.safe_concat render_report_element(child, provided_locals) - end + + file_name = element.type_of + if element.type_of.in? %w(step result_asset result_table result_text) + file_name = "my_module_#{element.type_of.singularize}" end - children_html.safe_concat render_new_element(false) + view = "reports/elements/#{file_name}_element.html.erb" + + locals = provided_locals.nil? ? {} : provided_locals.clone + locals[:children] = children_html + + # ReportExtends is located in config/initializers/extends/report_extends.rb + + ReportElement.type_ofs.keys.each do |type| + next unless element.public_send("#{type}?") + local_sym = type.split('_').last.to_sym + local_sym = type + .split('_') + .first + .to_sym if type.in? ReportExtends::FIRST_PART_ELEMENTS + local_sym = :my_module if type.in? ReportExtends::MY_MODULE_ELEMENTS + locals[local_sym] = element.element_reference + locals[:order] = element + .sort_order if type.in? ReportExtends::SORTED_ELEMENTS + end + + (render partial: view, locals: locals).html_safe end - view = "reports/elements/#{element.type_of}_element.html.erb" - - locals = provided_locals.nil? ? {} : provided_locals.clone - locals[:children] = children_html - - if element.project_header? - locals[:project] = element.element_reference - elsif element.experiment? - locals[:experiment] = element.element_reference - elsif element.my_module? - locals[:my_module] = element.element_reference - elsif element.step? - locals[:step] = element.element_reference - elsif element.result_asset? - locals[:result] = element.element_reference - elsif element.result_table? - locals[:result] = element.element_reference - elsif element.result_text? - locals[:result] = element.element_reference - elsif element.my_module_activity? - locals[:my_module] = element.element_reference - locals[:order] = element.sort_order - elsif element.my_module_samples? - locals[:my_module] = element.element_reference - locals[:order] = element.sort_order - elsif element.step_checklist? - locals[:checklist] = element.element_reference - elsif element.step_asset? - locals[:asset] = element.element_reference - elsif element.step_table? - locals[:table] = element.element_reference - elsif element.step_comments? - locals[:step] = element.element_reference - locals[:order] = element.sort_order - elsif element.result_comments? - locals[:result] = element.element_reference - locals[:order] = element.sort_order - elsif element.project_activity? - # TODO - elsif element.project_samples? - # TODO + # "Hack" to omit file preview URL because of WKHTML issues + def report_image_asset_url(asset) + prefix = '' + if ENV['PAPERCLIP_STORAGE'].present? && + ENV['MAIL_SERVER_URL'].present? && + ENV['PAPERCLIP_STORAGE'] == 'filesystem' + prefix = ENV['MAIL_SERVER_URL'] + end + if !prefix.empty? && + !prefix.include?('http://') && + !prefix.include?('https://') + prefix = "http://#{prefix}" + end + url = prefix + asset.url(:medium, timeout: Constants::URL_LONG_EXPIRE_TIME) + image_tag(url) end - return (render partial: view, locals: locals).html_safe -end + # "Hack" to load Glyphicons css directly from the CDN + # site so they work in report + def bootstrap_cdn_link_tag + specs = Gem.loaded_specs['bootstrap-sass'] + return '' unless specs.present? + stylesheet_link_tag("http://netdna.bootstrapcdn.com/bootstrap/" \ + "#{specs.version.version}/css/bootstrap.min.css", + media: 'all') + end -# "Hack" to omit file preview URL because of WKHTML issues -def report_image_asset_url(asset) - prefix = (ENV["PAPERCLIP_STORAGE"].present? && ENV["MAIL_SERVER_URL"].present? && ENV["PAPERCLIP_STORAGE"] == "filesystem") ? ENV["MAIL_SERVER_URL"] : "" - prefix = (!prefix.empty? && !prefix.include?("http://") && !prefix.include?("https://")) ? "http://#{prefix}" : prefix - url = prefix + asset.url(:medium, timeout: Constants::URL_LONG_EXPIRE_TIME) - image_tag(url) -end - -# "Hack" to load Glyphicons css directly from the CDN site so they work in report -def bootstrap_cdn_link_tag - specs = Gem.loaded_specs["bootstrap-sass"] - specs.present? ? stylesheet_link_tag("http://netdna.bootstrapcdn.com/bootstrap/#{specs.version.version}/css/bootstrap.min.css", media: "all") : "" -end - -def font_awesome_cdn_link_tag - stylesheet_link_tag( - 'https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css' - ) -end + def font_awesome_cdn_link_tag + stylesheet_link_tag( + 'https://maxcdn.bootstrapcdn.com/font-awesome' \ + '/4.6.3/css/font-awesome.min.css' + ) + end end diff --git a/app/models/report.rb b/app/models/report.rb index 3cd7aa376..5d1b5e599 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -105,12 +105,13 @@ class Report < ActiveRecord::Base el.position = index el.report = self el.parent = parent - el.type_of = json_element["type_of"] - el.sort_order = json_element["sort_order"] - el.set_element_reference(json_element["id"]) + el.type_of = json_element['type_of'] + el.sort_order = json_element['sort_order'] + el.set_element_reference(json_element['id']) el.save! - if json_element["children"].present? - json_element["children"].each_with_index do |child, i| + + if json_element['children'].present? + json_element['children'].each_with_index do |child, i| save_json_element(child, i, el) end end diff --git a/app/models/report_element.rb b/app/models/report_element.rb index 131d3d528..398a69883 100644 --- a/app/models/report_element.rb +++ b/app/models/report_element.rb @@ -1,22 +1,5 @@ class ReportElement < ActiveRecord::Base - enum type_of: { - project_header: 0, - my_module: 1, - step: 2, - result_asset: 3, - result_table: 4, - result_text: 5, - my_module_activity: 6, - my_module_samples: 7, - step_checklist: 8, - step_asset: 9, - step_table: 10, - step_comments: 11, - result_comments: 12, - project_activity: 13, # TODO - project_samples: 14, # TODO - experiment: 15 - } + enum type_of: Extends::REPORT_ELEMENT_TYPES # This is only used by certain elements enum sort_order: { @@ -32,8 +15,12 @@ class ReportElement < ActiveRecord::Base belongs_to :report, inverse_of: :report_elements # Hierarchical structure representation - has_many :children, -> { order(:position) }, class_name: "ReportElement", foreign_key: "parent_id", dependent: :destroy - belongs_to :parent, class_name: "ReportElement" + has_many :children, + -> { order(:position) }, + class_name: 'ReportElement', + foreign_key: 'parent_id', + dependent: :destroy + belongs_to :parent, class_name: 'ReportElement' # References to various report entities belongs_to :project, inverse_of: :report_elements @@ -59,44 +46,18 @@ class ReportElement < ActiveRecord::Base # Get the referenced element (previously, element's type_of must be set) def element_reference - if project_header? or project_activity? or project_samples? - return project - elsif experiment? - return experiment - elsif my_module? or my_module_activity? or my_module_samples? - return my_module - elsif step? or step_comments? - return step - elsif result_asset? or result_table? or result_text? or result_comments? - return result - elsif step_checklist? - return checklist - elsif step_asset? - return asset - elsif step_table? - return table + ReportExtends::ELEMENT_REFERENCES.each do |el_ref| + return eval(el_ref.element.gsub('_id', '')) if el_ref.check(self) end end - # Set the element reference (previously, element's type_of must be set) def set_element_reference(ref_id) - if project_header? or project_activity? or project_samples? - self.project_id = ref_id - elsif experiment? - self.experiment_id = ref_id - elsif my_module? or my_module_activity? or my_module_samples? - self.my_module_id = ref_id - elsif step? or step_comments? - self.step_id = ref_id - elsif result_asset? or result_table? or result_text? or result_comments? - self.result_id = ref_id - elsif step_checklist? - self.checklist_id = ref_id - elsif step_asset? - self.asset_id = ref_id - elsif step_table? - self.table_id = ref_id + ReportExtends::SET_ELEMENT_REFERENCES_LIST.each do |el_ref| + check = el_ref.check(self) + next unless check + public_send("#{el_ref.element}=", ref_id) + break end end @@ -120,18 +81,17 @@ class ReportElement < ActiveRecord::Base private def has_one_of_referenced_elements - num_of_refs = [ - project, - experiment, - my_module, - step, - result, - checklist, - asset, - table - ].count { |r| r.present? } + num_of_refs = [project, + experiment, + my_module, + step, + result, + checklist, + asset, + table].count { |r| r.present? } if num_of_refs != 1 - errors.add(:base, "Report element must have exactly one element reference.") + errors.add(:base, + 'Report element must have exactly one element reference.') end end end diff --git a/app/views/reports/elements/_result_asset_element.html.erb b/app/views/reports/elements/_my_module_result_asset_element.html.erb similarity index 100% rename from app/views/reports/elements/_result_asset_element.html.erb rename to app/views/reports/elements/_my_module_result_asset_element.html.erb diff --git a/app/views/reports/elements/_result_table_element.html.erb b/app/views/reports/elements/_my_module_result_table_element.html.erb similarity index 100% rename from app/views/reports/elements/_result_table_element.html.erb rename to app/views/reports/elements/_my_module_result_table_element.html.erb diff --git a/app/views/reports/elements/_result_text_element.html.erb b/app/views/reports/elements/_my_module_result_text_element.html.erb similarity index 100% rename from app/views/reports/elements/_result_text_element.html.erb rename to app/views/reports/elements/_my_module_result_text_element.html.erb diff --git a/app/views/reports/elements/_step_element.html.erb b/app/views/reports/elements/_my_module_step_element.html.erb similarity index 100% rename from app/views/reports/elements/_step_element.html.erb rename to app/views/reports/elements/_my_module_step_element.html.erb diff --git a/app/views/reports/elements/_project_header_element.html.erb b/app/views/reports/elements/_project_header_element.html.erb index c39d2a483..beb727530 100644 --- a/app/views/reports/elements/_project_header_element.html.erb +++ b/app/views/reports/elements/_project_header_element.html.erb @@ -18,4 +18,4 @@
<%= children if (defined? children and children.present?) %>
- \ No newline at end of file + diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 7883381d9..544aa2748 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -10,11 +10,29 @@ class Extends # > end # > - # Notification types. Should not be freezed, as modules might append to this. + # Extends enum types. Should not be freezed, as modules might append to this. NOTIFICATIONS_TYPES = { assignment: 0, recent_changes: 1, system_message: 2 } TASKS_STATES = { uncompleted: 0, completed: 1 } + + REPORT_ELEMENT_TYPES = { project_header: 0, + my_module: 1, + step: 2, + result_asset: 3, + result_table: 4, + result_text: 5, + my_module_activity: 6, + my_module_samples: 7, + step_checklist: 8, + step_asset: 9, + step_table: 10, + step_comments: 11, + result_comments: 12, + project_activity: 13, # TODO + project_samples: 14, # TODO + experiment: 15 } + end diff --git a/config/initializers/extends/report_extends.rb b/config/initializers/extends/report_extends.rb new file mode 100644 index 000000000..d243c6bb0 --- /dev/null +++ b/config/initializers/extends/report_extends.rb @@ -0,0 +1,195 @@ +######################################################### +# 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. + # :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 prock which the my_module is passed and have to return a + # collection of element + # :singular => true by defaut change the enum type to singular + # needed when querying partials by name + + ModuleElement = Struct.new(:element, + :children, + :locals, + :coll, + :singular) do + def initialize(element, children, locals, coll = nil, singular = true) + super(element, children, locals, coll, singular) + end + + def collection(my_module) + coll.call(my_module) if coll + end + + def parse_locals(values) + container = {} + locals.each_with_index do |local, index| + container[local] = values[index] + end + container + end + + def file_name + return element.to_s unless singular + element.to_s.singularize + end + end + + # Module contents element + MODULE_CONTENTS = [ + ModuleElement.new(:steps, + true, + [:step], + proc do |my_module| + my_module.protocol.completed_steps + end), + ModuleElement.new(:result_assets, + true, + [:result], + proc do |my_module| + my_module.results.select { |r| r.is_asset && r.active? } + end), + ModuleElement.new(:result_tables, + true, + [:result], + proc do |my_module| + my_module.results.select { |r| r.is_table && r.active? } + end), + ModuleElement.new(:result_texts, + true, + [:result], + proc do |my_module| + my_module.results.select { |r| r.is_text && r.active? } + end), + ModuleElement.new(:activity, + false, + [:my_module, :order]), + ModuleElement.new(:samples, + false, + [:my_module, :order]) + ] + + # path: app/helpers/reports_helpers.rb + # method: render_report_element + + # adds :order local to listed elements views + # ADD REPORT ELEMENT TYPE WHICH YOU WANT TO PASS 'ORDER' LOCAL IN THE PARTIAL + 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 + FIRST_PART_ELEMENTS = %w(result_comments + result_text + result_asset + result_table + project_header + step_comments) + + # 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