From c47681635977e8a7968ff14c88bce3ce48f7329b Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Mon, 18 Mar 2019 15:03:14 +0100 Subject: [PATCH] Task activities refactoring, without tests --- app/controllers/canvas_controller.rb | 15 +- .../my_module_comments_controller.rb | 56 ++------ app/controllers/my_modules_controller.rb | 136 ++++-------------- app/controllers/user_my_modules_controller.rb | 45 ++---- app/models/experiment.rb | 61 ++++---- config/locales/en.yml | 6 +- 6 files changed, 92 insertions(+), 227 deletions(-) diff --git a/app/controllers/canvas_controller.rb b/app/controllers/canvas_controller.rb index c055f9199..992aa1cc4 100644 --- a/app/controllers/canvas_controller.rb +++ b/app/controllers/canvas_controller.rb @@ -183,14 +183,13 @@ class CanvasController < ApplicationController to_archive.each do |module_id| my_module = MyModule.find_by_id(module_id) next if my_module.blank? - Activity.create(type_of: :archive_module, - project: my_module.experiment.project, - experiment: my_module.experiment, - my_module: my_module, - user: current_user, - message: t('activities.archive_module', - user: current_user.full_name, - module: my_module.name)) + Activities::CreateActivityService + .call(activity_type: :archive_module, + owner: current_user, + team: my_module.experiment.project.team, + project: my_module.experiment.project, + subject: my_module, + message_items: { my_module: @my_module.id }) end # Create workflow image diff --git a/app/controllers/my_module_comments_controller.rb b/app/controllers/my_module_comments_controller.rb index 3f7840c15..d2ff2d41a 100644 --- a/app/controllers/my_module_comments_controller.rb +++ b/app/controllers/my_module_comments_controller.rb @@ -51,21 +51,8 @@ class MyModuleCommentsController < ApplicationController respond_to do |format| if @comment.save - my_module_comment_annotation_notification - # Generate activity - Activity.create( - type_of: :add_comment_to_module, - user: current_user, - project: @my_module.experiment.project, - experiment: @my_module.experiment, - my_module: @my_module, - message: t( - 'activities.add_comment_to_module', - user: current_user.full_name, - module: @my_module.name - ) - ) + log_activity(:add_comment_to_module) format.json do render json: { @@ -112,21 +99,9 @@ class MyModuleCommentsController < ApplicationController respond_to do |format| format.json do if @comment.save - my_module_comment_annotation_notification(old_text) - # Generate activity - Activity.create( - type_of: :edit_module_comment, - user: current_user, - project: @my_module.experiment.project, - experiment: @my_module.experiment, - my_module: @my_module, - message: t( - 'activities.edit_module_comment', - user: current_user.full_name, - module: @my_module.name - ) - ) + log_activity(:edit_module_comment) + message = custom_auto_link(@comment.message, team: current_team) render json: { comment: message }, status: :ok else @@ -141,19 +116,8 @@ class MyModuleCommentsController < ApplicationController respond_to do |format| format.json do if @comment.destroy - # Generate activity - Activity.create( - type_of: :delete_module_comment, - user: current_user, - project: @my_module.experiment.project, - experiment: @my_module.experiment, - my_module: @my_module, - message: t( - 'activities.delete_module_comment', - user: current_user.full_name, - module: @my_module.name - ) - ) + log_activity(:delete_module_comment) + # 'counter' and 'linked_id' are used for counter badge render json: { linked_id: @my_module.id, counter: @my_module.task_comments.count }, @@ -217,4 +181,14 @@ class MyModuleCommentsController < ApplicationController ))) ) end + + def log_activity(type_of) + Activities::CreateActivityService + .call(activity_type: type_of, + owner: current_user, + team: @my_module.experiment.project.team, + project: @my_module.experiment.project, + subject: @my_module, + message_items: { my_module: @my_module.id }) + end end diff --git a/app/controllers/my_modules_controller.rb b/app/controllers/my_modules_controller.rb index 15089a128..2c5e36f1a 100644 --- a/app/controllers/my_modules_controller.rb +++ b/app/controllers/my_modules_controller.rb @@ -162,54 +162,21 @@ class MyModulesController < ApplicationController saved = @my_module.archive(current_user) if saved # Currently not in use - Activity.create( - type_of: :archive_module, - project: @my_module.experiment.project, - experiment: @my_module.experiment, - my_module: @my_module, - user: current_user, - message: t( - 'activities.archive_module', - user: current_user.full_name, - module: @my_module.name - ) - ) + log_activity(:archive_module) end elsif @my_module.archived_changed?(from: true, to: false) saved = @my_module.restore(current_user) if saved restored = true - Activity.create( - type_of: :restore_module, - project: @my_module.experiment.project, - experiment: @my_module.experiment, - my_module: @my_module, - user: current_user, - message: t( - 'activities.restore_module', - user: current_user.full_name, - module: @my_module.name - ) - ) + log_activity(:restore_module) end else saved = @my_module.save if saved if description_changed - Activity.create( - type_of: :change_module_description, - project: @my_module.experiment.project, - experiment: @my_module.experiment, - my_module: @my_module, - user: current_user, - message: t( - 'activities.change_module_description', - user: current_user.full_name, - module: @my_module.name - ) - ) + log_activity(:change_module_description) end if due_date_changes @@ -337,21 +304,6 @@ class MyModulesController < ApplicationController my_module.samples.push(*new_samples) task_names << my_module.name end - if new_samples.any? - Activity.create( - type_of: :assign_sample, - project: @my_module.experiment.project, - experiment: @my_module.experiment, - my_module: @my_module, - user: current_user, - message: I18n.t( - 'activities.assign_sample', - user: current_user.full_name, - tasks: task_names.join(', '), - samples: new_samples.map(&:name).join(', ') - ) - ) - end end redirect_to samples_my_module_path(@my_module) end @@ -375,21 +327,6 @@ class MyModulesController < ApplicationController task_names << my_module.name my_module.samples.destroy(samples & my_module.samples) end - if samples.any? - Activity.create( - type_of: :unassign_sample, - project: @my_module.experiment.project, - experiment: @my_module.experiment, - my_module: @my_module, - user: current_user, - message: I18n.t( - 'activities.unassign_sample', - user: current_user.full_name, - tasks: task_names.join(', '), - samples: samples.map(&:name).join(', ') - ) - ) - end end redirect_to samples_my_module_path(@my_module) end @@ -484,19 +421,8 @@ class MyModulesController < ApplicationController if records_names.any? records_names.uniq! - Activity.create( - type_of: :assign_repository_record, - project: @project, - experiment: @experiment, - my_module: @my_module, - user: current_user, - message: I18n.t('activities.assign_repository_records', - user: current_user.full_name, - task: @my_module.name, - repository: @repository.name, - records: records_names.join(', ') - ) - ) + log_activity_assign_unassign_records(:assign_repository_record, + records_names) flash = I18n.t('repositories.assigned_records_flash', records: records_names.join(', ')) flash = I18n.t('repositories.assigned_records_downstream_flash', @@ -542,20 +468,8 @@ class MyModulesController < ApplicationController records.update_all(last_modified_by_id: current_user.id) if records.any? - Activity.create( - type_of: :unassign_repository_record, - project: @project, - experiment: @experiment, - my_module: @my_module, - user: current_user, - message: I18n.t( - 'activities.unassign_repository_records', - user: current_user.full_name, - task: @my_module.name, - repository: @repository.name, - records: records.map(&:name).join(', ') - ) - ) + log_activity_assign_unassign_records(:unassign_repository_record, + records.map(&:name)) flash = I18n.t('repositories.unassigned_records_flash', records: records.map(&:name).join(', ')) respond_to do |format| @@ -663,19 +577,7 @@ class MyModulesController < ApplicationController def task_completion_activity completed = @my_module.completed? - str = 'activities.uncomplete_module' - str = 'activities.complete_module' if completed - message = t(str, - user: current_user.full_name, - module: @my_module.name) - Activity.create( - user: current_user, - project: @project, - experiment: @experiment, - my_module: @my_module, - message: message, - type_of: completed ? :complete_task : :uncomplete_task - ) + log_activity(completed ? :complete_task : :uncomplete_task) start_work_on_next_task_notification end @@ -760,4 +662,26 @@ class MyModulesController < ApplicationController params.require(:my_module).permit(:name, :description, :due_date, :archived) end + + def log_activity(type_of) + Activities::CreateActivityService + .call(activity_type: type_of, + owner: current_user, + team: @my_module.experiment.project.team, + project: @my_module.experiment.project, + subject: @my_module, + message_items: { my_module: @my_module.id }) + end + + def log_activity_assign_unassign_records(type_of, records_names) + Activities::CreateActivityService + .call(activity_type: type_of, + owner: current_user, + team: @my_module.experiment.project.team, + project: @my_module.experiment.project, + subject: @my_module, + message_items: { my_module: @my_module.id, + repository: @repository.id, + record_names: records_names.join(', ') }) + end end diff --git a/app/controllers/user_my_modules_controller.rb b/app/controllers/user_my_modules_controller.rb index 846402f1a..61338902c 100644 --- a/app/controllers/user_my_modules_controller.rb +++ b/app/controllers/user_my_modules_controller.rb @@ -40,21 +40,8 @@ class UserMyModulesController < ApplicationController @um = UserMyModule.new(um_params.merge(my_module: @my_module)) @um.assigned_by = current_user if @um.save - # Create activity - message = t( - 'activities.assign_user_to_module', - assigned_user: @um.user.full_name, - module: @my_module.name, - assigned_by_user: current_user.full_name - ) - Activity.create( - user: current_user, - project: @um.my_module.experiment.project, - experiment: @um.my_module.experiment, - my_module: @um.my_module, - message: message, - type_of: :assign_user_to_module - ) + log_activity(:assign_user_to_module) + respond_to do |format| format.json do redirect_to my_module_users_edit_path(format: :json), @@ -75,22 +62,7 @@ class UserMyModulesController < ApplicationController def destroy if @um.destroy - # Create activity - message = t( - "activities.unassign_user_from_module", - unassigned_user: @um.user.full_name, - module: @my_module.name, - unassigned_by_user: current_user.full_name - ) - - Activity.create( - user: current_user, - project: @um.my_module.experiment.project, - experiment: @um.my_module.experiment, - my_module: @um.my_module, - message: message, - type_of: :unassign_user_from_module - ) + log_activity(:unassign_user_from_module) respond_to do |format| format.json do @@ -144,4 +116,15 @@ class UserMyModulesController < ApplicationController def um_params params.require(:user_my_module).permit(:user_id, :my_module_id) end + + def log_activity(type_of) + Activities::CreateActivityService + .call(activity_type: type_of, + owner: current_user, + team: @um.my_module.experiment.project.team, + project: @um.my_module.experiment.project, + subject: @um.my_module, + message_items: { my_module: @my_module.id, + user_target: @um.user.id }) + end end diff --git a/app/models/experiment.rb b/app/models/experiment.rb index cdcf8c3a1..60b36eaf0 100644 --- a/app/models/experiment.rb +++ b/app/models/experiment.rb @@ -146,28 +146,20 @@ class Experiment < ApplicationRecord rename_modules(to_rename, current_user) # Add activities that modules were created - originals.each do |m| - Activity.create(type_of: :create_module, - user: current_user, - project: project, - experiment: m.experiment, - my_module: m, - message: I18n.t('activities.create_module', - user: current_user.full_name, - module: m.name)) + originals.each do |my_module| + log_activity(:create_module, current_user, my_module) end # Add activities that modules were cloned cloned_pairs.each do |mn, mo| - Activity.create(type_of: :clone_module, - project: mn.experiment.project, - experiment: mn.experiment, - my_module: mn, - user: current_user, - message: I18n.t('activities.clone_module', - user: current_user.full_name, - module_new: mn.name, - module_original: mo.name)) + Activities::CreateActivityService + .call(activity_type: :clone_module, + owner: current_user, + team: project.team, + project: mn.experiment.project, + subject: mn, + message_items: { my_module_original: mo.id, + my_module_new: mn.id }) end # Then, archive modules that need to be archived @@ -263,17 +255,9 @@ class Experiment < ApplicationRecord # Archive all modules. Receives an array of module integer IDs # and current user. def archive_modules(module_ids, current_user) - my_modules.where(id: module_ids).each do |m| - m.archive!(current_user) - - Activities::CreateActivityService.call(activity_type: :archive_task, - owner: current_user, - subject: m, - project: project, - team: project.team, - message_items: { - my_module: m.id - }) + my_modules.where(id: module_ids).each do |my_module| + my_module.archive!(current_user) + log_activity(:archive_task, current_user, my_module) end my_modules.reload end @@ -321,14 +305,7 @@ class Experiment < ApplicationRecord if my_module.present? my_module.name = new_name my_module.save! - Activities::CreateActivityService.call(activity_type: :rename_task, - owner: current_user, - subject: my_module, - project: project, - team: project.team, - message_items: { - my_module: my_module.id - }) + log_activity(:rename_task, current_user, my_module) end end end @@ -618,4 +595,14 @@ class Experiment < ApplicationRecord rescue false end + + def log_activity(type_of, current_user, my_module) + Activities::CreateActivityService + .call(activity_type: type_of, + owner: current_user, + team: my_module.experiment.project.team, + project: my_module.experiment.project, + subject: my_module, + message_items: { my_module: my_module.id }) + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 9a4c6b58f..4285e9fab 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1334,10 +1334,8 @@ en: change_module_description: "%{user} changed task %{module}'s description." complete_module: "%{user} completed task %{module}." uncomplete_module: "%{user} uncompleted task %{module}." - assign_sample: "%{user} assigned sample(s) %{samples} to task(s) %{tasks}" - unassign_sample: "%{user} unassigned sample(s) %{samples} from task(s) %{tasks}" - assign_repository_records: "%{user} assigned item(s) %{records} from %{repository} inventory to task %{task}" - unassign_repository_records: "%{user} unassigned item(s) %{records} from %{repository} inventory from task %{task}" + assign_repository_records: "%{user} assigned item(s) %{record_names} from %{repository} inventory to task %{my_module}" + unassign_repository_records: "%{user} unassigned item(s) %{record_names} from %{repository} inventory from task %{my_module}" create_step: "%{user} created Step %{step} %{step_name}." destroy_step: "%{user} deleted Step %{step} %{step_name}." add_comment_to_step: "%{user} commented on Step %{step} %{step_name}."