From 02dd6636baf34729033efab6f2808e33b90c43b0 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Mon, 5 Jun 2017 12:24:06 +0200 Subject: [PATCH] Allow saving & editing of reports with repository elements Closes SCI-1279. --- app/assets/javascripts/reports/new.js.erb | 18 ++++---- app/controllers/reports_controller.rb | 8 ++-- app/helpers/reports_helper.rb | 10 ++--- app/models/report.rb | 2 +- app/models/report_element.rb | 34 +++++++-------- app/models/repository.rb | 1 + .../elements/_experiment_element.html.erb | 3 +- .../_my_module_activity_element.html.erb | 2 +- .../elements/_my_module_element.html.erb | 2 +- .../_my_module_repository_element.html.erb | 6 +-- .../_my_module_result_asset_element.html.erb | 2 +- .../_my_module_result_table_element.html.erb | 2 +- .../_my_module_result_text_element.html.erb | 2 +- .../_my_module_samples_element.html.erb | 2 +- .../elements/_my_module_step_element.html.erb | 2 +- .../elements/_project_header_element.html.erb | 2 +- .../_result_comments_element.html.erb | 2 +- .../elements/_step_asset_element.html.erb | 2 +- .../elements/_step_checklist_element.html.erb | 2 +- .../elements/_step_comments_element.html.erb | 2 +- .../elements/_step_table_element.html.erb | 2 +- config/initializers/extends.rb | 3 +- config/initializers/extends/report_extends.rb | 43 ++++++++++--------- ...200_add_repository_id_to_report_element.rb | 6 +++ 24 files changed, 82 insertions(+), 78 deletions(-) create mode 100644 db/migrate/20170602161200_add_repository_id_to_report_element.rb diff --git a/app/assets/javascripts/reports/new.js.erb b/app/assets/javascripts/reports/new.js.erb index bf0c05158..80a4b162d 100644 --- a/app/assets/javascripts/reports/new.js.erb +++ b/app/assets/javascripts/reports/new.js.erb @@ -217,9 +217,7 @@ function initializeNewElement(newEl) { url: url, type: "GET", dataType: "json", - data: { - id: parentElementId - }, + data: parentElementId, success: function(data, status, jqxhr) { // Open modal, set its title, and display module contents addContentsModal.find(".modal-title").text(modalTitle); @@ -500,11 +498,11 @@ function initializeUnsavedWorkDialog() { */ function getSidebarEl(reportEl) { var type = reportEl.data("type"); - var id = reportEl.data("id"); + var scrollId = reportEl.data("scroll-id"); return $(SIDEBAR_PARENT_TREE).find( "li" + "[data-type='" + type + "']" + - "[data-id='" + id + "']" + "[data-scroll-id='" + scrollId + "']" ); } @@ -516,11 +514,11 @@ function getSidebarEl(reportEl) { */ function getReportEl(sidebarEl) { var type = sidebarEl.data("type"); - var id = sidebarEl.data("id"); + var scrollId = sidebarEl.data("scroll-id"); return $(REPORT_CONTENT).find( "div.report-element" + "[data-type='" + type + "']" + - "[data-id='" + id + "']" + "[data-scroll-id='" + scrollId + "']" ); } @@ -565,14 +563,14 @@ function initSidebarElement(reportEl) { var elChildrenContainer = reportEl.children(".report-element-children"); var type = reportEl.data("type"); var name = reportEl.data("name"); - var id = reportEl.data("id"); + var scrollId = reportEl.data("scroll-id"); var iconClass = "glyphicon " + reportEl.data("icon-class"); // Generate list element var newLi = $(document.createElement("li")); newLi .attr("data-type", type) - .attr("data-id", id); + .attr("data-scroll-id", scrollId); var newSpan = $(document.createElement("span")); newSpan.appendTo(newLi); @@ -1025,7 +1023,7 @@ function addElement(jsonEl, prevEl) { .find( ".report-element" + "[data-type='" + el.attr("data-type") + "']" + - "[data-id='" + el.attr("data-id") + "']" + "[data-scroll-id='" + el.attr("data-scroll-id") + "']" ); if (existing.length && existing.length > 0) { // TODO Remove event listeners on existing element diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index 5aca8e35d..524123e90 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -233,7 +233,7 @@ class ReportsController < ApplicationController # Experiment for adding contents into experiment element def experiment_contents_modal - experiment = Experiment.find_by_id(params[:id]) + experiment = Experiment.find_by_id(params[:experiment_id]) respond_to do |format| if experiment.blank? @@ -255,7 +255,7 @@ class ReportsController < ApplicationController # Modal for adding contents into module element def module_contents_modal - my_module = MyModule.find_by_id(params[:id]) + my_module = MyModule.find_by_id(params[:my_module_id]) respond_to do |format| if my_module.blank? @@ -277,7 +277,7 @@ class ReportsController < ApplicationController # Modal for adding contents into step element def step_contents_modal - step = Step.find_by_id(params[:id]) + step = Step.find_by_id(params[:step_id]) respond_to do |format| if step.blank? @@ -299,7 +299,7 @@ class ReportsController < ApplicationController # Modal for adding contents into result element def result_contents_modal - result = Result.find_by_id(params[:id]) + result = Result.find_by_id(params[:result_id]) respond_to do |format| if result.blank? diff --git a/app/helpers/reports_helper.rb b/app/helpers/reports_helper.rb index 374aae60b..5fa20a09e 100644 --- a/app/helpers/reports_helper.rb +++ b/app/helpers/reports_helper.rb @@ -45,13 +45,9 @@ module ReportsHelper 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 + element.element_references.each do |el_ref| + locals[el_ref.class.name.underscore.to_sym] = el_ref + end locals[:order] = element .sort_order if type.in? ReportExtends::SORTED_ELEMENTS end diff --git a/app/models/report.rb b/app/models/report.rb index 9c862fbd8..651ccc486 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -95,7 +95,7 @@ class Report < ActiveRecord::Base 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.set_element_references(json_element['id']) el.save! if json_element['children'].present? diff --git a/app/models/report_element.rb b/app/models/report_element.rb index 398a69883..623ba3fe6 100644 --- a/app/models/report_element.rb +++ b/app/models/report_element.rb @@ -31,6 +31,7 @@ class ReportElement < ActiveRecord::Base belongs_to :checklist, inverse_of: :report_elements belongs_to :asset, inverse_of: :report_elements belongs_to :table, inverse_of: :report_elements + belongs_to :repository, inverse_of: :report_elements def has_children? children.length > 0 @@ -44,19 +45,21 @@ class ReportElement < ActiveRecord::Base step_comments? or result_comments? end - # Get the referenced element (previously, element's type_of must be set) - def element_reference + # Get the referenced elements (previously, element's type_of must be set) + def element_references ReportExtends::ELEMENT_REFERENCES.each do |el_ref| - return eval(el_ref.element.gsub('_id', '')) if el_ref.check(self) + return el_ref.elements.map { |el| eval(el.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) + # Set the element references (previously, element's type_of must be set) + def set_element_references(ref_ids) ReportExtends::SET_ELEMENT_REFERENCES_LIST.each do |el_ref| check = el_ref.check(self) next unless check - public_send("#{el_ref.element}=", ref_id) + el_ref.elements.each do |element| + public_send("#{element}=", ref_ids[element]) + end break end end @@ -64,7 +67,7 @@ class ReportElement < ActiveRecord::Base # removes element that are archived or deleted def clean_removed_or_archived_elements parent_model = '' - %w(project experiment my_module step result checklist asset table) + %w(project experiment my_module step result checklist asset table repository) .each do |el| parent_model = el if send el end @@ -81,17 +84,12 @@ 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? } - if num_of_refs != 1 - errors.add(:base, - 'Report element must have exactly one element reference.') + element_references.each do |el| + if el.nil? + errors.add(:base, + 'Report element doesn\'t have correct element references.') + break + end end end end diff --git a/app/models/repository.rb b/app/models/repository.rb index 43977d3cf..841ff4d9b 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -5,6 +5,7 @@ class Repository < ActiveRecord::Base has_many :repository_rows has_many :repository_table_states, inverse_of: :repository, dependent: :destroy + has_many :report_elements, inverse_of: :repository, dependent: :destroy auto_strip_attributes :name, nullify: false validates :name, diff --git a/app/views/reports/elements/_experiment_element.html.erb b/app/views/reports/elements/_experiment_element.html.erb index a2aff7b80..d8cbe17a6 100644 --- a/app/views/reports/elements/_experiment_element.html.erb +++ b/app/views/reports/elements/_experiment_element.html.erb @@ -3,7 +3,8 @@
" data-name="<%= name %>" data-icon-class="fa fa-flask">
diff --git a/app/views/reports/elements/_my_module_activity_element.html.erb b/app/views/reports/elements/_my_module_activity_element.html.erb index a0a47a563..fab07dacd 100644 --- a/app/views/reports/elements/_my_module_activity_element.html.erb +++ b/app/views/reports/elements/_my_module_activity_element.html.erb @@ -2,7 +2,7 @@ <% if order.blank? and @order.present? then order = @order end %> <% timestamp = Time.current + 1.year - 2.days %> <% activities = my_module.activities.order(created_at: order) %> -
" data-name="<%=t "projects.reports.elements.module_activity.sidebar_name" %>" data-icon-class="glyphicon glyphicon-equalizer"> +
" data-name="<%=t "projects.reports.elements.module_activity.sidebar_name" %>" data-icon-class="glyphicon glyphicon-equalizer">
diff --git a/app/views/reports/elements/_my_module_element.html.erb b/app/views/reports/elements/_my_module_element.html.erb index 79bac1047..9537da131 100644 --- a/app/views/reports/elements/_my_module_element.html.erb +++ b/app/views/reports/elements/_my_module_element.html.erb @@ -1,7 +1,7 @@ <% if my_module.blank? and @my_module.present? then my_module = @my_module end %> <% timestamp = my_module.created_at %> <% name = my_module.name %> -
" data-name="<%= name %>" data-icon-class="glyphicon-credit-card"> +
" data-name="<%= name %>" data-icon-class="glyphicon-credit-card">
diff --git a/app/views/reports/elements/_my_module_repository_element.html.erb b/app/views/reports/elements/_my_module_repository_element.html.erb index 2a97e05d0..e077b5049 100644 --- a/app/views/reports/elements/_my_module_repository_element.html.erb +++ b/app/views/reports/elements/_my_module_repository_element.html.erb @@ -1,10 +1,10 @@ -<% repository = Repository.find(element_id) %> +<% repository ||= Repository.find(element_id) %> <% if my_module.blank? and @my_module.present? then my_module = @my_module end %> <% rows = my_module.repository_rows.where(repository: repository) %> <% if order.blank? and @order.present? then order = @order end %> <% timestamp = Time.current + 1.year - 1.days %> -<% rows_json = my_module.repository_json_hot(element_id, order) %> -
" data-order="<%= order == :asc ? "asc" : "desc" %>" data-name="<%= repository.name %>" data-icon-class="glyphicon-oil"> +<% rows_json = my_module.repository_json_hot(repository.id, order) %> +
" data-order="<%= order == :asc ? "asc" : "desc" %>" data-name="<%= repository.name %>" data-icon-class="glyphicon-oil">
diff --git a/app/views/reports/elements/_my_module_result_asset_element.html.erb b/app/views/reports/elements/_my_module_result_asset_element.html.erb index c863c0597..f91ac3858 100644 --- a/app/views/reports/elements/_my_module_result_asset_element.html.erb +++ b/app/views/reports/elements/_my_module_result_asset_element.html.erb @@ -5,7 +5,7 @@ <% timestamp = asset.created_at %> <% name = result.name %> <% icon_class = is_image ? "glyphicon-picture" : "glyphicon-file" %> -
" data-name="<%= name %>" data-icon-class="<%= icon_class %>"> +
" data-name="<%= name %>" data-icon-class="<%= icon_class %>">
diff --git a/app/views/reports/elements/_my_module_result_table_element.html.erb b/app/views/reports/elements/_my_module_result_table_element.html.erb index 7129f5eed..e5489cab3 100644 --- a/app/views/reports/elements/_my_module_result_table_element.html.erb +++ b/app/views/reports/elements/_my_module_result_table_element.html.erb @@ -3,7 +3,7 @@ <% comments = result.result_comments %> <% timestamp = table.created_at %> <% name = result.name %> -
" data-name="<%= name %>" data-icon-class="glyphicon-th"> +
" data-name="<%= name %>" data-icon-class="glyphicon-th">
diff --git a/app/views/reports/elements/_my_module_result_text_element.html.erb b/app/views/reports/elements/_my_module_result_text_element.html.erb index c353f7395..1dee844d1 100644 --- a/app/views/reports/elements/_my_module_result_text_element.html.erb +++ b/app/views/reports/elements/_my_module_result_text_element.html.erb @@ -3,7 +3,7 @@ <% comments = result.result_comments %> <% timestamp = result.created_at %> <% name = result.name %> -
" data-name="<%= name %>" data-icon-class="glyphicon-asterisk"> +
" data-name="<%= name %>" data-icon-class="glyphicon-asterisk">
diff --git a/app/views/reports/elements/_my_module_samples_element.html.erb b/app/views/reports/elements/_my_module_samples_element.html.erb index b4f5f4105..ab1bc9fdc 100644 --- a/app/views/reports/elements/_my_module_samples_element.html.erb +++ b/app/views/reports/elements/_my_module_samples_element.html.erb @@ -2,7 +2,7 @@ <% if order.blank? and @order.present? then order = @order end %> <% timestamp = Time.current + 1.year - 1.days %> <% samples_json = my_module.samples_json_hot(order) %> -
" data-name="<%=t "projects.reports.elements.module_samples.sidebar_name" %>" data-icon-class="glyphicon-tint"> +
" data-name="<%=t "projects.reports.elements.module_samples.sidebar_name" %>" data-icon-class="glyphicon-tint">
diff --git a/app/views/reports/elements/_my_module_step_element.html.erb b/app/views/reports/elements/_my_module_step_element.html.erb index 4c34e3da3..0ee71cb0f 100644 --- a/app/views/reports/elements/_my_module_step_element.html.erb +++ b/app/views/reports/elements/_my_module_step_element.html.erb @@ -6,7 +6,7 @@ <% assets = step.assets %> <% checklists = step.checklists %> <% comments = step.step_comments %> -
" data-name="<%=t "projects.reports.elements.step.sidebar_name", pos: (step.position + 1), name: step.name %>" data-icon-class="glyphicon-circle-arrow-right"> +
" data-name="<%=t "projects.reports.elements.step.sidebar_name", pos: (step.position + 1), name: step.name %>" data-icon-class="glyphicon-circle-arrow-right">
diff --git a/app/views/reports/elements/_project_header_element.html.erb b/app/views/reports/elements/_project_header_element.html.erb index beb727530..aa422d5f3 100644 --- a/app/views/reports/elements/_project_header_element.html.erb +++ b/app/views/reports/elements/_project_header_element.html.erb @@ -1,6 +1,6 @@ <% if project.blank? and @project.present? then project = @project end %> <% name = t("projects.reports.elements.project_header.title", project: project.name) %> -
+
diff --git a/app/views/reports/elements/_result_comments_element.html.erb b/app/views/reports/elements/_result_comments_element.html.erb index 08a6a96ed..2e9077128 100644 --- a/app/views/reports/elements/_result_comments_element.html.erb +++ b/app/views/reports/elements/_result_comments_element.html.erb @@ -2,7 +2,7 @@ <% if order.blank? and @order.present? then order = @order end %> <% comments = result.result_comments.order(created_at: order) %> <% timestamp = Time.current + 1.year %> -
" data-type="result_comments" data-id="<%= result.id %>" data-name="<%=t "projects.reports.elements.result_comments.sidebar_name" %>" data-icon-class="glyphicon-comment"> +
" data-type="result_comments" data-id='{ "result_id": <%= result.id %> }' data-scroll-id="<%= result.id %>" data-name="<%=t "projects.reports.elements.result_comments.sidebar_name" %>" data-icon-class="glyphicon-comment">
diff --git a/app/views/reports/elements/_step_asset_element.html.erb b/app/views/reports/elements/_step_asset_element.html.erb index 35896c710..c003d1cfc 100644 --- a/app/views/reports/elements/_step_asset_element.html.erb +++ b/app/views/reports/elements/_step_asset_element.html.erb @@ -2,7 +2,7 @@ <% is_image = asset.is_image? %> <% timestamp = asset.created_at %> <% icon_class = is_image ? 'glyphicon-picture' : 'glyphicon-file' %> -
" data-icon-class="<%= icon_class %>"> +
" data-icon-class="<%= icon_class %>">
diff --git a/app/views/reports/elements/_step_checklist_element.html.erb b/app/views/reports/elements/_step_checklist_element.html.erb index f0fb660e6..4dfe8e2e6 100644 --- a/app/views/reports/elements/_step_checklist_element.html.erb +++ b/app/views/reports/elements/_step_checklist_element.html.erb @@ -1,7 +1,7 @@ <% if checklist.blank? and @checklist.present? then checklist = @checklist end %> <% items = checklist.checklist_items %> <% timestamp = checklist.created_at %> -
+
diff --git a/app/views/reports/elements/_step_comments_element.html.erb b/app/views/reports/elements/_step_comments_element.html.erb index dbb8d1a65..fddee65cd 100644 --- a/app/views/reports/elements/_step_comments_element.html.erb +++ b/app/views/reports/elements/_step_comments_element.html.erb @@ -2,7 +2,7 @@ <% if order.blank? and @order.present? then order = @order end %> <% comments = step.step_comments.order(created_at: order) %> <% timestamp = Time.current + 1.year %> -
" data-icon-class="glyphicon-comment"> +
" data-icon-class="glyphicon-comment">
diff --git a/app/views/reports/elements/_step_table_element.html.erb b/app/views/reports/elements/_step_table_element.html.erb index 081af31bf..8507cc62a 100644 --- a/app/views/reports/elements/_step_table_element.html.erb +++ b/app/views/reports/elements/_step_table_element.html.erb @@ -1,6 +1,6 @@ <% if table.blank? and @table.present? then table = @table end %> <% timestamp = table.created_at %> -
+
diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index ec0ebd0bc..eeb492a51 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -35,7 +35,8 @@ class Extends result_comments: 12, project_activity: 13, # TODO project_samples: 14, # TODO - experiment: 15 } + experiment: 15, + my_module_repository: 16 } # Data type name should match corresponding model's name REPOSITORY_DATA_TYPES = { RepositoryTextValue: 0, diff --git a/config/initializers/extends/report_extends.rb b/config/initializers/extends/report_extends.rb index 182479131..8ec741537 100644 --- a/config/initializers/extends/report_extends.rb +++ b/config/initializers/extends/report_extends.rb @@ -108,10 +108,11 @@ module ReportExtends # ADD REPORT ELEMENT TYPE WHICH YOU WANT TO PASS 'ORDER' LOCAL IN THE PARTIAL SORTED_ELEMENTS = %w(my_module_activity my_module_samples + my_module_repository 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) + MY_MODULE_ELEMENTS = %w(my_module my_module_activity my_module_samples my_module_repository) # sets local name to first element of the listed elements FIRST_PART_ELEMENTS = %w(result_comments @@ -124,9 +125,9 @@ module ReportExtends # 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) + ElementReference = Struct.new(:checker, :elements) do + def initialize(checker, elements = :element_reference_needed!) + super(checker, elements) end def check(report_element) @@ -141,22 +142,23 @@ module ReportExtends report_element.project_activity? || report_element.project_samples? end, - 'project_id' + ['project_id'] ), - ElementReference.new(proc(&:experiment?), 'experiment_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' + ['my_module_id'] ), + ElementReference.new(proc(&:my_module_repository?), ['my_module_id', 'repository_id']), ElementReference.new( proc do |report_element| report_element.step? || report_element.step_comments? end, - 'step_id' + ['step_id'] ), ElementReference.new( proc do |report_element| @@ -165,11 +167,11 @@ module ReportExtends report_element.result_text? || report_element.result_comments? end, - 'result_id' + ['result_id'] ), - ElementReference.new(proc(&:step_checklist?), 'checklist_id'), - ElementReference.new(proc(&:step_asset?), 'asset_id'), - ElementReference.new(proc(&:step_table?), 'table_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 @@ -182,23 +184,24 @@ module ReportExtends report_element.project_activity? || report_element.project_samples? end, - 'project_id' + ['project_id'] ), - ElementReference.new(proc(&:experiment?), 'experiment_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' + ['my_module_id'] ), + ElementReference.new(proc(&:my_module_repository?), ['my_module_id', 'repository_id']), ElementReference.new( proc do |report_element| report_element.step? || report_element.step_comments? end, - 'step_id' + ['step_id'] ), ElementReference.new( proc do |report_element| @@ -207,10 +210,10 @@ module ReportExtends report_element.result_text? || report_element.result_comments? end, - 'result_id' + ['result_id'] ), - ElementReference.new(proc(&:step_checklist?), 'checklist_id'), - ElementReference.new(proc(&:step_asset?), 'asset_id'), - ElementReference.new(proc(&:step_table?), 'table_id') + ElementReference.new(proc(&:step_checklist?), ['checklist_id']), + ElementReference.new(proc(&:step_asset?), ['asset_id']), + ElementReference.new(proc(&:step_table?), ['table_id']) ] end diff --git a/db/migrate/20170602161200_add_repository_id_to_report_element.rb b/db/migrate/20170602161200_add_repository_id_to_report_element.rb new file mode 100644 index 000000000..d8cecf038 --- /dev/null +++ b/db/migrate/20170602161200_add_repository_id_to_report_element.rb @@ -0,0 +1,6 @@ +class AddRepositoryIdToReportElement < ActiveRecord::Migration + def change + add_column :report_elements, :repository_id, :integer + add_index :report_elements, :repository_id + end +end