From b91f08fe99385fca05dc51e13f71a9d59a732a68 Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Fri, 29 Mar 2019 11:24:22 +0100 Subject: [PATCH 1/4] Add activity for copying Inventory and Invetory item --- app/models/repository.rb | 7 +++++++ .../repository_actions/duplicate_rows.rb | 7 +++++++ .../references/_repository.html.erb | 2 +- config/initializers/extends.rb | 8 +++++--- config/locales/global_activities/en.yml | 10 +++++++--- db/schema.rb | 2 ++ spec/models/repository_spec.rb | 17 +++++++++++++++++ .../repository_actions/duplicate_rows_spec.rb | 12 ++++++++++++ 8 files changed, 58 insertions(+), 7 deletions(-) diff --git a/app/models/repository.rb b/app/models/repository.rb index ab43f6db7..f1f8c1a88 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -113,6 +113,13 @@ class Repository < ApplicationRecord end # If everything is okay, return new_repo + Activities::CreateActivityService + .call(activity_type: :copy_inventory, + owner: created_by, + subject: new_repo, + team: new_repo.team, + message_items: { repository_new: new_repo.id, repository_original: id }) + new_repo end diff --git a/app/services/repository_actions/duplicate_rows.rb b/app/services/repository_actions/duplicate_rows.rb index 91a412ed3..138dd0fa9 100644 --- a/app/services/repository_actions/duplicate_rows.rb +++ b/app/services/repository_actions/duplicate_rows.rb @@ -36,6 +36,13 @@ module RepositoryActions row.repository_cells.each do |cell| duplicate_repository_cell(cell, new_row) end + + Activities::CreateActivityService + .call(activity_type: :copy_inventory_item, + owner: @user, + subject: @repository, + team: @repository.team, + message_items: { repository_row_new: new_row.id, repository_row_original: row.id }) end end diff --git a/app/views/global_activities/references/_repository.html.erb b/app/views/global_activities/references/_repository.html.erb index 8cbf026f2..f0dab5953 100644 --- a/app/views/global_activities/references/_repository.html.erb +++ b/app/views/global_activities/references/_repository.html.erb @@ -3,7 +3,7 @@
<% if subject %> - <%= route_to_other_team(repositories_path(subject), + <%= route_to_other_team(repository_path(subject.id), team, subject.name&.truncate(Constants::NAME_TRUNCATION_LENGTH), title: subject.name) %> diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 0d93f5e2c..27f9bf21a 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -209,7 +209,9 @@ class Extends export_system_logs: 98, edit_wopi_file_on_result: 99, edit_wopi_file_on_step: 100, - edit_wopi_file_on_step_in_repository: 101 + edit_wopi_file_on_step_in_repository: 101, + copy_inventory: 105, + copy_inventory_item: 102 }.freeze ACTIVITY_GROUPS = { @@ -220,8 +222,8 @@ class Extends task_inventory: [55, 56], experiment: [*27..31, 57], reports: [48, 49, 50], - inventories: [*70..78], - protocol_repository: [*79..91], + inventories: [*70..78, 105, 102], + protocol_repository: [*79..91, 101], team: [*92..96], exports: [*97..98] }.freeze diff --git a/config/locales/global_activities/en.yml b/config/locales/global_activities/en.yml index 40de2109d..4e7fd0427 100644 --- a/config/locales/global_activities/en.yml +++ b/config/locales/global_activities/en.yml @@ -106,11 +106,13 @@ en: delete_item_inventory_html: "%{user} deleted inventory item %{repository_row}." create_column_inventory_html: "%{user} created column %{repository_column} in inventory %{repository}." edit_column_inventory_html: "%{user} edited column %{repository_column} in inventory %{repository}." + copy_inventory_html: "%{user} copied inventory %{repository_new} from inventory %{repository_original} as template" + copy_inventory_item_html: "%{user} copied inventory item %{repository_row_new} from inventory item %{repository_row_original}" delete_column_inventory_html: "%{user} deleted column %{repository_column} in inventory %{repository}." create_protocol_in_repository_html: "%{user} created protocol %{protocol} in Protocol repository." - add_step_to_protocol_repository_html: "%{user} created protocol %{protocol}'s step %{step} in Protocol repository." - edit_step_in_protocol_repository_html: "%{user} edited protocol %{protocol}'s step %{step} in Protocol repository." - delete_step_in_protocol_repository_html: "%{user} deleted protocol %{protocol}'s step %{step} in Protocol repository." + add_step_to_protocol_repository_html: "%{user} created protocol %{protocol}'s step %{step_position} %{step} in Protocol repository." + edit_step_in_protocol_repository_html: "%{user} edited protocol %{protocol}'s step %{step_position} %{step} in Protocol repository." + delete_step_in_protocol_repository_html: "%{user} deleted protocol %{protocol}'s step %{step_position} %{step} in Protocol repository." edit_description_in_protocol_repository_html: "%{user} edited protocol %{protocol}'s description in Protocol repository." edit_keywords_in_protocol_repository_html: "%{user} edited protocol %{protocol}'s keywords in Protocol repository." edit_authors_in_protocol_repository_html: "%{user} edited protocol %{protocol}'s authors in Protocol repository." @@ -201,6 +203,8 @@ en: delete_item_inventory: "Inventory item deleted" create_column_inventory: "Inventory column created" edit_column_inventory: "Inventory column edited" + copy_inventory: "Inventory copied" + copy_inventory_item: "Inventory item copied" delete_column_inventory: "Inventory column deleted" update_protocol_in_repository_from_task: "Protocol updated from task" create_protocol_in_repository: "Protocol created" diff --git a/db/schema.rb b/db/schema.rb index ba64bcc59..137929904 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -803,6 +803,7 @@ ActiveRecord::Schema.define(version: 20190304153544) do t.bigint "assigned_by_id" t.index ["assigned_by_id"], name: "index_user_projects_on_assigned_by_id" t.index ["project_id"], name: "index_user_projects_on_project_id" + t.index ["user_id", "project_id"], name: "index_user_projects_on_user_id_and_project_id", unique: true t.index ["user_id"], name: "index_user_projects_on_user_id" end @@ -828,6 +829,7 @@ ActiveRecord::Schema.define(version: 20190304153544) do t.bigint "assigned_by_id" t.index ["assigned_by_id"], name: "index_user_teams_on_assigned_by_id" t.index ["team_id"], name: "index_user_teams_on_team_id" + t.index ["user_id", "team_id"], name: "index_user_teams_on_user_id_and_team_id", unique: true t.index ["user_id"], name: "index_user_teams_on_user_id" end diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index 0c38f71f1..f6095cb71 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -42,4 +42,21 @@ describe Repository, type: :model do expect(repo).to_not be_valid end end + + describe '.copy' do + let(:created_by) { create :user } + let(:repository) { create :repository } + + it 'calls create activity for copying inventory' do + expect(Activities::CreateActivityService) + .to(receive(:call).with(hash_including(activity_type: :copy_inventory))) + + repository.copy(created_by, 'name for copied repo') + end + + it 'adds activity in DB' do + expect { repository.copy(created_by, 'name for copied repo') } + .to(change { Activity.count }) + end + end end diff --git a/spec/services/repository_actions/duplicate_rows_spec.rb b/spec/services/repository_actions/duplicate_rows_spec.rb index 2dc3b2cd0..5b433ab98 100644 --- a/spec/services/repository_actions/duplicate_rows_spec.rb +++ b/spec/services/repository_actions/duplicate_rows_spec.rb @@ -79,5 +79,17 @@ describe RepositoryActions::DuplicateRows do service_obj.call expect(service_obj.number_of_duplicated_items).to eq 0 end + + it 'calls create activity for copying intentory items 3 times' do + expect(Activities::CreateActivityService) + .to(receive(:call).with(hash_including(activity_type: :copy_inventory_item))).exactly(3).times + + described_class.new(user, repository, @rows_ids).call + end + + it 'adds 3 activities in DB' do + expect { described_class.new(user, repository, @rows_ids).call } + .to(change { Activity.count }.by(3)) + end end end From 0ed96d68deb39981476823eefc3c34c0593c9477 Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Fri, 29 Mar 2019 15:12:15 +0100 Subject: [PATCH 2/4] Add step position for steps in Protocol repository. Add default value for blank names also. --- app/helpers/global_activities_helper.rb | 18 ++++++++++++++++-- config/locales/global_activities/en.yml | 1 + 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/helpers/global_activities_helper.rb b/app/helpers/global_activities_helper.rb index fa98ad583..7968200e1 100644 --- a/app/helpers/global_activities_helper.rb +++ b/app/helpers/global_activities_helper.rb @@ -45,9 +45,11 @@ module GlobalActivitiesHelper path = obj.archived? ? projects_path : project_path(obj) when Experiment return current_value unless obj.navigable? + path = obj.archived? ? experiment_archive_project_path(obj.project) : canvas_experiment_path(obj) when MyModule return current_value unless obj.navigable? + path = obj.archived? ? module_archive_experiment_path(obj.experiment) : protocols_my_module_path(obj) when Protocol if obj.in_repository? @@ -59,16 +61,28 @@ module GlobalActivitiesHelper end when Result return current_value unless obj.navigable? + path = obj.archived? ? archive_my_module_path(obj.my_module) : results_my_module_path(obj.my_module) + when Step + if obj.protocol.in_repository? + path = protocols_path + elsif obj.protocol.my_module.navigable? + path = protocols_my_module_path(obj.my_module) + else + return current_value + end else return current_value end - route_to_other_team(path, obj, current_value) + route_to_other_team(path, team, current_value) end def generate_name(message_item) obj = message_item[:type].constantize.find_by_id(message_item[:id]) return message_item[:value] unless obj - obj.public_send(message_item[:getter] || 'name') + + value = obj.public_send(message_item[:getter] || 'name') + value = t('global_activities.index.no_name') if value.blank? + value end end diff --git a/config/locales/global_activities/en.yml b/config/locales/global_activities/en.yml index 4e7fd0427..243f23739 100644 --- a/config/locales/global_activities/en.yml +++ b/config/locales/global_activities/en.yml @@ -32,6 +32,7 @@ en: select_users: "Select Users" select_objects: "Select Objects" deselect_all_objects: "Deselect All Objects" + no_name: "(unnamed)" content: create_project_html: "%{user} created project %{project}." rename_project_html: "%{user} renamed project %{project}." From dc0c629798282644b8b95ee78cf40f30a131c59f Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Mon, 1 Apr 2019 08:55:09 +0200 Subject: [PATCH 3/4] Fix to show report name in Activities --- app/helpers/global_activities_helper.rb | 2 ++ db/schema.rb | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/helpers/global_activities_helper.rb b/app/helpers/global_activities_helper.rb index 7968200e1..411b6b4fb 100644 --- a/app/helpers/global_activities_helper.rb +++ b/app/helpers/global_activities_helper.rb @@ -71,6 +71,8 @@ module GlobalActivitiesHelper else return current_value end + when Report + path = reports_path else return current_value end diff --git a/db/schema.rb b/db/schema.rb index 137929904..ba64bcc59 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -803,7 +803,6 @@ ActiveRecord::Schema.define(version: 20190304153544) do t.bigint "assigned_by_id" t.index ["assigned_by_id"], name: "index_user_projects_on_assigned_by_id" t.index ["project_id"], name: "index_user_projects_on_project_id" - t.index ["user_id", "project_id"], name: "index_user_projects_on_user_id_and_project_id", unique: true t.index ["user_id"], name: "index_user_projects_on_user_id" end @@ -829,7 +828,6 @@ ActiveRecord::Schema.define(version: 20190304153544) do t.bigint "assigned_by_id" t.index ["assigned_by_id"], name: "index_user_teams_on_assigned_by_id" t.index ["team_id"], name: "index_user_teams_on_team_id" - t.index ["user_id", "team_id"], name: "index_user_teams_on_user_id_and_team_id", unique: true t.index ["user_id"], name: "index_user_teams_on_user_id" end From 73dd6678eec14675031dea7ecba9543a94a4a3b2 Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Tue, 2 Apr 2019 10:36:11 +0200 Subject: [PATCH 4/4] Fix for show date value --- app/helpers/global_activities_helper.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/app/helpers/global_activities_helper.rb b/app/helpers/global_activities_helper.rb index 411b6b4fb..8512caefa 100644 --- a/app/helpers/global_activities_helper.rb +++ b/app/helpers/global_activities_helper.rb @@ -64,13 +64,7 @@ module GlobalActivitiesHelper path = obj.archived? ? archive_my_module_path(obj.my_module) : results_my_module_path(obj.my_module) when Step - if obj.protocol.in_repository? - path = protocols_path - elsif obj.protocol.my_module.navigable? - path = protocols_my_module_path(obj.my_module) - else - return current_value - end + return current_value when Report path = reports_path else @@ -83,8 +77,12 @@ module GlobalActivitiesHelper obj = message_item[:type].constantize.find_by_id(message_item[:id]) return message_item[:value] unless obj - value = obj.public_send(message_item[:getter] || 'name') + value = obj.public_send(message_item[:value_for] || 'name') value = t('global_activities.index.no_name') if value.blank? + + # format if is datetime format + value = l(value, format: :full_date) if value.methods.include? :strftime + value end end