From 5d6c9e78fdfaaf138ba40d3a51ff7e0f025d993d Mon Sep 17 00:00:00 2001 From: aignatov-bio Date: Fri, 2 Oct 2020 14:49:08 +0200 Subject: [PATCH 1/8] Refactor dashboard layout --- .../javascripts/dashboard/quick_start.js | 8 +- .../stylesheets/dashboard/calendar.scss | 15 +--- .../stylesheets/dashboard/current_tasks.scss | 8 -- .../stylesheets/dashboard/quick_start.scss | 48 ----------- .../stylesheets/dashboard/recent_work.scss | 23 +----- app/assets/stylesheets/dashboard/show.scss | 81 +++++++++++++++++-- app/views/dashboards/_calendar.html.erb | 2 +- app/views/dashboards/_current_tasks.html.erb | 2 +- app/views/dashboards/_quick_start.html.erb | 35 +++----- app/views/dashboards/_recent_work.html.erb | 4 +- app/views/dashboards/show.html.erb | 17 ++-- config/initializers/extends.rb | 6 ++ 12 files changed, 111 insertions(+), 138 deletions(-) delete mode 100644 app/assets/stylesheets/dashboard/quick_start.scss diff --git a/app/assets/javascripts/dashboard/quick_start.js b/app/assets/javascripts/dashboard/quick_start.js index 25d1b3c82..20af31ab8 100644 --- a/app/assets/javascripts/dashboard/quick_start.js +++ b/app/assets/javascripts/dashboard/quick_start.js @@ -8,19 +8,19 @@ var DasboardQuickStartWidget = (function() { var newProjectsVisibility = '#create-task-modal .new-projects-visibility'; function initNewReportLink() { - $('.quick-start-widget .new-report').click(() => { + $('.quick-start-buttons .new-report').click(() => { sessionStorage.setItem('scinote-dashboard-new-report', Math.floor(Date.now() / 1000)); }); } function initNewProtocolLink() { - $('.quick-start-widget .new-protocol').click(() => { + $('.quick-start-buttons .new-protocol').click(() => { sessionStorage.setItem('scinote-dashboard-new-protocol', Math.floor(Date.now() / 1000)); }); } function initNewTaskModal() { - $('.quick-start-widget .new-task').click(() => { + $('.quick-start-buttons .new-task').click(() => { $('#create-task-modal').modal('show'); $('#create-task-modal .select-block').attr('data-error', ''); }); @@ -118,7 +118,7 @@ var DasboardQuickStartWidget = (function() { return { init: () => { - if ($('.quick-start-widget').length) { + if ($('.quick-start-buttons').length) { initNewTaskModal(); initNewProtocolLink(); initNewReportLink(); diff --git a/app/assets/stylesheets/dashboard/calendar.scss b/app/assets/stylesheets/dashboard/calendar.scss index 1b91db708..d54d3ed4b 100644 --- a/app/assets/stylesheets/dashboard/calendar.scss +++ b/app/assets/stylesheets/dashboard/calendar.scss @@ -5,8 +5,6 @@ .dashboard-container .calendar-widget { --calendar-day-size: 32px; - grid-column: 10 / span 3; - grid-row: 1 / span 6; min-height: 320px; .dashboard-calendar { @@ -127,17 +125,8 @@ } } -@media (max-width: 1250px) { +@media (max-width: 1600px) { .dashboard-container .calendar-widget { - grid-column: 9 / span 4; - } -} - -@media (max-width: 1000px) { - .dashboard-container .calendar-widget { - grid-column: 1 / span 6; - grid-row: 5 / span 4; - .clndr { .events-container { left: 0; @@ -150,7 +139,5 @@ @media (max-width: 700px) { .dashboard-container .calendar-widget { --calendar-day-size: 28px; - grid-column: 1 / span 12; - grid-row: 2; } } diff --git a/app/assets/stylesheets/dashboard/current_tasks.scss b/app/assets/stylesheets/dashboard/current_tasks.scss index 3efb8afe3..2e05a9058 100644 --- a/app/assets/stylesheets/dashboard/current_tasks.scss +++ b/app/assets/stylesheets/dashboard/current_tasks.scss @@ -2,8 +2,6 @@ // scss-lint:disable NestingDepth .dashboard-container .current-tasks-widget { - grid-column: 1 / span 9; - grid-row: 1 / span 6; .title { flex-shrink: 0; @@ -238,8 +236,6 @@ @media (max-width: 1250px) { .dashboard-container .current-tasks-widget { - grid-column: 1 / span 8; - .task-progress-container { max-width: 150px; } @@ -259,9 +255,6 @@ @media (max-width: 1000px) { .dashboard-container .current-tasks-widget { - grid-column: 1 / span 12; - grid-row: 1 / span 4; - .no-tasks .fas { margin-left: 500px; } @@ -271,7 +264,6 @@ @media (max-width: 700px) { .dashboard-container .current-tasks-widget { --widget-header-size: 72px; - grid-row: 1; min-height: 450px; .widget-title { diff --git a/app/assets/stylesheets/dashboard/quick_start.scss b/app/assets/stylesheets/dashboard/quick_start.scss deleted file mode 100644 index 1ab28051a..000000000 --- a/app/assets/stylesheets/dashboard/quick_start.scss +++ /dev/null @@ -1,48 +0,0 @@ -// scss-lint:disable SelectorDepth -// scss-lint:disable NestingDepth - -.dashboard-container .quick-start-widget { - grid-column: 1 / span 2; - grid-row: 7 / span 6; - - .widget-body { - .quick-start-description { - margin: 16px 16px 24px; - } - - .btn-secondary { - margin: 8px 16px; - text-align: left; - width: calc(100% - 32px); - } - } -} - -@media (max-width: 1700px) { - .dashboard-container .quick-start-widget { - grid-column: 1 / span 3; - } -} - - -@media (max-width: 1300px) { - .dashboard-container .quick-start-widget { - grid-column: 1 / span 4; - } -} - -@media (max-width: 1000px) { - .dashboard-container .quick-start-widget { - grid-column: 7 / span 6; - grid-row: 5 / span 4; - } -} - -@media (max-width: 700px) { - .dashboard-container .quick-start-widget { - --widget-header-size: 36px; - grid-column: 1 / span 12; - grid-row: 4; - min-height: 300px; - } -} diff --git a/app/assets/stylesheets/dashboard/recent_work.scss b/app/assets/stylesheets/dashboard/recent_work.scss index 00fad2df4..696ffeb98 100644 --- a/app/assets/stylesheets/dashboard/recent_work.scss +++ b/app/assets/stylesheets/dashboard/recent_work.scss @@ -2,9 +2,6 @@ // scss-lint:disable NestingDepth .dashboard-container .recent-work-widget { - grid-column: 3 / span 7; - grid-row: 7 / span 6; - .widget-title { flex-grow: 1; } @@ -58,24 +55,9 @@ } } -@media (max-width: 1700px) { - .dashboard-container .recent-work-widget { - grid-column: 4 / span 6; - } -} - - -@media (max-width: 1300px) { - .dashboard-container .recent-work-widget { - grid-column: 5 / span 8; - } -} @media (max-width: 1000px) { .dashboard-container .recent-work-widget { - grid-column: 1 / span 12; - grid-row: 9 / span 4; - .no-results { .no-results-arrow { display: none; @@ -84,14 +66,11 @@ } } -@media (max-width: 700px) { +@media (max-width: 1100px) { .dashboard-container .recent-work-widget { - --widget-header-size: 72px; - grid-row: 3; min-height: 450px; .widget-title { - flex-basis: 100%; line-height: 36px; } diff --git a/app/assets/stylesheets/dashboard/show.scss b/app/assets/stylesheets/dashboard/show.scss index 9a9598352..0ef2994e4 100644 --- a/app/assets/stylesheets/dashboard/show.scss +++ b/app/assets/stylesheets/dashboard/show.scss @@ -1,23 +1,53 @@ // scss-lint:disable SelectorDepth // scss-lint:disable NestingDepth -.dashboard-container { +.dashboard-background { + background: $color-concrete; + height: 100%; + left: 0; + position: fixed; + top: 0; + width: 100%; +} + +.dashboard-view { --dashboard-widgets-gap: 30px; + padding: calc(var(--dashboard-widgets-gap) / 2) + calc(var(--dashboard-widgets-gap) / 2) + var(--dashboard-widgets-gap); +} + +.dashboard-header { + padding-bottom: calc(var(--dashboard-widgets-gap) / 2); +} + +.dashboard-container { --widget-header-size: 44px; display: grid; + grid-auto-rows: 30em; grid-column-gap: var(--dashboard-widgets-gap); grid-row-gap: var(--dashboard-widgets-gap); - grid-template-columns: repeat(12, 1fr); - grid-template-rows: repeat(12, 1fr); - min-height: calc(100vh - 51px); - padding: var(--dashboard-widgets-gap) calc(var(--dashboard-widgets-gap) - 15px); + grid-template-columns: repeat(auto-fit, minmax(7em, 1fr)); width: 100%; .basic-widget { + background: $color-white; border-radius: $border-radius-modal; box-shadow: $flyout-shadow; position: relative; + &.large-widget { + grid-column: auto / 9 span; + } + + &.medium-widget { + grid-column: auto / 6 span; + } + + &.small-widget { + grid-column: auto / 3 span; + } + .widget-header { align-items: center; border-bottom: $border-tertiary; @@ -27,6 +57,7 @@ .widget-title { @include font-h2; + flex-shrink: 0; } } @@ -51,16 +82,50 @@ } } -@media (max-width: 1300px) { +@media (max-width: 1100px) { .dashboard-container { - --dashboard-widgets-gap: 16px; + --dashboard-widgets-gap: 15px; + grid-template-columns: 100%; + + .basic-widget { + &.large-widget, + &.medium-widget, + &.small-widget { + grid-column: auto / 1 span; + } + } + } +} + +@media (max-height: 800px) { + .dashboard-container { + grid-auto-rows: 24em; + } +} + +@media (min-height: 1300px) { + .dashboard-container { + grid-auto-rows: 36em; } } @media (max-width: 700px) { + + .dashboard-view { + --dashboard-widgets-gap: 15px; + } + .dashboard-container { --widget-header-size: 72px; - grid-template-rows: auto; + grid-auto-rows: auto; + + .basic-widget { + &.large-widget, + &.medium-widget, + &.small-widget { + grid-column: 1 span; + } + } .widget-header { flex-wrap: wrap; diff --git a/app/views/dashboards/_calendar.html.erb b/app/views/dashboards/_calendar.html.erb index fa6195543..0400f6869 100644 --- a/app/views/dashboards/_calendar.html.erb +++ b/app/views/dashboards/_calendar.html.erb @@ -1,4 +1,4 @@ -
+
+
<%= t("dashboard.current_tasks.title") %>
diff --git a/app/views/dashboards/_quick_start.html.erb b/app/views/dashboards/_quick_start.html.erb index 6e321adb1..d40b44e79 100644 --- a/app/views/dashboards/_quick_start.html.erb +++ b/app/views/dashboards/_quick_start.html.erb @@ -1,27 +1,12 @@ -
-
-
- <%= t("dashboard.quick_start.title") %> -
-
-
- <% if current_user.is_guest_of_team?(current_team) %> -
-

<%= t("dashboard.quick_start.placeholder.title") %>

-

<%= t("dashboard.quick_start.placeholder.description") %>

-
- <% else %> -
- <%= t("dashboard.quick_start.description") %> -
-
<%= t("dashboard.quick_start.new_task") %>
- <%= link_to protocols_path, {class: "new-protocol btn btn-secondary btn-block"} do %> - <%= t("dashboard.quick_start.new_protocol") %> - <% end %> - <%= link_to reports_path, {class: "new-report btn btn-secondary btn-block"} do %> - <%= t("dashboard.quick_start.new_report") %> - <% end %> +<% unless current_user.is_guest_of_team?(current_team) %> +
+
<%= t("dashboard.quick_start.new_task") %>
+ <%= link_to protocols_path, {class: "new-protocol btn btn-secondary"} do %> + <%= t("dashboard.quick_start.new_protocol") %> + <% end %> + <%= link_to reports_path, {class: "new-report btn btn-secondary"} do %> + <%= t("dashboard.quick_start.new_report") %> <% end %>
-
-<%= render "create_task_modal" %> \ No newline at end of file + <%= render "create_task_modal" %> +<% end %> diff --git a/app/views/dashboards/_recent_work.html.erb b/app/views/dashboards/_recent_work.html.erb index e6baa06bb..2788e1095 100644 --- a/app/views/dashboards/_recent_work.html.erb +++ b/app/views/dashboards/_recent_work.html.erb @@ -1,4 +1,4 @@ -
+
<%= t('dashboard.recent_work.title') %> @@ -37,4 +37,4 @@
<% end %>
- \ No newline at end of file + diff --git a/app/views/dashboards/show.html.erb b/app/views/dashboards/show.html.erb index 2c4a62741..b46a22b58 100644 --- a/app/views/dashboards/show.html.erb +++ b/app/views/dashboards/show.html.erb @@ -1,10 +1,17 @@ <% provide :head_title, t('nav.label.dashboard') %> <% if current_team %> -
- <%= render "calendar" %> - <%= render "current_tasks" %> - <%= render "recent_work" %> - <%= render "quick_start" %> +
+
+
+ <%= render partial: 'quick_start' %> +
+
+ <% Extends::DEFAULT_DASHBOARD_CONFIGURATION.each do |widget| %> + <% if widget[:visible] %> + <%= render partial: widget[:partial], locals: {widget: widget} %> + <% end %> + <% end %> +
<% end %> diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 4a8f59fb9..7a5ff89fb 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -123,6 +123,12 @@ class Extends 'Protocol' => :description, 'MyModule' => :description } + DEFAULT_DASHBOARD_CONFIGURATION = [ + { partial: 'dashboards/current_tasks', visible: true, size: 'large-widget', position: 1 }, + { partial: 'dashboards/calendar', visible: true, size: 'small-widget', position: 2 }, + { partial: 'dashboards/recent_work', visible: true, size: 'medium-widget', position: 3 } + ] + ACTIVITY_SUBJECT_TYPES = %w( Team RepositoryBase Project Experiment MyModule Result Protocol Report RepositoryRow ).freeze From a7da7b0de379254f763a8ec8939cbb02fd5eb0ff Mon Sep 17 00:00:00 2001 From: aignatov-bio Date: Mon, 12 Oct 2020 15:55:06 +0200 Subject: [PATCH 2/8] Refactor export all my module generator --- app/models/report.rb | 78 +++++++++++++++++++--------------- config/initializers/extends.rb | 8 ++++ 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/app/models/report.rb b/app/models/report.rb index 87d49b5e4..982c2d9dc 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -100,41 +100,9 @@ class Report < ApplicationRecord exp.my_modules.each do |my_module| module_children = [] - module_children += gen_element_content(my_module, nil, 'my_module_protocol', true) - my_module.protocol.steps.each do |step| - step_children = - gen_element_content(step, step.assets, 'step_asset') - step_children += - gen_element_content(step, step.tables, 'step_table') - step_children += - gen_element_content(step, step.checklists, 'step_checklist') - step_children += - gen_element_content(step, nil, 'step_comments', true, 'asc') - - module_children += - gen_element_content(step, nil, 'step', true, nil, step_children) + Extends::EXPORT_ALL_MY_MODULE_ELEMENTS.each do |my_module_element| + module_children += public_send("generate_my_module_#{my_module_element}_element", my_module, project) end - - my_module.results.each do |result| - result_children = - gen_element_content(result, nil, 'result_comments', true, 'asc') - - result_type = if result.asset - 'result_asset' - elsif result.table - 'result_table' - elsif result.result_text - 'result_text' - end - module_children += - gen_element_content(result, nil, result_type, true, nil, - result_children) - end - - repositories = project.assigned_repositories_and_snapshots - - module_children += gen_element_content(my_module, nil, 'my_module_activity', true, 'asc') - module_children += gen_element_content(my_module, repositories, 'my_module_repository', true, 'asc') modules += gen_element_content(my_module, nil, 'my_module', true, nil, module_children) end @@ -183,6 +151,48 @@ class Report < ApplicationRecord elements end + def self.generate_my_module_protocol_element(my_module, _project) + gen_element_content(my_module, nil, 'my_module_protocol', true) + end + + def self.generate_my_module_steps_element(my_module, _project) + my_module_steps = [] + my_module.protocol.steps.each do |step| + step_children = gen_element_content(step, step.assets, 'step_asset') + step_children += gen_element_content(step, step.tables, 'step_table') + step_children += gen_element_content(step, step.checklists, 'step_checklist') + step_children += gen_element_content(step, nil, 'step_comments', true, 'asc') + + my_module_steps += gen_element_content(step, nil, 'step', true, nil, step_children) + end + my_module_steps + end + + def self.generate_my_module_results_element(my_module, _project) + my_module_results = [] + my_module.results.each do |result| + result_children = gen_element_content(result, nil, 'result_comments', true, 'asc') + result_type = if result.asset + 'result_asset' + elsif result.table + 'result_table' + elsif result.result_text + 'result_text' + end + my_module_results += gen_element_content(result, nil, result_type, true, nil, result_children) + end + my_module_results + end + + def self.generate_my_module_activities_element(my_module, _project) + gen_element_content(my_module, nil, 'my_module_activity', true, 'asc') + end + + def self.generate_my_module_repositories_element(my_module, project) + repositories = project.assigned_repositories_and_snapshots + gen_element_content(my_module, repositories, 'my_module_repository', true, 'asc') + end + private # Recursively save a single JSON element diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 7a5ff89fb..4a6a64039 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -41,6 +41,14 @@ class Extends my_module_repository: 17, my_module_protocol: 18 } + EXPORT_ALL_MY_MODULE_ELEMENTS = [ + :protocol, + :steps, + :results, + :activities, + :repositories + ] + # Data type name should match corresponding model's name REPOSITORY_DATA_TYPES = { RepositoryTextValue: 0, RepositoryDateValue: 1, From 36bab10de7381669a18b61724a0882da7221ef42 Mon Sep 17 00:00:00 2001 From: aignatov-bio Date: Wed, 14 Oct 2020 12:08:04 +0200 Subject: [PATCH 3/8] Update export all to tree structure --- app/models/report.rb | 111 ++++++++++++++++++++++----------- config/initializers/extends.rb | 25 +++++--- 2 files changed, 92 insertions(+), 44 deletions(-) diff --git a/app/models/report.rb b/app/models/report.rb index 982c2d9dc..3e85a90cc 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -93,21 +93,8 @@ class Report < ApplicationRecord def self.generate_whole_project_report(project, current_user, current_team) report_contents = gen_element_content(project, nil, 'project_header', true) - - project.experiments.each do |exp| - modules = [] - - exp.my_modules.each do |my_module| - module_children = [] - - Extends::EXPORT_ALL_MY_MODULE_ELEMENTS.each do |my_module_element| - module_children += public_send("generate_my_module_#{my_module_element}_element", my_module, project) - end - modules += gen_element_content(my_module, nil, 'my_module', true, nil, module_children) - end - - report_contents += - gen_element_content(exp, nil, 'experiment', true, nil, modules) + Extends::EXPORT_ALL_PROJECT_ELEMENTS.each do |report_element, children| + report_contents += public_send("generate_#{report_element}_element", project, children) end report = Report.new @@ -151,27 +138,73 @@ class Report < ApplicationRecord elements end - def self.generate_my_module_protocol_element(my_module, _project) - gen_element_content(my_module, nil, 'my_module_protocol', true) - end - - def self.generate_my_module_steps_element(my_module, _project) - my_module_steps = [] - my_module.protocol.steps.each do |step| - step_children = gen_element_content(step, step.assets, 'step_asset') - step_children += gen_element_content(step, step.tables, 'step_table') - step_children += gen_element_content(step, step.checklists, 'step_checklist') - step_children += gen_element_content(step, nil, 'step_comments', true, 'asc') - - my_module_steps += gen_element_content(step, nil, 'step', true, nil, step_children) + def self.generate_experiments_element(project, elements) + experiments = [] + project.experiments.each do |experiment| + experiment_contents = [] + elements.each do |report_element, children| + experiment_contents += public_send("generate_#{report_element}_element", experiment, children) + end + experiments += gen_element_content(experiment, nil, 'experiment', true, nil, experiment_contents) end - my_module_steps + experiments end - def self.generate_my_module_results_element(my_module, _project) - my_module_results = [] + def self.generate_my_modules_element(experiment, elements) + my_modules = [] + experiment.my_modules.each do |my_module| + my_module_contents = [] + elements.each do |report_element, children| + my_module_contents += public_send("generate_#{report_element}_element", my_module, children) + end + my_modules += gen_element_content(my_module, nil, 'my_module', true, nil, my_module_contents) + end + my_modules + end + + def self.generate_my_module_protocol_element(my_module, elements) + protcol_contents = [] + elements.each do |report_element, children| + protcol_contents += public_send("generate_#{report_element}_element", my_module, children) + end + gen_element_content(my_module, nil, 'my_module_protocol', true, nil, protcol_contents) + end + + def self.generate_my_module_steps_element(my_module, elements) + steps = [] + my_module.protocol.steps.each do |step| + step_contents = [] + elements.each do |report_element, children| + step_contents += public_send("generate_#{report_element}_element", step, children) + end + steps += gen_element_content(step, nil, 'step', true, nil, step_contents) + end + steps + end + + def self.generate_step_assets_element(step, elements) + gen_element_content(step, step.assets, 'step_asset') + end + + def self.generate_step_tables_element(step, elements) + gen_element_content(step, step.tables, 'step_table') + end + + def self.generate_step_checklists_element(step, elements) + gen_element_content(step, step.checklists, 'step_checklist') + end + + def self.generate_step_comments_element(step, elements) + gen_element_content(step, nil, 'step_comments', true, 'asc') + end + + def self.generate_my_module_results_element(my_module, elements) + results = [] my_module.results.each do |result| - result_children = gen_element_content(result, nil, 'result_comments', true, 'asc') + result_contents = [] + elements.each do |report_element, children| + result_contents += public_send("generate_#{report_element}_element", result, children) + end result_type = if result.asset 'result_asset' elsif result.table @@ -179,17 +212,21 @@ class Report < ApplicationRecord elsif result.result_text 'result_text' end - my_module_results += gen_element_content(result, nil, result_type, true, nil, result_children) + results += gen_element_content(result, nil, result_type, true, nil, result_contents) end - my_module_results + results end - def self.generate_my_module_activities_element(my_module, _project) + def self.generate_result_comments_element(result, elements) + gen_element_content(result, nil, 'result_comments', true, 'asc') + end + + def self.generate_my_module_activities_element(my_module, elements) gen_element_content(my_module, nil, 'my_module_activity', true, 'asc') end - def self.generate_my_module_repositories_element(my_module, project) - repositories = project.assigned_repositories_and_snapshots + def self.generate_my_module_repositories_element(my_module, elements) + repositories = my_module.experiment.project.assigned_repositories_and_snapshots gen_element_content(my_module, repositories, 'my_module_repository', true, 'asc') end diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 4a6a64039..284f94648 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -41,13 +41,24 @@ class Extends my_module_repository: 17, my_module_protocol: 18 } - EXPORT_ALL_MY_MODULE_ELEMENTS = [ - :protocol, - :steps, - :results, - :activities, - :repositories - ] + EXPORT_ALL_PROJECT_ELEMENTS = { + experiments: { + my_modules: { + my_module_protocol: {}, + my_module_steps: { + step_assets: {}, + step_tables: {}, + step_checklists: {}, + step_comments: {} + }, + my_module_results: { + result_comments: {} + }, + my_module_activities: {}, + my_module_repositories: {} + } + } + } # Data type name should match corresponding model's name REPOSITORY_DATA_TYPES = { RepositoryTextValue: 0, From e240b629c4d861818e30ffd100aed231f953f078 Mon Sep 17 00:00:00 2001 From: aignatov-bio Date: Wed, 14 Oct 2020 12:12:32 +0200 Subject: [PATCH 4/8] Fix rubocop issues --- app/models/report.rb | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/app/models/report.rb b/app/models/report.rb index 3e85a90cc..688c71e74 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Report < ApplicationRecord include SearchableModel include SearchableByNameModel @@ -58,7 +60,7 @@ class Report < ApplicationRecord end def root_elements - (report_elements.order(:position)).select { |el| el.parent.blank? } + report_elements.order(:position).select { |el| el.parent.blank? } end # Save the JSON represented contents to this report @@ -66,7 +68,7 @@ class Report < ApplicationRecord def save_with_contents(json_contents) begin Report.transaction do - #First, save the report itself + # First, save the report itself save! # Secondly, delete existing report elements @@ -80,15 +82,13 @@ class Report < ApplicationRecord rescue ActiveRecord::ActiveRecordError, ArgumentError return false end - return true + true end # Clean report elements from report # the function runs before the report is edit def cleanup_report - report_elements.each do |el| - el.clean_removed_or_archived_elements - end + report_elements.each(&:clean_removed_or_archived_elements) end def self.generate_whole_project_report(project, current_user, current_team) @@ -130,8 +130,8 @@ class Report < ApplicationRecord end ids_hash["#{type}_id"] = obj.id if extra_id_needed end, - 'sort_order' => sort_order.present? ? sort_order : nil, - 'children' => children.present? ? children : [] + 'sort_order' => sort_order.presence, + 'children' => children.presence || [] } end @@ -182,19 +182,19 @@ class Report < ApplicationRecord steps end - def self.generate_step_assets_element(step, elements) + def self.generate_step_assets_element(step, _elements) gen_element_content(step, step.assets, 'step_asset') end - def self.generate_step_tables_element(step, elements) + def self.generate_step_tables_element(step, _elements) gen_element_content(step, step.tables, 'step_table') end - def self.generate_step_checklists_element(step, elements) + def self.generate_step_checklists_element(step, _elements) gen_element_content(step, step.checklists, 'step_checklist') end - def self.generate_step_comments_element(step, elements) + def self.generate_step_comments_element(step, _elements) gen_element_content(step, nil, 'step_comments', true, 'asc') end @@ -217,15 +217,15 @@ class Report < ApplicationRecord results end - def self.generate_result_comments_element(result, elements) + def self.generate_result_comments_element(result, _elements) gen_element_content(result, nil, 'result_comments', true, 'asc') end - def self.generate_my_module_activities_element(my_module, elements) + def self.generate_my_module_activities_element(my_module, _elements) gen_element_content(my_module, nil, 'my_module_activity', true, 'asc') end - def self.generate_my_module_repositories_element(my_module, elements) + def self.generate_my_module_repositories_element(my_module, _elements) repositories = my_module.experiment.project.assigned_repositories_and_snapshots gen_element_content(my_module, repositories, 'my_module_repository', true, 'asc') end From dac8dda6bcfb3635ce176e7c67973e9a8edf0ebb Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Wed, 14 Oct 2020 14:54:29 +0200 Subject: [PATCH 5/8] Update comment --- config/initializers/extends.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 7a5ff89fb..435083fcd 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -298,7 +298,7 @@ class Extends restore_inventory: 145, export_inventory_items_assigned_to_task: 146, export_inventory_snapshot_items_assigned_to_task: 147, - change_status_on_task_flow: 148 # 149, 150, 151 in AdddOn! + change_status_on_task_flow: 148 # 149..157 in AdddOn! } ACTIVITY_GROUPS = { From bdcc7939ada792a3924168416d56323d4e08be3d Mon Sep 17 00:00:00 2001 From: aignatov-bio Date: Fri, 16 Oct 2020 18:25:01 +0200 Subject: [PATCH 6/8] Update export all generator --- app/models/report.rb | 137 +++++---------------------------- config/initializers/extends.rb | 102 ++++++++++++++++++++---- 2 files changed, 105 insertions(+), 134 deletions(-) diff --git a/app/models/report.rb b/app/models/report.rb index 688c71e74..1686fa725 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -92,10 +92,7 @@ class Report < ApplicationRecord end def self.generate_whole_project_report(project, current_user, current_team) - report_contents = gen_element_content(project, nil, 'project_header', true) - Extends::EXPORT_ALL_PROJECT_ELEMENTS.each do |report_element, children| - report_contents += public_send("generate_#{report_element}_element", project, children) - end + report_contents = gen_element_content(project, Extends::EXPORT_ALL_PROJECT_ELEMENTS) report = Report.new report.name = loop do @@ -110,126 +107,32 @@ class Report < ApplicationRecord report end - def self.gen_element_content(parent_obj, association_objs, type_of, - use_parent_id = false, sort_order = nil, - children = nil) - parent_type = parent_obj.class.name.underscore - type = type_of.split('_').last.singularize - extra_id_needed = use_parent_id && !association_objs.nil? + def self.gen_element_content(parent, children) elements = [] - association_objs ||= [nil] - association_objs.each do |obj| - elements << { - 'type_of' => type_of, - 'id' => {}.tap do |ids_hash| - if use_parent_id - ids_hash["#{parent_type}_id"] = parent_obj.id - else - ids_hash["#{type}_id"] = obj.id - end - ids_hash["#{type}_id"] = obj.id if extra_id_needed - end, - 'sort_order' => sort_order.presence, - 'children' => children.presence || [] + children.each do |element| + element_hash = lambda { |object| + hash_object = { + 'type_of' => element[:type_of] || element[:type_of_lambda].call(object), + 'id' => { element[:id_key] => object.id }, + 'sort_order' => element[:sort_order], + 'children' => gen_element_content(object, element[:children]) + } + hash_object['id'][element[:parent_id_key]] = parent.id if element[:parent_id_key] + hash_object } - end + if element[:relation] + (element[:relation].inject(parent) { |p, method| p.public_send(method) }).each do |child| + elements.push(element_hash.call(child)) + end + else + elements.push(element_hash.call(parent)) + end + end elements end - def self.generate_experiments_element(project, elements) - experiments = [] - project.experiments.each do |experiment| - experiment_contents = [] - elements.each do |report_element, children| - experiment_contents += public_send("generate_#{report_element}_element", experiment, children) - end - experiments += gen_element_content(experiment, nil, 'experiment', true, nil, experiment_contents) - end - experiments - end - - def self.generate_my_modules_element(experiment, elements) - my_modules = [] - experiment.my_modules.each do |my_module| - my_module_contents = [] - elements.each do |report_element, children| - my_module_contents += public_send("generate_#{report_element}_element", my_module, children) - end - my_modules += gen_element_content(my_module, nil, 'my_module', true, nil, my_module_contents) - end - my_modules - end - - def self.generate_my_module_protocol_element(my_module, elements) - protcol_contents = [] - elements.each do |report_element, children| - protcol_contents += public_send("generate_#{report_element}_element", my_module, children) - end - gen_element_content(my_module, nil, 'my_module_protocol', true, nil, protcol_contents) - end - - def self.generate_my_module_steps_element(my_module, elements) - steps = [] - my_module.protocol.steps.each do |step| - step_contents = [] - elements.each do |report_element, children| - step_contents += public_send("generate_#{report_element}_element", step, children) - end - steps += gen_element_content(step, nil, 'step', true, nil, step_contents) - end - steps - end - - def self.generate_step_assets_element(step, _elements) - gen_element_content(step, step.assets, 'step_asset') - end - - def self.generate_step_tables_element(step, _elements) - gen_element_content(step, step.tables, 'step_table') - end - - def self.generate_step_checklists_element(step, _elements) - gen_element_content(step, step.checklists, 'step_checklist') - end - - def self.generate_step_comments_element(step, _elements) - gen_element_content(step, nil, 'step_comments', true, 'asc') - end - - def self.generate_my_module_results_element(my_module, elements) - results = [] - my_module.results.each do |result| - result_contents = [] - elements.each do |report_element, children| - result_contents += public_send("generate_#{report_element}_element", result, children) - end - result_type = if result.asset - 'result_asset' - elsif result.table - 'result_table' - elsif result.result_text - 'result_text' - end - results += gen_element_content(result, nil, result_type, true, nil, result_contents) - end - results - end - - def self.generate_result_comments_element(result, _elements) - gen_element_content(result, nil, 'result_comments', true, 'asc') - end - - def self.generate_my_module_activities_element(my_module, _elements) - gen_element_content(my_module, nil, 'my_module_activity', true, 'asc') - end - - def self.generate_my_module_repositories_element(my_module, _elements) - repositories = my_module.experiment.project.assigned_repositories_and_snapshots - gen_element_content(my_module, repositories, 'my_module_repository', true, 'asc') - end - private # Recursively save a single JSON element diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 284f94648..15519fc2a 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -41,24 +41,92 @@ class Extends my_module_repository: 17, my_module_protocol: 18 } - EXPORT_ALL_PROJECT_ELEMENTS = { - experiments: { - my_modules: { - my_module_protocol: {}, - my_module_steps: { - step_assets: {}, - step_tables: {}, - step_checklists: {}, - step_comments: {} - }, - my_module_results: { - result_comments: {} - }, - my_module_activities: {}, - my_module_repositories: {} - } + EXPORT_ALL_PROJECT_ELEMENTS = [ + { + type_of: 'project_header', + id_key: 'project_id', + children: [] + }, + { + type_of: 'experiment', + id_key: 'experiment_id', + relation: %w(experiments), + children: [ + { + type_of: 'my_module', + id_key: 'my_module_id', + relation: %w(my_modules), + children: [ + { + type_of: 'my_module_protocol', + id_key: 'my_module_id', + children: [] + }, + { + type_of: 'step', + relation: %w(protocol steps), + id_key: 'step_id', + children: [ + { + type_of: 'step_asset', + relation: %w(assets), + id_key: 'asset_id', + children: [] + }, + { + type_of: 'step_table', + relation: %w(tables), + id_key: 'table_id', + children: [] + }, + { + type_of: 'step_checklist', + relation: %w(checklists), + id_key: 'checklist_id', + children: [] + }, + { + type_of: 'step_comments', + id_key: 'step_id', + sort_order: 'asc', + children: [] + } + ] + }, + { + type_of_lambda: lambda { |result| + (result.result_asset || + result.result_table || + result.result_text).class.to_s.underscore + }, + relation: %w(results), + id_key: 'result_id', + children: [{ + type_of: 'result_comments', + id_key: 'result_id', + sort_order: 'asc', + children: [] + }] + }, + { + type_of: 'my_module_activity', + id_key: 'my_module_id', + sort_order: 'asc', + children: [] + }, + { + type_of: 'my_module_repository', + relation: %w(experiment project assigned_repositories_and_snapshots), + id_key: 'repository_id', + parent_id_key: 'my_module_id', + sort_order: 'asc', + children: [] + } + ] + } + ] } - } + ] # Data type name should match corresponding model's name REPOSITORY_DATA_TYPES = { RepositoryTextValue: 0, From f4bf2ab75fa9c36acc3e18af59e999b717bcdcc3 Mon Sep 17 00:00:00 2001 From: aignatov-bio Date: Thu, 22 Oct 2020 14:29:35 +0200 Subject: [PATCH 7/8] Remove empty children from extents --- app/models/report.rb | 2 +- config/initializers/extends.rb | 15 +++------------ 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/app/models/report.rb b/app/models/report.rb index 1686fa725..84556d095 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -116,7 +116,7 @@ class Report < ApplicationRecord 'type_of' => element[:type_of] || element[:type_of_lambda].call(object), 'id' => { element[:id_key] => object.id }, 'sort_order' => element[:sort_order], - 'children' => gen_element_content(object, element[:children]) + 'children' => gen_element_content(object, element[:children] || []) } hash_object['id'][element[:parent_id_key]] = parent.id if element[:parent_id_key] hash_object diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 15519fc2a..025d54ef8 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -45,7 +45,6 @@ class Extends { type_of: 'project_header', id_key: 'project_id', - children: [] }, { type_of: 'experiment', @@ -60,7 +59,6 @@ class Extends { type_of: 'my_module_protocol', id_key: 'my_module_id', - children: [] }, { type_of: 'step', @@ -71,25 +69,21 @@ class Extends type_of: 'step_asset', relation: %w(assets), id_key: 'asset_id', - children: [] }, { type_of: 'step_table', relation: %w(tables), id_key: 'table_id', - children: [] }, { type_of: 'step_checklist', relation: %w(checklists), id_key: 'checklist_id', - children: [] }, { type_of: 'step_comments', id_key: 'step_id', sort_order: 'asc', - children: [] } ] }, @@ -104,23 +98,20 @@ class Extends children: [{ type_of: 'result_comments', id_key: 'result_id', - sort_order: 'asc', - children: [] + sort_order: 'asc' }] }, { type_of: 'my_module_activity', id_key: 'my_module_id', - sort_order: 'asc', - children: [] + sort_order: 'asc' }, { type_of: 'my_module_repository', relation: %w(experiment project assigned_repositories_and_snapshots), id_key: 'repository_id', parent_id_key: 'my_module_id', - sort_order: 'asc', - children: [] + sort_order: 'asc' } ] } From 3b0f35aef31bd5ca4008e0cdf86742c14acf1f87 Mon Sep 17 00:00:00 2001 From: aignatov-bio Date: Thu, 22 Oct 2020 14:33:17 +0200 Subject: [PATCH 8/8] Fix markup --- config/initializers/extends.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 025d54ef8..2e0ee4734 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -44,7 +44,7 @@ class Extends EXPORT_ALL_PROJECT_ELEMENTS = [ { type_of: 'project_header', - id_key: 'project_id', + id_key: 'project_id' }, { type_of: 'experiment', @@ -58,7 +58,7 @@ class Extends children: [ { type_of: 'my_module_protocol', - id_key: 'my_module_id', + id_key: 'my_module_id' }, { type_of: 'step', @@ -68,22 +68,22 @@ class Extends { type_of: 'step_asset', relation: %w(assets), - id_key: 'asset_id', + id_key: 'asset_id' }, { type_of: 'step_table', relation: %w(tables), - id_key: 'table_id', + id_key: 'table_id' }, { type_of: 'step_checklist', relation: %w(checklists), - id_key: 'checklist_id', + id_key: 'checklist_id' }, { type_of: 'step_comments', id_key: 'step_id', - sort_order: 'asc', + sort_order: 'asc' } ] },