From d706a18e2d760677f600e0f4572be90e2272008e Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Tue, 12 Feb 2019 13:27:55 +0100 Subject: [PATCH 1/4] Add flag for archived tasks to experiment exporter --- app/services/model_exporters/experiment_exporter.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/services/model_exporters/experiment_exporter.rb b/app/services/model_exporters/experiment_exporter.rb index b74d3edb7..74476a41a 100644 --- a/app/services/model_exporters/experiment_exporter.rb +++ b/app/services/model_exporters/experiment_exporter.rb @@ -3,6 +3,7 @@ module ModelExporters class ExperimentExporter < ModelExporter def initialize(experiment_id) + @include_archived = true @experiment = Experiment.find_by_id(experiment_id) raise StandardError, 'Can not load experiment' unless @experiment @@ -11,6 +12,7 @@ module ModelExporters def export_template_to_dir @asset_counter = 0 + @include_archived = false @experiment.transaction do @experiment.uuid ||= SecureRandom.uuid @dir_to_export = FileUtils.mkdir_p( @@ -35,10 +37,17 @@ module ModelExporters end def experiment + if @include_archived + my_modules = @experiment.my_modules + my_module_groups = @experiment.my_module_groups + else + my_modules = @experiment.active_my_modules + my_module_groups = @experiment.active_module_groups + end return { experiment: @experiment, - my_modules: @experiment.my_modules.map { |m| my_module(m) }, - my_module_groups: @experiment.my_module_groups + my_modules: my_modules.map { |m| my_module(m) }, + my_module_groups: my_module_groups }, @assets_to_copy end From 5825c7120d2ab790a2735231cc774dfd764f32be Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Wed, 13 Feb 2019 11:49:01 +0100 Subject: [PATCH 2/4] fix falling test --- spec/services/templates_service_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/services/templates_service_spec.rb b/spec/services/templates_service_spec.rb index 717460afa..da4c1844c 100644 --- a/spec/services/templates_service_spec.rb +++ b/spec/services/templates_service_spec.rb @@ -37,9 +37,9 @@ describe TemplatesService do expect(tmpl_exp.name).to eq(demo_exp.name) expect(tmpl_exp.uuid).to_not eq(nil) expect(tmpl_exp.my_modules.pluck(:name)) - .to match_array(demo_exp.my_modules.pluck(:name)) + .to match_array(demo_exp.active_my_modules.pluck(:name)) tmpl_tasks = tmpl_exp.my_modules - demo_tasks = demo_exp.my_modules + demo_tasks = demo_exp.active_my_modules demo_tasks.each do |demo_task| tmpl_task = tmpl_tasks.find_by_name(demo_task.name) expect(tmpl_task.name).to eq(demo_task.name) From 0e14c126e557e67afa4c97e8e08e33f865621fa6 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Thu, 14 Feb 2019 10:53:48 +0100 Subject: [PATCH 3/4] Add scope instead of method for active my_module_group --- app/models/experiment.rb | 6 ------ app/models/my_module_group.rb | 4 ++++ app/services/model_exporters/experiment_exporter.rb | 4 ++-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/app/models/experiment.rb b/app/models/experiment.rb index 0dca3b772..0e7740469 100644 --- a/app/models/experiment.rb +++ b/app/models/experiment.rb @@ -100,12 +100,6 @@ class Experiment < ApplicationRecord end end - def active_module_groups - my_module_groups.joins(:my_modules) - .where('my_modules.archived = ?', false) - .distinct - end - def active_modules my_modules.where(archived: false) end diff --git a/app/models/my_module_group.rb b/app/models/my_module_group.rb index 37c36adc9..2de05fa71 100644 --- a/app/models/my_module_group.rb +++ b/app/models/my_module_group.rb @@ -10,6 +10,10 @@ class MyModuleGroup < ApplicationRecord optional: true has_many :my_modules, inverse_of: :my_module_group, dependent: :nullify + scope :active, (lambda do + joins(:my_modules).where('my_modules.archived = ?', false).distinct + end) + def deep_clone_to_experiment(current_user, experiment) clone = MyModuleGroup.new( created_by: created_by, diff --git a/app/services/model_exporters/experiment_exporter.rb b/app/services/model_exporters/experiment_exporter.rb index 74476a41a..4874016ed 100644 --- a/app/services/model_exporters/experiment_exporter.rb +++ b/app/services/model_exporters/experiment_exporter.rb @@ -41,8 +41,8 @@ module ModelExporters my_modules = @experiment.my_modules my_module_groups = @experiment.my_module_groups else - my_modules = @experiment.active_my_modules - my_module_groups = @experiment.active_module_groups + my_modules = @experiment.my_modules.active + my_module_groups = @experiment.my_module_groups.active end return { experiment: @experiment, From 343a46584f06ce2df69d4d79411b167da47dfaa9 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Thu, 14 Feb 2019 14:54:04 +0100 Subject: [PATCH 4/4] Rename scope --- app/models/my_module_group.rb | 2 +- app/services/model_exporters/experiment_exporter.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/my_module_group.rb b/app/models/my_module_group.rb index 2de05fa71..718c53fc0 100644 --- a/app/models/my_module_group.rb +++ b/app/models/my_module_group.rb @@ -10,7 +10,7 @@ class MyModuleGroup < ApplicationRecord optional: true has_many :my_modules, inverse_of: :my_module_group, dependent: :nullify - scope :active, (lambda do + scope :without_archived_modules, (lambda do joins(:my_modules).where('my_modules.archived = ?', false).distinct end) diff --git a/app/services/model_exporters/experiment_exporter.rb b/app/services/model_exporters/experiment_exporter.rb index 4874016ed..0d5a49318 100644 --- a/app/services/model_exporters/experiment_exporter.rb +++ b/app/services/model_exporters/experiment_exporter.rb @@ -42,7 +42,7 @@ module ModelExporters my_module_groups = @experiment.my_module_groups else my_modules = @experiment.my_modules.active - my_module_groups = @experiment.my_module_groups.active + my_module_groups = @experiment.my_module_groups.without_archived_modules end return { experiment: @experiment,