Protocol activities refactoring, without tests

This commit is contained in:
Mojca Lorber 2019-03-19 16:12:07 +01:00
parent c476816359
commit cd7a87348f
7 changed files with 134 additions and 198 deletions

View file

@ -376,6 +376,7 @@ class ProtocolsController < ApplicationController
end
else
# Everything good, display flash & render 200
log_activity_task_protocol(:update_protocol_in_task_from_repository)
flash[:success] = t(
'my_modules.protocols.revert_flash'
)
@ -415,18 +416,7 @@ class ProtocolsController < ApplicationController
end
else
# Everything good, record activity, display flash & render 200
Activity.create(
type_of: :revert_protocol,
project: @protocol.my_module.experiment.project,
experiment: @protocol.my_module.experiment,
my_module: @protocol.my_module,
user: current_user,
message: I18n.t(
'activities.revert_protocol',
user: current_user.full_name,
protocol: @protocol.name
)
)
log_activity_task_protocol(:update_protocol_in_repository_from_task)
flash[:success] = t(
'my_modules.protocols.update_parent_flash'
)
@ -466,6 +456,7 @@ class ProtocolsController < ApplicationController
end
else
# Everything good, display flash & render 200
log_activity_task_protocol(:update_protocol_in_task_from_repository)
flash[:success] = t(
'my_modules.protocols.update_from_parent_flash'
)
@ -505,18 +496,7 @@ class ProtocolsController < ApplicationController
end
else
# Everything good, record activity, display flash & render 200
Activity.create(
type_of: :load_protocol_from_repository,
project: @protocol.my_module.experiment.project,
experiment: @protocol.my_module.experiment,
my_module: @protocol.my_module,
user: current_user,
message: I18n.t(
'activities.load_protocol_from_repository',
user: current_user.full_name,
protocol: @source.name
)
)
log_activity_task_protocol(:load_protocol_to_task_from_repository)
flash[:success] = t('my_modules.protocols.load_from_repository_flash')
flash.keep(:success)
format.json { render json: {}, status: :ok }
@ -553,18 +533,15 @@ class ProtocolsController < ApplicationController
end
else
# Everything good, record activity, display flash & render 200
Activity.create(
type_of: :load_protocol_from_file,
project: @protocol.my_module.experiment.project,
experiment: @protocol.my_module.experiment,
my_module: @protocol.my_module,
user: current_user,
message: I18n.t(
'activities.load_protocol_from_file',
user: current_user.full_name,
protocol: @protocol_json[:name]
)
)
Activities::CreateActivityService
.call(activity_type: :load_protocol_to_task_from_file,
owner: current_user,
subject: @protocol,
team: current_team,
project: @protocol.my_module.experiment.project,
message_items: {
protocol: @protocol.id
})
flash[:success] = t(
'my_modules.protocols.load_from_file_flash'
)
@ -1234,4 +1211,17 @@ class ProtocolsController < ApplicationController
protocol: @protocol.id
})
end
def log_activity_task_protocol(type_of)
Activities::CreateActivityService
.call(activity_type: type_of,
owner: current_user,
subject: @protocol,
team: current_team,
project: @protocol.my_module.experiment.project,
message_items: {
protocol_task: @protocol.id,
protocol_reporitory: @protocol.parent.id
})
end
end

View file

@ -52,19 +52,7 @@ class StepCommentsController < ApplicationController
step_comment_annotation_notification
# Generate activity (this can only occur in module,
# but nonetheless check if my module is not nil)
Activity.create(
type_of: :add_comment_to_step,
user: current_user,
project: @step.my_module.experiment.project,
experiment: @step.my_module.experiment,
my_module: @step.my_module,
message: t(
"activities.add_comment_to_step",
user: current_user.full_name,
step: @step.position + 1,
step_name: @step.name
)
)
log_activity(:add_comment_to_step)
format.json {
render json: {
@ -111,19 +99,8 @@ class StepCommentsController < ApplicationController
step_comment_annotation_notification(old_text)
# Generate activity
Activity.create(
type_of: :edit_step_comment,
user: current_user,
project: @step.my_module.experiment.project,
experiment: @step.my_module.experiment,
my_module: @step.my_module,
message: t(
'activities.edit_step_comment',
user: current_user.full_name,
step: @step.position + 1,
step_name: @step.name
)
)
log_activity(:edit_step_comment)
message = custom_auto_link(@comment.message, team: current_team)
render json: { comment: message }, status: :ok
else
@ -138,20 +115,7 @@ class StepCommentsController < ApplicationController
respond_to do |format|
format.json do
if @comment.destroy
# Generate activity
Activity.create(
type_of: :delete_step_comment,
user: current_user,
project: @step.my_module.experiment.project,
experiment: @step.my_module.experiment,
my_module: @step.my_module,
message: t(
'activities.delete_step_comment',
user: current_user.full_name,
step: @step.position + 1,
step_name: @step.name
)
)
log_activity(:delete_step_comment)
render json: {}, status: :ok
else
render json: { message: I18n.t('comments.delete_error') },
@ -215,4 +179,18 @@ class StepCommentsController < ApplicationController
protocols_my_module_url(@step.my_module)))
)
end
def log_activity(type_of)
Activities::CreateActivityService
.call(activity_type: type_of,
owner: current_user,
subject: @protocol,
team: current_team,
project: @step.my_module.experiment.project,
message_items: {
protocol: @protocol.id,
step: @step.id,
step_position: { id: @step.id, value_for: 'position' }
})
end
end

View file

@ -67,19 +67,7 @@ class StepsController < ApplicationController
# Generate activity
if @protocol.in_module?
Activity.create(
type_of: :create_step,
user: current_user,
project: @my_module.experiment.project,
experiment: @my_module.experiment,
my_module: @my_module,
message: t(
"activities.create_step",
user: current_user.full_name,
step: @step.position + 1,
step_name: @step.name
)
)
log_activity(:create_step, @my_module.experiment.project)
else
log_activity(:add_step_to_protocol_repository)
end
@ -186,19 +174,7 @@ class StepsController < ApplicationController
# Generate activity
if @protocol.in_module?
Activity.create(
type_of: :edit_step,
user: current_user,
project: @my_module.experiment.project,
experiment: @my_module.experiment,
my_module: @my_module,
message: t(
"activities.edit_step",
user: current_user.full_name,
step: @step.position + 1,
step_name: @step.name
)
)
log_activity(:edit_step, @my_module.experiment.project)
else
log_activity(:edit_step_in_protocol_repository)
end
@ -276,41 +252,34 @@ class StepsController < ApplicationController
# Create activity
if changed
str = if checked
'activities.check_step_checklist_item'
else
'activities.uncheck_step_checklist_item'
end
completed_items = @chk_item.checklist.checklist_items
.where(checked: true).count
all_items = @chk_item.checklist.checklist_items.count
text_activity = smart_annotation_parser(@chk_item.text)
.gsub(/\s+/, ' ')
message = t(
str,
user: current_user.full_name,
checkbox: text_activity,
step: @chk_item.checklist.step.position + 1,
step_name: @chk_item.checklist.step.name,
completed: completed_items,
all: all_items
)
type_of = if checked
:check_step_checklist_item
else
:uncheck_step_checklist_item
end
# This should always hold true (only in module can
# check items be checked, but still check just in case)
if @protocol.in_module?
Activity.create(
user: current_user,
project: @protocol.my_module.experiment.project,
experiment: @protocol.my_module.experiment,
my_module: @protocol.my_module,
message: message,
type_of: if checked
:check_step_checklist_item
else
:uncheck_step_checklist_item
end
)
Activities::CreateActivityService
.call(activity_type: type_of,
owner: current_user,
subject: @protocol,
team: current_team,
project: @protocol.my_module.experiment.project,
message_items: {
protocol: @protocol.id,
step: @chk_item.checklist.step.id,
step_position: { id: @chk_item.checklist.step.id,
value_for: 'position' },
checkbox: text_activity,
num_completed: completed_items.to_s,
num_all: all_items.to_s
})
end
end
else
@ -340,30 +309,25 @@ class StepsController < ApplicationController
if changed
completed_steps = @protocol.steps.where(completed: true).count
all_steps = @protocol.steps.count
str = 'activities.uncomplete_step'
str = 'activities.complete_step' if completed
message = t(
str,
user: current_user.full_name,
step: @step.position + 1,
step_name: @step.name,
completed: completed_steps,
all: all_steps
)
type_of = completed ? :complete_step : :uncomplete_step
# Toggling step state can only occur in
# module protocols, so my_module is always
# not nil; nonetheless, check if my_module is present
if @protocol.in_module?
Activity.create(
user: current_user,
project: @protocol.my_module.experiment.project,
experiment: @protocol.my_module.experiment,
my_module: @protocol.my_module,
message: message,
type_of: completed ? :complete_step : :uncomplete_step
)
Activities::CreateActivityService
.call(activity_type: type_of,
owner: current_user,
subject: @protocol,
team: current_team,
project: @protocol.my_module.experiment.project,
message_items: {
protocol: @protocol.id,
step: @step.id,
step_position: { id: @step.id, value_for: 'position' },
num_completed: completed_steps.to_s,
num_all: all_steps.to_s
})
end
end
@ -652,15 +616,17 @@ class StepsController < ApplicationController
)
end
def log_activity(type_of)
def log_activity(type_of, project = nil)
Activities::CreateActivityService
.call(activity_type: type_of,
owner: current_user,
subject: @protocol,
team: current_team,
project: project,
message_items: {
protocol: @protocol.id,
step: @step.id
step: @step.id,
step_position: { id: @step.id, value_for: 'position' }
})
end
end

View file

@ -131,19 +131,17 @@ class Step < ApplicationRecord
# Generate "delete" activity, but only if protocol is
# located inside module
if (protocol.my_module.present?) then
Activity.create(
type_of: :destroy_step,
project: protocol.my_module.experiment.project,
experiment: protocol.my_module.experiment,
my_module: protocol.my_module,
user: @current_user,
message: I18n.t(
"activities.destroy_step",
user: @current_user.full_name,
step: position + 1,
step_name: name
)
)
Activities::CreateActivityService
.call(activity_type: :destroy_step,
owner: @current_user,
subject: protocol,
team: protocol.my_module.experiment.project.team,
project: protocol.my_module.experiment.project,
message_items: {
protocol: protocol.id,
step: id,
step_position: { id: id, value_for: 'position' }
})
end
end

View file

@ -73,41 +73,40 @@ module WopiUtil
end
def create_wopi_file_activity(current_user, started_editing)
action = if started_editing
t('activities.wupi_file_editing.started')
else
t('activities.wupi_file_editing.finished')
end
if @assoc.class == Step
activity = Activity.new(
type_of: :start_edit_wopi_file,
user: current_user,
message: t(
started_editing ? 'activities.start_edit_wopi_file_step' :
'activities.unlock_wopi_file_step',
user: current_user.full_name,
file: @asset.file_file_name,
step: @asset.step.position + 1,
step_name: @asset.step.name
)
)
if @protocol.in_module?
activity.my_module = @protocol.my_module
activity.project = @protocol.my_module.experiment.project
project = @protocol.my_module.experiment.project
end
activity.save
Activities::CreateActivityService
.call(activity_type: :edit_wopi_file_on_step,
owner: current_user,
subject: @protocol,
team: current_team,
project: project,
message_items: {
protocol: @protocol.id,
step: @asset.step.id,
step_position: { id: @asset.step.id, value_for: 'position' },
asset_name: { id: @asset.id, value_for: 'file_file_name' },
action: action
})
elsif @assoc.class == Result
Activity.create(
type_of: :start_edit_wopi_file,
user: current_user,
project: @my_module.experiment.project,
experiment: @my_module.experiment,
my_module: @my_module,
message: t(
started_editing ? 'activities.start_edit_wopi_file_result' :
'activities.unlock_wopi_file_result',
user: current_user.full_name,
file: @asset.file_file_name,
result: @asset.result.name
)
)
Activities::CreateActivityService
.call(activity_type: :edit_wopi_file_on_result,
owner: current_user,
subject: @asset.result,
team: @my_module.experiment.project.team,
project: @my_module.experiment.project,
message_items: {
result: @asset.result.id,
asset_name: { id: @asset.id, value_for: 'file_file_name' },
action: action
})
end
end
end

View file

@ -142,9 +142,9 @@ class Extends
destroy_result: 42,
start_edit_wopi_file: 43,
unlock_wopi_file: 44,
load_protocol_from_file: 45,
load_protocol_from_repository: 46,
revert_protocol: 47,
load_protocol_to_task_from_file: 45,
load_protocol_to_task_from_repository: 46,
update_protocol_in_task_from_repository: 47,
create_report: 48,
delete_report: 49,
edit_report: 50,
@ -195,6 +195,8 @@ class Extends
export_projects: 95,
export_inventory_items: 96,
export_audit_trails: 97,
export_system_logs: 98
export_system_logs: 98,
edit_wopi_file_on_result: 99,
edit_wopi_file_on_step: 100
}.freeze
end

View file

@ -1317,6 +1317,9 @@ en:
text: "text"
table: "table"
asset: "file"
wupi_file_editing:
started: "editing started"
finished: "editing finished"
create_project: "<i>%{user}</i> created project <strong>%{project}</strong>."
rename_project: "<i>%{user}</i> renamed project <strong>%{project}</strong>."
change_project_visibility: "<i>%{user}</i> changed project <strong>%{project}</strong>'s visibility to %{visibility}."