From eac90109f7ed7e06b750ecf72c5defccca6e614a Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Fri, 26 Apr 2019 10:47:08 +0200 Subject: [PATCH 1/2] Change ownership of inventory items, when a user leaves a team --- app/models/user_team.rb | 9 +++++++++ .../user_teams/_destroy_user_team_modal_body.html.erb | 1 + .../user_teams/_leave_user_team_modal_body.html.erb | 1 + config/locales/en.yml | 6 ++++-- 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/models/user_team.rb b/app/models/user_team.rb index 77a1c4320..f6d703f48 100644 --- a/app/models/user_team.rb +++ b/app/models/user_team.rb @@ -77,6 +77,15 @@ class UserTeam < ApplicationRecord protocol.save end + # Make new owner author of all inventory items that were added + # by departing user and belong to this team. + repository_rows = + RepositoryRow.joins(:repository) + .where('repositories.team_id = ? and repository_rows.created_by_id = ?', team, user) + repository_rows.find_each do |repository_row| + repository_row.update(created_by: new_owner) + end + super() end diff --git a/app/views/users/settings/user_teams/_destroy_user_team_modal_body.html.erb b/app/views/users/settings/user_teams/_destroy_user_team_modal_body.html.erb index 7f5a68711..b77e2f226 100644 --- a/app/views/users/settings/user_teams/_destroy_user_team_modal_body.html.erb +++ b/app/views/users/settings/user_teams/_destroy_user_team_modal_body.html.erb @@ -15,6 +15,7 @@
  • <%= t("users.settings.user_teams.destroy_uo_alert_line_1") %>
  • <%= t("users.settings.user_teams.destroy_uo_alert_line_2").html_safe %>
  • <%= t("users.settings.user_teams.destroy_uo_alert_line_3") %>
  • +
  • <%= t("users.settings.user_teams.destroy_uo_alert_line_4") %>
  • <% end %> diff --git a/app/views/users/settings/user_teams/_leave_user_team_modal_body.html.erb b/app/views/users/settings/user_teams/_leave_user_team_modal_body.html.erb index 9e1c023c9..3701a7aa3 100644 --- a/app/views/users/settings/user_teams/_leave_user_team_modal_body.html.erb +++ b/app/views/users/settings/user_teams/_leave_user_team_modal_body.html.erb @@ -13,6 +13,7 @@
  • <%= t("users.settings.user_teams.leave_uo_alert_line_1") %>
  • <%= t("users.settings.user_teams.leave_uo_alert_line_2").html_safe %>
  • <%= t("users.settings.user_teams.leave_uo_alert_line_3")%>
  • +
  • <%= t("users.settings.user_teams.leave_uo_alert_line_4")%>
  • <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 86db7e9b3..081fceff9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1497,14 +1497,16 @@ en: leave_uo_alert_heading: "Leaving team has following consequences:" leave_uo_alert_line_1: "you will lose access to all content belonging to the team (including projects, tasks, protocols and activities);" leave_uo_alert_line_2: "all projects in the team where you were the sole Owner will receive a new owner from the team administrators;" - leave_uo_alert_line_3: "all repository protocols in the team belonging to you will be reassigned onto a new owner from team administrators." + leave_uo_alert_line_3: "all repository protocols in the team belonging to you will be reassigned onto a new owner from team administrators;" + leave_uo_alert_line_4: "all inventory items in the team added by you will be reassigned onto a new owner from team administrators." leave_uo_confirm: "Leave" destroy_uo_heading: "Remove user %{user} from team %{team}" destroy_uo_message: "Are you sure you wish to remove user %{user} from team %{team}?" destroy_uo_alert_heading: "Removing user from team has following consequences:" destroy_uo_alert_line_1: "user will lose access to all content belonging to the team (including projects, tasks, protocols and activities);" destroy_uo_alert_line_2: "all projects in the team where user was the sole Owner will be reassigned onto you as a new owner;" - destroy_uo_alert_line_3: "all repository protocols in the team belonging to user will be reassigned onto you." + destroy_uo_alert_line_3: "all repository protocols in the team belonging to user will be reassigned onto you;" + destroy_uo_alert_line_4: "all inventory items in the team added by user will be reassigned onto you." destroy_uo_confirm: "Remove" leave_flash: "Successfuly left team %{team}." From 5fd4c47388553a34c54d552bbaf550ff51154c6a Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Mon, 29 Apr 2019 14:02:02 +0200 Subject: [PATCH 2/2] Move change owner functionality to RepositoryRow --- app/models/repository_row.rb | 6 ++++++ app/models/user_team.rb | 7 +------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/models/repository_row.rb b/app/models/repository_row.rb index 4daa0aae3..783c48ca7 100644 --- a/app/models/repository_row.rb +++ b/app/models/repository_row.rb @@ -30,4 +30,10 @@ class RepositoryRow < ApplicationRecord def self.name_like(query) where('repository_rows.name ILIKE ?', "%#{query}%") end + + def self.change_owner(team, user, new_owner) + joins(:repository) + .where('repositories.team_id = ? and repository_rows.created_by_id = ?', team, user) + .update_all(created_by_id: new_owner.id) + end end diff --git a/app/models/user_team.rb b/app/models/user_team.rb index f6d703f48..567b4d594 100644 --- a/app/models/user_team.rb +++ b/app/models/user_team.rb @@ -79,12 +79,7 @@ class UserTeam < ApplicationRecord # Make new owner author of all inventory items that were added # by departing user and belong to this team. - repository_rows = - RepositoryRow.joins(:repository) - .where('repositories.team_id = ? and repository_rows.created_by_id = ?', team, user) - repository_rows.find_each do |repository_row| - repository_row.update(created_by: new_owner) - end + RepositoryRow.change_owner(team, user, new_owner) super() end