Merge pull request #4465 from consul/restore_user_content

Restore all related content along when a user is restored
This commit is contained in:
Javi Martín
2021-04-13 13:48:38 +02:00
committed by GitHub
4 changed files with 65 additions and 12 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

@@ -241,19 +241,27 @@ class User < ApplicationRecord
end end
def block def block
debates_ids = Debate.where(author_id: id).pluck(:id)
comments_ids = Comment.where(user_id: id).pluck(:id)
proposal_ids = Proposal.where(author_id: id).pluck(:id)
investment_ids = Budget::Investment.where(author_id: id).pluck(:id)
proposal_notification_ids = ProposalNotification.where(author_id: id).pluck(:id)
hide hide
Debate.hide_all debates_ids Debate.hide_all debate_ids
Comment.hide_all comments_ids Comment.hide_all comment_ids
Proposal.hide_all proposal_ids Proposal.hide_all proposal_ids
Budget::Investment.hide_all investment_ids Budget::Investment.hide_all budget_investment_ids
ProposalNotification.hide_all proposal_notification_ids ProposalNotification.hide_all ProposalNotification.where(author_id: id).pluck(:id)
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 end
def erase(erase_reason = nil) def erase(erase_reason = nil)

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