Reports activities refactoring

This commit is contained in:
Mojca Lorber 2019-03-11 13:26:54 +01:00
parent d1e38e5bc9
commit 1418abc8d7
4 changed files with 92 additions and 32 deletions

View file

@ -68,16 +68,13 @@ class ReportsController < ApplicationController
if continue && @report.save_with_contents(report_contents)
# record an activity
Activity.create(
type_of: :create_report,
Activities::CreateActivityService
.call(activity_type: :create_report,
owner: current_user,
subject: @report,
team: @report.team,
project: @report.project,
user: current_user,
message: I18n.t(
'activities.create_report',
user: current_user.full_name,
report: @report.name
)
)
message_items: { report: @report.id })
respond_to do |format|
format.json do
render json: { url: reports_path }, status: :ok
@ -113,16 +110,13 @@ class ReportsController < ApplicationController
if continue && @report.save_with_contents(report_contents)
# record an activity
Activity.create(
type_of: :edit_report,
Activities::CreateActivityService
.call(activity_type: :edit_report,
owner: current_user,
subject: @report,
team: @report.team,
project: @report.project,
user: current_user,
message: I18n.t(
'activities.edit_report',
user: current_user.full_name,
report: @report.name
)
)
message_items: { report: @report.id })
respond_to do |format|
format.json do
render json: { url: reports_path }, status: :ok
@ -149,16 +143,13 @@ class ReportsController < ApplicationController
report = Report.find_by_id(report_id)
next unless report.present? && can_manage_reports?(current_team)
# record an activity
Activity.create(
type_of: :delete_report,
Activities::CreateActivityService
.call(activity_type: :delete_report,
owner: current_user,
subject: report,
team: report.team,
project: report.project,
user: current_user,
message: I18n.t(
'activities.delete_report',
user: current_user.full_name,
report: report.name
)
)
message_items: { report: report.id })
report.destroy
end

View file

@ -85,11 +85,11 @@ class Extends
FILE_FA_ICON_MAPPINGS = {}
ACTIVITY_SUBJECT_TYPES = %w(
Team Repository Project Experiment MyModule Result Protocol Step
Team Repository Project Experiment MyModule Result Protocol Step Report
).freeze
SEARCHABLE_ACTIVITY_SUBJECT_TYPES = %w(
Repository Project Experiment MyModule Result Protocol Step
Repository Project Experiment MyModule Result Protocol Step Report
).freeze
ACTIVITY_MESSAGE_ITEMS_TYPES =

View file

@ -0,0 +1,68 @@
# frozen_string_literal: true
require 'rails_helper'
describe ReportsController, type: :controller do
login_user
let(:user) { User.first }
let!(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, team: team, user: user }
let(:user_project) { create :user_project, :owner, user: user }
let(:project) do
create :project, team: team, user_projects: [user_project]
end
let(:report) do
create :report, user: user, project: project, team: team,
name: 'test repot A1', description: 'test description A1'
end
describe '#create' do
context 'in JSON format' do
let(:params) do
{ project_id: project.id,
report: { name: 'test report created',
description: 'test description created' },
report_contents: '[{"type_of":"project_header","id":{"project_id":' +
project.id.to_s + '},"sort_order":null,"children":[]}]' }
end
it 'calls create activity service' do
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :create_report))
post :create, params: params, format: :json
end
end
end
describe '#update' do
context 'in JSON format' do
let(:params) do
{ project_id: project.id,
id: report.id,
report: { name: 'test report update',
description: 'test description update' },
report_contents: '[{"type_of":"project_header","id":{"project_id":' +
project.id.to_s + '},"sort_order":null,"children":[]}]' }
end
it 'calls create activity service' do
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :edit_report))
put :update, params: params, format: :json
end
end
end
describe '#destroy' do
let(:params) { { report_ids: "[#{report.id}]" } }
it 'calls create activity service' do
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :delete_report))
delete :destroy, params: params
end
end
end

View file

@ -6,5 +6,6 @@ FactoryBot.define do
project
team
name { Faker::Name.unique.name }
description { Faker::Lorem.sentence }
end
end