diff --git a/app/controllers/admin/hidden_users_controller.rb b/app/controllers/admin/hidden_users_controller.rb index fef56a94e..1b5635270 100644 --- a/app/controllers/admin/hidden_users_controller.rb +++ b/app/controllers/admin/hidden_users_controller.rb @@ -19,7 +19,7 @@ class Admin::HiddenUsersController < Admin::BaseController end def restore - @user.restore + @user.full_restore Activity.log(current_user, :restore, @user) redirect_with_query_params_to(action: :index) end diff --git a/app/models/user.rb b/app/models/user.rb index f20db4bea..94afc59e0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -241,19 +241,27 @@ class User < ApplicationRecord end 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 - Debate.hide_all debates_ids - Comment.hide_all comments_ids + Debate.hide_all debate_ids + Comment.hide_all comment_ids Proposal.hide_all proposal_ids - Budget::Investment.hide_all investment_ids - ProposalNotification.hide_all proposal_notification_ids + Budget::Investment.hide_all budget_investment_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 def erase(erase_reason = nil) diff --git a/spec/factories/proposals.rb b/spec/factories/proposals.rb index e4e31278f..7c6df9b46 100644 --- a/spec/factories/proposals.rb +++ b/spec/factories/proposals.rb @@ -106,7 +106,7 @@ FactoryBot.define do end trait :hidden do - hidden_at { Date.current } + hidden_at { Time.current } end trait :with_confirmed_hide do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index f10eefed7..4e2ece4e3 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -721,4 +721,49 @@ describe User do expect(User.find_by_manager_login("admin_user_#{user.id}")).to eq user 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