Restore all related content when a user is restored

This commit is contained in:
Carlos Iniesta
2020-10-25 18:48:11 +01:00
committed by Javi Martín
parent 712d33ef99
commit 67d61699b6
4 changed files with 61 additions and 2 deletions

View File

@@ -19,7 +19,7 @@ class Admin::HiddenUsersController < Admin::BaseController
end end
def restore def restore
@user.restore @user.full_restore
Activity.log(current_user, :restore, @user) Activity.log(current_user, :restore, @user)
redirect_with_query_params_to(action: :index) redirect_with_query_params_to(action: :index)
end end

View File

@@ -250,6 +250,20 @@ class User < ApplicationRecord
ProposalNotification.hide_all ProposalNotification.where(author_id: id).pluck(:id) ProposalNotification.hide_all ProposalNotification.where(author_id: id).pluck(:id)
end end
def full_restore
ActiveRecord::Base.transaction do
Debate.restore_all debates.where("hidden_at >= ?", hidden_at)
Comment.restore_all comments.where("hidden_at >= ?", hidden_at)
Proposal.restore_all proposals.where("hidden_at >= ?", hidden_at)
Budget::Investment.restore_all budget_investments.where("hidden_at >= ?", hidden_at)
ProposalNotification.restore_all(
ProposalNotification.only_hidden.where("hidden_at >= ?", hidden_at).where(author_id: id)
)
restore
end
end
def erase(erase_reason = nil) def erase(erase_reason = nil)
update!( update!(
erased_at: Time.current, erased_at: Time.current,

View File

@@ -106,7 +106,7 @@ FactoryBot.define do
end end
trait :hidden do trait :hidden do
hidden_at { Date.current } hidden_at { Time.current }
end end
trait :with_confirmed_hide do trait :with_confirmed_hide do

View File

@@ -721,4 +721,49 @@ describe User do
expect(User.find_by_manager_login("admin_user_#{user.id}")).to eq user expect(User.find_by_manager_login("admin_user_#{user.id}")).to eq user
end end
end end
describe "#full_restore" do
it "restore all previous hidden user content" do
user = create(:user, :hidden)
other_user = create(:user, :hidden)
comment = create(:comment, :hidden, author: user)
debate = create(:debate, :hidden, author: user)
investment = create(:budget_investment, :hidden, author: user)
proposal = create(:proposal, :hidden, author: user)
proposal_notification = create(:proposal_notification, :hidden, proposal: proposal)
old_hidden_comment = create(:comment, hidden_at: 3.days.ago, author: user)
old_hidden_debate = create(:debate, hidden_at: 3.days.ago, author: user)
old_hidden_investment = create(:budget_investment, hidden_at: 3.days.ago, author: user)
old_hidden_proposal = create(:proposal, hidden_at: 3.days.ago, author: user)
old_hidden_proposal_notification = create(:proposal_notification, hidden_at: 3.days.ago, proposal: proposal)
other_user_comment = create(:comment, :hidden, author: other_user)
other_user_debate = create(:debate, :hidden, author: other_user)
other_user_proposal = create(:proposal, :hidden, author: other_user)
other_user_investment = create(:budget_investment, :hidden, author: other_user)
other_user_proposal_notification = create(:proposal_notification, :hidden, proposal: other_user_proposal)
user.full_restore
expect(debate.reload).not_to be_hidden
expect(comment.reload).not_to be_hidden
expect(investment.reload).not_to be_hidden
expect(proposal.reload).not_to be_hidden
expect(proposal_notification.reload).not_to be_hidden
expect(old_hidden_comment.reload).to be_hidden
expect(old_hidden_debate.reload).to be_hidden
expect(old_hidden_investment.reload).to be_hidden
expect(old_hidden_proposal.reload).to be_hidden
expect(old_hidden_proposal_notification.reload).to be_hidden
expect(other_user_comment.reload).to be_hidden
expect(other_user_debate.reload).to be_hidden
expect(other_user_investment.reload).to be_hidden
expect(other_user_proposal.reload).to be_hidden
expect(other_user_proposal_notification.reload).to be_hidden
end
end
end end