Results activities refactoring

This commit is contained in:
Mojca Lorber 2019-03-13 19:05:29 +01:00
parent 1418abc8d7
commit 7757096693
14 changed files with 402 additions and 196 deletions

View file

@ -67,14 +67,8 @@ class ReportsController < ApplicationController
@report.last_modified_by = current_user
if continue && @report.save_with_contents(report_contents)
# record an activity
Activities::CreateActivityService
.call(activity_type: :create_report,
owner: current_user,
subject: @report,
team: @report.team,
project: @report.project,
message_items: { report: @report.id })
log_activity(:create_report)
respond_to do |format|
format.json do
render json: { url: reports_path }, status: :ok
@ -109,14 +103,8 @@ class ReportsController < ApplicationController
@report.assign_attributes(report_params)
if continue && @report.save_with_contents(report_contents)
# record an activity
Activities::CreateActivityService
.call(activity_type: :edit_report,
owner: current_user,
subject: @report,
team: @report.team,
project: @report.project,
message_items: { report: @report.id })
log_activity(:edit_report)
respond_to do |format|
format.json do
render json: { url: reports_path }, status: :ok
@ -504,4 +492,14 @@ class ReportsController < ApplicationController
:repository_item_id,
:html)
end
def log_activity(type_of)
Activities::CreateActivityService
.call(activity_type: type_of,
owner: current_user,
subject: @report,
team: @report.team,
project: @report.project,
message_items: { report: @report.id })
end
end

View file

@ -93,18 +93,7 @@ class ResultAssetsController < ApplicationController
success_flash = t('result_assets.archive.success_flash',
module: @my_module.name)
if saved
Activity.create(
type_of: :archive_result,
project: @my_module.experiment.project,
experiment: @my_module.experiment,
my_module: @my_module,
user: current_user,
message: t(
'activities.archive_asset_result',
user: current_user.full_name,
result: @result.name
)
)
log_activity(:archive_result)
end
elsif @result.archived_changed?(from: true, to: false)
render_403
@ -135,16 +124,7 @@ class ResultAssetsController < ApplicationController
# Post process new file if neccesary
@result.asset.post_process_file(team) if @result.asset.present?
Activity.create(
type_of: :edit_result,
user: current_user,
project: @my_module.experiment.project,
experiment: @my_module.experiment,
my_module: @my_module,
message: t('activities.edit_asset_result',
user: current_user.full_name,
result: @result.name)
)
log_activity(:edit_result)
end
end
@ -229,20 +209,33 @@ class ResultAssetsController < ApplicationController
asset.post_process_file(@my_module.experiment.project.team)
# Generate activity
Activity.create(
type_of: :add_result,
user: current_user,
project: @my_module.experiment.project,
experiment: @my_module.experiment,
my_module: @my_module,
message: t('activities.add_asset_result',
user: current_user.full_name,
result: result.name)
)
Activities::CreateActivityService
.call(activity_type: :add_result,
owner: current_user,
subject: result,
team: @my_module.experiment.project.team,
project: @my_module.experiment.project,
message_items: {
result: result.id,
result_type: t('activities.result_type.asset')
})
else
success = false
end
end
{ status: success, results: results }
end
def log_activity(type_of)
Activities::CreateActivityService
.call(activity_type: type_of,
owner: current_user,
subject: @result,
team: @my_module.experiment.project.team,
project: @my_module.experiment.project,
message_items: {
result: @result.id,
result_type: t('activities.result_type.asset')
})
end
end

View file

@ -50,21 +50,8 @@ class ResultCommentsController < ApplicationController
respond_to do |format|
if @comment.save
result_comment_annotation_notification
# Generate activity
Activity.create(
type_of: :add_comment_to_result,
user: current_user,
project: @result.my_module.experiment.project,
experiment: @result.my_module.experiment,
my_module: @result.my_module,
message: t(
'activities.add_comment_to_result',
user: current_user.full_name,
result: @result.name
)
)
log_activity(:add_comment_to_result)
format.json {
render json: {
@ -109,21 +96,8 @@ class ResultCommentsController < ApplicationController
respond_to do |format|
format.json do
if @comment.save
result_comment_annotation_notification(old_text)
# Generate activity
Activity.create(
type_of: :edit_result_comment,
user: current_user,
project: @result.my_module.experiment.project,
experiment: @result.my_module.experiment,
my_module: @result.my_module,
message: t(
'activities.edit_result_comment',
user: current_user.full_name,
result: @result.name
)
)
log_activity(:edit_result_comment)
message = custom_auto_link(@comment.message, team: current_team)
render json: { comment: message }, status: :ok
else
@ -138,19 +112,7 @@ class ResultCommentsController < ApplicationController
respond_to do |format|
format.json do
if @comment.destroy
# Generate activity
Activity.create(
type_of: :delete_result_comment,
user: current_user,
project: @result.my_module.experiment.project,
experiment: @result.my_module.experiment,
my_module: @result.my_module,
message: t(
'activities.delete_result_comment',
user: current_user.full_name,
result: @result.name
)
)
log_activity(:delete_result_comment)
render json: {}, status: :ok
else
render json: { message: I18n.t('comments.delete_error') },
@ -212,4 +174,14 @@ class ResultCommentsController < ApplicationController
)))
)
end
def log_activity(type_of)
Activities::CreateActivityService
.call(activity_type: type_of,
owner: current_user,
subject: @result,
team: @result.my_module.experiment.project.team,
project: @result.my_module.experiment.project,
message_items: { result: @result.id })
end
end

View file

@ -43,19 +43,7 @@ class ResultTablesController < ApplicationController
respond_to do |format|
if (@result.save and @table.save) then
# Generate activity
Activity.create(
type_of: :add_result,
user: current_user,
project: @my_module.experiment.project,
experiment: @my_module.experiment,
my_module: @my_module,
message: t(
"activities.add_table_result",
user: current_user.full_name,
result: @result.name
)
)
log_activity(:add_result)
format.html {
flash[:success] = t(
@ -103,18 +91,7 @@ class ResultTablesController < ApplicationController
flash_success = t("result_tables.archive.success_flash",
module: @my_module.name)
if saved
Activity.create(
type_of: :archive_result,
project: @my_module.experiment.project,
experiment: @my_module.experiment,
my_module: @my_module,
user: current_user,
message: t(
'activities.archive_table_result',
user: current_user.full_name,
result: @result.name
)
)
log_activity(:archive_result)
end
elsif @result.archived_changed?(from: true, to: false)
render_403
@ -122,18 +99,7 @@ class ResultTablesController < ApplicationController
saved = @result.save
if saved then
Activity.create(
type_of: :edit_result,
user: current_user,
project: @my_module.experiment.project,
experiment: @my_module.experiment,
my_module: @my_module,
message: t(
"activities.edit_table_result",
user: current_user.full_name,
result: @result.name
)
)
log_activity(:edit_result)
end
end
respond_to do |format|
@ -214,4 +180,17 @@ class ResultTablesController < ApplicationController
]
)
end
def log_activity(type_of)
Activities::CreateActivityService
.call(activity_type: type_of,
owner: current_user,
subject: @result,
team: @my_module.experiment.project.team,
project: @my_module.experiment.project,
message_items: {
result: @result.id,
result_type: t('activities.result_type.table')
})
end
end

View file

@ -49,19 +49,7 @@ class ResultTextsController < ApplicationController
link_tiny_mce_assets(@result_text.text, @result_text)
result_annotation_notification
# Generate activity
Activity.create(
type_of: :add_result,
user: current_user,
project: @my_module.experiment.project,
experiment: @my_module.experiment,
my_module: @my_module,
message: t(
"activities.add_text_result",
user: current_user.full_name,
result: @result.name
)
)
log_activity(:add_result)
format.html {
flash[:success] = t(
@ -115,18 +103,7 @@ class ResultTextsController < ApplicationController
success_flash = t("result_texts.archive.success_flash",
module: @my_module.name)
if saved
Activity.create(
type_of: :archive_result,
project: @my_module.experiment.project,
experiment: @my_module.experiment,
my_module: @my_module,
user: current_user,
message: t(
'activities.archive_text_result',
user: current_user.full_name,
result: @result.name
)
)
log_activity(:archive_result)
end
elsif @result.archived_changed?(from: true, to: false)
render_403
@ -134,18 +111,7 @@ class ResultTextsController < ApplicationController
saved = @result.save
if saved then
Activity.create(
type_of: :edit_result,
user: current_user,
project: @my_module.experiment.project,
experiment: @my_module.experiment,
my_module: @my_module,
message: t(
"activities.edit_text_result",
user: current_user.full_name,
result: @result.name
)
)
log_activity(:edit_result)
end
end
@ -242,4 +208,17 @@ class ResultTextsController < ApplicationController
)))
)
end
def log_activity(type_of)
Activities::CreateActivityService
.call(activity_type: type_of,
owner: current_user,
subject: @result,
team: @my_module.experiment.project.team,
project: @my_module.experiment.project,
message_items: {
result: @result.id,
result_type: t('activities.result_type.text')
})
end
end

View file

@ -3,27 +3,21 @@ class ResultsController < ApplicationController
before_action :check_destroy_permissions
def destroy
act_log = t('my_modules.module_archive.table_log',
user: current_user.name,
result: @result.name,
date: l(Time.current, format: :full_date))
act_log = t('my_modules.module_archive.text_log',
user: current_user.name,
result: @result.name,
date: l(Time.current, format: :full_date)) if @result.is_text
act_log = t('my_modules.module_archive.asset_log',
user: current_user.name,
result: @result.name,
date: l(Time.current, format: :full_date)) if @result.is_asset
Activity.create(
type_of: :destroy_result,
user: current_user,
project: @my_module.experiment.project,
experiment: @my_module.experiment,
my_module: @my_module,
message: act_log
)
result_type = if @result.is_text
t('activities.result_type.text')
elsif @result.is_table
t('activities.result_type.table')
elsif @result.is_asset
t('activities.result_type.asset')
end
Activities::CreateActivityService
.call(activity_type: :destroy_result,
owner: current_user,
subject: @result,
team: @my_module.experiment.project.team,
project: @my_module.experiment.project,
message_items: { result: @result.id,
result_type: result_type })
flash[:success] = t('my_modules.module_archive.delete_flash',
result: @result.name,
module: @my_module.name)

View file

@ -587,9 +587,6 @@ en:
option_delete: "Delete"
confirm_delete: "Are you sure you want to permanently delete result?"
delete_flash: "Sucessfully removed result <strong>%{result}</strong> from task <strong>%{module}</strong>."
table_log: "%{user} deleted table result <strong>%{result}</strong> on %{date}"
text_log: "%{user} deleted text result <strong>%{result}</strong> on %{date}"
asset_log: "%{user} deleted file result <strong>%{result}</strong> on %{date}"
archived_on: "Archived on"
archived_on_title: "Result archived on %{date} at %{time}."
option_download: "Download"
@ -1316,6 +1313,10 @@ en:
collapse_all: "Collapse all"
modal:
modal_title: "Activities"
result_type:
text: "text"
table: "table"
asset: "file"
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}."
@ -1345,16 +1346,11 @@ en:
check_step_checklist_item: "<i>%{user}</i> completed checklist item <strong>%{checkbox}</strong> (%{completed}/%{all} completed) in Step %{step} <strong>%{step_name}</strong>."
uncheck_step_checklist_item: "<i>%{user}</i> uncompleted checklist item <strong>%{checkbox}</strong> (%{completed}/%{all} completed) in Step %{step} <strong>%{step_name}</strong>."
edit_step: "<i>%{user}</i> edited Step %{step} <strong>%{step_name}</strong>."
add_asset_result: "<i>%{user}</i> added file result <strong>%{result}</strong>."
add_text_result: "<i>%{user}</i> added text result <strong>%{result}</strong>."
add_table_result: "<i>%{user}</i> added table result <strong>%{result}</strong>."
add_result: "<i>%{user}</i> added %{result_type} result <strong>%{result}</strong>."
add_comment_to_result: "<i>%{user}</i> commented on result <strong>%{result}</strong>."
archive_asset_result: "<i>%{user}</i> archived file result <strong>%{result}</strong>."
archive_text_result: "<i>%{user}</i> archived text result <strong>%{result}</strong>."
archive_table_result: "<i>%{user}</i> archived table result <strong>%{result}</strong>."
edit_asset_result: "<i>%{user}</i> edited file result <strong>%{result}</strong>."
edit_text_result: "<i>%{user}</i> edited text result <strong>%{result}</strong>."
edit_table_result: "<i>%{user}</i> edited table result <strong>%{result}</strong>."
archive_result: "<i>%{user}</i> archived %{result_type} result <strong>%{result}</strong>."
destroy_result: "<i>%{user}</i> deleted %{result_type} result <strong>%{result}</strong>."
edit_result: "<i>%{user}</i> edited %{result_type} result <strong>%{result}</strong>."
archive_experiment: "<i>%{user}</i> archived experiment <strong>%{experiment}</strong>."
create_experiment: "<i>%{user}</i> created experiment <strong>%{experiment}</strong>."
edit_experiment: "<i>%{user}</i> edited experiment <strong>%{experiment}</strong>."

View file

@ -0,0 +1,58 @@
# frozen_string_literal: true
require 'rails_helper'
describe ResultAssetsController, type: :controller do
login_user
let(:user) { User.first }
let!(:team) { create :team, :with_members }
let!(:user_project) { create :user_project, :owner, user: user }
let(:project) do
create :project, team: team, user_projects: [user_project]
end
let(:experiment) { create :experiment, project: project }
let(:task) { create :my_module, name: 'test task', experiment: experiment }
let(:result) do
create :result, name: 'test result', my_module: task, user: user
end
let(:result_asset) { create :result_asset, result: result }
describe '#create' do
let(:params) do
{ my_module_id: task.id,
results_names: { '0': 'result name created' },
results_files:
{ '0': fixture_file_upload('files/export.csv', 'text/csv') } }
end
it 'calls create activity service' do
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :add_result))
post :create, params: params, format: :json
end
end
describe '#update' do
let(:params) do
{ id: result_asset.id,
result: { name: result.name } }
end
it 'calls create activity service (edit_result)' do
params[:result][:name] = 'test result changed'
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :edit_result))
put :update, params: params, format: :json
end
it 'calls create activity service (archive_result)' do
params[:result][:archived] = 'true'
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :archive_result))
put :update, params: params, format: :json
end
end
end

View file

@ -0,0 +1,74 @@
# frozen_string_literal: true
require 'rails_helper'
describe ResultCommentsController, type: :controller do
login_user
let(:user) { User.first }
let!(:team) { create :team, :with_members }
let!(:user_project) { create :user_project, :owner, user: user }
let(:project) do
create :project, team: team, user_projects: [user_project]
end
let(:experiment) { create :experiment, project: project }
let(:task) { create :my_module, name: 'test task', experiment: experiment }
let(:result) do
create :result, name: 'test result', my_module: task, user: user
end
let!(:result_text) do
create :result_text, text: 'test text result', result: result
end
let(:result_comment) do
create :result_comment, message: 'test comment result',
result: result,
user: user
end
describe '#create' do
context 'in JSON format' do
let(:params) do
{ result_id: result.id,
comment: { message: 'test comment' } }
end
it 'calls create activity service' do
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :add_comment_to_result))
post :create, params: params, format: :json
end
end
end
describe '#update' do
context 'in JSON format' do
let(:params) do
{ result_id: result.id,
id: result_comment.id,
comment: { message: 'test comment updated' } }
end
it 'calls create activity service' do
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :edit_result_comment))
put :update, params: params, format: :json
end
end
end
describe '#destroy' do
let(:params) do
{ result_id: result.id,
id: result_comment.id }
end
it 'calls create activity service' do
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :delete_result_comment))
delete :destroy, params: params, format: :json
end
end
end

View file

@ -0,0 +1,59 @@
# frozen_string_literal: true
require 'rails_helper'
describe ResultTablesController, type: :controller do
login_user
let(:user) { User.first }
let!(:team) { create :team, :with_members }
let!(:user_project) { create :user_project, :owner, user: user }
let(:project) do
create :project, team: team, user_projects: [user_project]
end
let(:experiment) { create :experiment, project: project }
let(:task) { create :my_module, name: 'test task', experiment: experiment }
let(:result) do
create :result, name: 'test result', my_module: task, user: user
end
let(:result_table) { create :result_table, result: result }
describe '#create' do
let(:params) do
{ my_module_id: task.id,
result:
{ name: 'result name created',
table_attributes:
{ contents: '{\"data\":[[\"a\",\"b\",\"1\",null,null]]}' } } }
end
it 'calls create activity service' do
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :add_result))
post :create, params: params, format: :json
end
end
describe '#update' do
let(:params) do
{ id: result_table.id,
result: { name: result.name } }
end
it 'calls create activity service (edit_result)' do
params[:result][:name] = 'test result changed'
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :edit_result))
put :update, params: params, format: :json
end
it 'calls create activity service (archive_result)' do
params[:result][:archived] = 'true'
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :archive_result))
put :update, params: params, format: :json
end
end
end

View file

@ -0,0 +1,59 @@
# frozen_string_literal: true
require 'rails_helper'
describe ResultTextsController, type: :controller do
login_user
let(:user) { User.first }
let!(:team) { create :team, :with_members }
let!(:user_project) { create :user_project, :owner, user: user }
let(:project) do
create :project, team: team, user_projects: [user_project]
end
let(:experiment) { create :experiment, project: project }
let(:task) { create :my_module, name: 'test task', experiment: experiment }
let(:result) do
create :result, name: 'test result', my_module: task, user: user
end
let(:result_text) do
create :result_text, text: 'test text result', result: result
end
describe '#create' do
let(:params) do
{ my_module_id: task.id,
result: { name: 'result name created',
result_text_attributes: { text: 'result text created' } } }
end
it 'calls create activity service' do
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :add_result))
post :create, params: params, format: :json
end
end
describe '#update' do
let(:params) do
{ id: result_text.id,
result: { name: result.name } }
end
it 'calls create activity service (edit_result)' do
params[:result][:name] = 'test result changed'
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :edit_result))
put :update, params: params, format: :json
end
it 'calls create activity service (archive_result)' do
params[:result][:archived] = 'true'
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :archive_result))
put :update, params: params, format: :json
end
end
end

View file

@ -0,0 +1,35 @@
# frozen_string_literal: true
require 'rails_helper'
describe ResultsController, type: :controller do
login_user
let(:user) { User.first }
let!(:team) { create :team, :with_members }
let!(:user_project) { create :user_project, :owner, user: user }
let(:project) do
create :project, team: team, user_projects: [user_project]
end
let(:experiment) { create :experiment, project: project }
let(:task) { create :my_module, name: 'test task', experiment: experiment }
let(:result) do
create :result, name: 'test result', my_module: task, user: user
end
let!(:result_text) do
create :result_text, text: 'test text result', result: result
end
describe '#destroy' do
let(:params) do
{ id: result.id }
end
it 'calls create activity service' do
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :destroy_result))
delete :destroy, params: params
end
end
end

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
FactoryBot.define do
factory :result_comment do
user
message { Faker::Lorem.sentence }
result { create(:result) }
end
end

View file

@ -3,5 +3,6 @@
FactoryBot.define do
factory :result_table do
table { create(:table) }
result { create(:result) }
end
end