From 70eebca5ee531a927890dcd914abc3fd48d32d73 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Tue, 5 Mar 2019 11:39:57 +0100 Subject: [PATCH] experiment activities refactoring --- app/controllers/experiments_controller.rb | 46 ++++++------------- .../copy_experiment_as_template_service.rb | 20 ++++---- .../experiments/move_to_project_service.rb | 23 ++++------ .../experiments_controller_spec.rb | 44 +++++++++++++++--- ...opy_experiment_as_template_service_sepc.rb | 7 +++ .../move_to_project_service_spec.rb | 7 +++ 6 files changed, 81 insertions(+), 66 deletions(-) diff --git a/app/controllers/experiments_controller.rb b/app/controllers/experiments_controller.rb index ef8fb7c5b..cffd51cf4 100644 --- a/app/controllers/experiments_controller.rb +++ b/app/controllers/experiments_controller.rb @@ -45,19 +45,8 @@ class ExperimentsController < ApplicationController @experiment.last_modified_by = current_user @experiment.project = @project if @experiment.save - experiment_annotation_notification - Activity.create( - type_of: :create_experiment, - project: @experiment.project, - experiment: @experiment, - user: current_user, - message: I18n.t( - 'activities.create_experiment', - user: current_user.full_name, - experiment: @experiment.name - ) - ) + log_activity(:create_experiment) flash[:success] = t('experiments.create.success_flash', experiment: @experiment.name) respond_to do |format| @@ -105,7 +94,6 @@ class ExperimentsController < ApplicationController @experiment.last_modified_by = current_user if @experiment.save - experiment_annotation_notification(old_text) activity_type = if experiment_params[:archived] == 'false' @@ -113,15 +101,7 @@ class ExperimentsController < ApplicationController else :edit_experiment end - Activities::CreateActivityService - .call(activity_type: activity_type, - owner: current_user, - subject: @experiment, - project: @experiment.project, - team: @experiment.project.team, - message_items: { - experiment: @experiment.id - }) + log_activity(activity_type) @experiment.touch(:workflowimg_updated_at) flash[:success] = t('experiments.update.success_flash', @@ -153,17 +133,7 @@ class ExperimentsController < ApplicationController @experiment.archived_by = current_user @experiment.archived_on = DateTime.now if @experiment.save - Activity.create( - type_of: :archive_experiment, - project: @experiment.project, - experiment: @experiment, - user: current_user, - message: I18n.t( - 'activities.archive_experiment', - user: current_user.full_name, - experiment: @experiment.name - ) - ) + log_activity(:archive_experiment) flash[:success] = t('experiments.archive.success_flash', experiment: @experiment.name) @@ -351,4 +321,14 @@ class ExperimentsController < ApplicationController canvas_experiment_url(@experiment))) ) end + + def log_activity(type_of) + Activities::CreateActivityService + .call(activity_type: type_of, + owner: current_user, + team: @experiment.project.team, + project: @experiment.project, + subject: @experiment, + message_items: { experiment: @experiment.id }) + end end diff --git a/app/services/experiments/copy_experiment_as_template_service.rb b/app/services/experiments/copy_experiment_as_template_service.rb index 5655d84c2..156266ea6 100644 --- a/app/services/experiments/copy_experiment_as_template_service.rb +++ b/app/services/experiments/copy_experiment_as_template_service.rb @@ -86,18 +86,14 @@ module Experiments end def track_activity - Activity.create( - type_of: :clone_experiment, - project: @project, - experiment: @exp, - user: @user, - message: I18n.t( - 'activities.clone_experiment', - user: @user, - experiment_new: @c_exp.name, - experiment_original: @exp.name - ) - ) + Activities::CreateActivityService + .call(activity_type: :clone_experiment, + owner: @user, + team: @project.team, + project: @project, + subject: @exp, + message_items: { experiment_new: @c_exp.id, + experiment_original: @exp.id }) end end end diff --git a/app/services/experiments/move_to_project_service.rb b/app/services/experiments/move_to_project_service.rb index 91749337d..09a40f142 100644 --- a/app/services/experiments/move_to_project_service.rb +++ b/app/services/experiments/move_to_project_service.rb @@ -73,20 +73,15 @@ module Experiments end def track_activity - Activity.create( - type_of: :move_experiment, - project: @project, - team: @project.team, - subject: @exp, - owner: @user, - message: I18n.t( - 'activities.move_experiment', - user: @user, - experiment: @exp.name, - project_new: @project.name, - project_original: @original_project.name - ) - ) + Activities::CreateActivityService + .call(activity_type: :move_experiment, + owner: @user, + team: @project.team, + project: @project, + subject: @exp, + message_items: { experiment: @exp.id, + project_new: @project.id, + project_original: @original_project.id }) end end end diff --git a/spec/controllers/experiments_controller_spec.rb b/spec/controllers/experiments_controller_spec.rb index 98815257b..cf869c3c2 100644 --- a/spec/controllers/experiments_controller_spec.rb +++ b/spec/controllers/experiments_controller_spec.rb @@ -5,14 +5,30 @@ require 'rails_helper' describe ExperimentsController, type: :controller do login_user - describe 'PUT update' do - let!(:user) { controller.current_user } - let!(:team) { create :team, created_by: user, users: [user] } - let!(:project) { create :project, team: team } - let!(:user_project) do - create :user_project, :owner, user: user, project: project + let!(:user) { controller.current_user } + let!(:team) { create :team, created_by: user, users: [user] } + let!(:project) { create :project, team: team } + let!(:user_project) do + create :user_project, :owner, user: user, project: project + end + let(:experiment) { create :experiment, project: project } + + describe '#create' do + let(:params) do + { project_id: project.id, + experiment: { name: 'test experiment A1', + description: 'test description one' } } end - let(:experiment) { create :experiment, project: project } + + it 'calls create activity service' do + expect(Activities::CreateActivityService).to receive(:call) + .with(hash_including(activity_type: :create_experiment)) + + post :create, params: params, format: :json + end + end + + describe 'PUT update' do let(:action) { put :update, params: params } context 'when editing experiment' do @@ -67,4 +83,18 @@ describe ExperimentsController, type: :controller do end end end + + describe '#archive' do + let(:params) do + { id: experiment.id, + experiment: { name: 'test experiment A1', + description: 'test description one' } } + end + it 'calls create activity service' do + expect(Activities::CreateActivityService).to receive(:call) + .with(hash_including(activity_type: :archive_experiment)) + + get :archive, params: params, format: :json + end + end end diff --git a/spec/services/experiments/copy_experiment_as_template_service_sepc.rb b/spec/services/experiments/copy_experiment_as_template_service_sepc.rb index 8865e44a4..316e0940a 100644 --- a/spec/services/experiments/copy_experiment_as_template_service_sepc.rb +++ b/spec/services/experiments/copy_experiment_as_template_service_sepc.rb @@ -32,6 +32,13 @@ describe Experiments::CopyExperimentAsTemplateService do expect { service_call }.to(change { Activity.all.count }) end + it 'calls create activity service' do + expect(Activities::CreateActivityService).to receive(:call) + .with(hash_including(activity_type: :clone_experiment)) + + service_call + end + it 'copies all tasks to new experiment' do expect(service_call.cloned_experiment.my_modules.count) .to be == experiment.my_modules.count diff --git a/spec/services/experiments/move_to_project_service_spec.rb b/spec/services/experiments/move_to_project_service_spec.rb index c78f25359..48ef29701 100644 --- a/spec/services/experiments/move_to_project_service_spec.rb +++ b/spec/services/experiments/move_to_project_service_spec.rb @@ -54,6 +54,13 @@ describe Experiments::MoveToProjectService do it 'adds Activity record' do expect { service_call }.to(change { Activity.all.count }) end + + it 'calls create activity service' do + expect(Activities::CreateActivityService).to receive(:call) + .with(hash_including(activity_type: :move_experiment)) + + service_call + end end context 'when call with invalid params' do