diff --git a/app/controllers/moderation/users_controller.rb b/app/controllers/moderation/users_controller.rb index 2c958eda8..b53dbd722 100644 --- a/app/controllers/moderation/users_controller.rb +++ b/app/controllers/moderation/users_controller.rb @@ -6,16 +6,10 @@ class Moderation::UsersController < Moderation::BaseController def index end - def hide_in_moderation_screen - block_user - - redirect_with_query_params_to({ action: :index }, { notice: I18n.t("moderation.users.notice_hide") }) - end - def hide block_user - redirect_to debates_path + redirect_with_query_params_to index_path_options, { notice: I18n.t("moderation.users.notice_hide") } end private @@ -28,4 +22,17 @@ class Moderation::UsersController < Moderation::BaseController @user.block Activity.log(current_user, :block, @user) end + + def index_path_options + if request.referer + referer_params = Rails.application.routes.recognize_path(request.referer) + + referer_params.except(:id).merge({ + controller: "/#{referer_params[:controller]}", + action: :index + }) + else + { action: :index } + end + end end diff --git a/app/models/ability.rb b/app/models/ability.rb index afe6f416e..45207eaae 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -2,10 +2,6 @@ class Ability include CanCan::Ability def initialize(user) - # If someone can hide something, he can also hide it - # from the moderation screen - alias_action :hide_in_moderation_screen, to: :hide - if user # logged-in users merge Abilities::Valuator.new(user) if user.valuator? diff --git a/app/views/moderation/users/index.html.erb b/app/views/moderation/users/index.html.erb index e1492c7bb..9068d0505 100644 --- a/app/views/moderation/users/index.html.erb +++ b/app/views/moderation/users/index.html.erb @@ -23,7 +23,7 @@ <% else %> <%= render Admin::TableActionsComponent.new(user, actions: []) do |actions| %> <%= actions.action( - :hide_in_moderation_screen, + :hide, text: t("moderation.users.index.hide"), method: :put, class: "button hollow alert" diff --git a/config/routes/moderation.rb b/config/routes/moderation.rb index 83984badd..e28f28336 100644 --- a/config/routes/moderation.rb +++ b/config/routes/moderation.rb @@ -4,7 +4,6 @@ namespace :moderation do resources :users, only: :index do member do put :hide - put :hide_in_moderation_screen end end diff --git a/spec/controllers/moderation/users_controller_spec.rb b/spec/controllers/moderation/users_controller_spec.rb index 1699c9c4e..f4818f0eb 100644 --- a/spec/controllers/moderation/users_controller_spec.rb +++ b/spec/controllers/moderation/users_controller_spec.rb @@ -2,14 +2,32 @@ require "rails_helper" describe Moderation::UsersController do before { sign_in create(:moderator).user } + let(:user) { create(:user, email: "user@consul.dev") } - describe "PUT hide_in_moderation_screen" do + describe "PUT hide" do it "keeps query parameters while using protected redirects" do - user = create(:user, email: "user@consul.dev") - - get :hide_in_moderation_screen, params: { id: user, search: "user@consul.dev", host: "evil.dev" } + get :hide, params: { id: user, search: "user@consul.dev", host: "evil.dev" } expect(response).to redirect_to "/moderation/users?search=user%40consul.dev" end + + it "redirects to the index of the section where it was called with a notice" do + proposal = create(:proposal, author: user) + request.env["HTTP_REFERER"] = proposal_path(proposal) + + put :hide, params: { id: user } + + expect(response).to redirect_to proposals_path + expect(flash[:notice]).to eq "User blocked. All of this user's debates and comments have been hidden." + end + + it "redirects to the index with a nested resource" do + investment = create(:budget_investment, author: user) + request.env["HTTP_REFERER"] = budget_investment_path(investment.budget, investment) + + put :hide, params: { id: user } + + expect(response).to redirect_to budget_investments_path(investment.budget) + end end end diff --git a/spec/models/abilities/moderator_spec.rb b/spec/models/abilities/moderator_spec.rb index 77bf1ae36..9f3cb14dc 100644 --- a/spec/models/abilities/moderator_spec.rb +++ b/spec/models/abilities/moderator_spec.rb @@ -52,7 +52,6 @@ describe Abilities::Moderator do let(:ignored_proposal) { create(:proposal, :with_ignored_flag) } it { should be_able_to(:hide, comment) } - it { should be_able_to(:hide_in_moderation_screen, comment) } it { should_not be_able_to(:hide, hidden_comment) } it { should be_able_to(:hide, own_comment) } @@ -60,12 +59,10 @@ describe Abilities::Moderator do it { should_not be_able_to(:moderate, own_comment) } it { should be_able_to(:hide, debate) } - it { should be_able_to(:hide_in_moderation_screen, debate) } it { should_not be_able_to(:hide, hidden_debate) } it { should_not be_able_to(:hide, own_debate) } it { should be_able_to(:hide, proposal) } - it { should be_able_to(:hide_in_moderation_screen, proposal) } it { should be_able_to(:hide, own_proposal) } it { should_not be_able_to(:hide, hidden_proposal) } diff --git a/spec/system/admin/activity_spec.rb b/spec/system/admin/activity_spec.rb index a8e1bca8f..09236ac67 100644 --- a/spec/system/admin/activity_spec.rb +++ b/spec/system/admin/activity_spec.rb @@ -219,7 +219,7 @@ describe "Admin activity" do within("#proposal_#{proposal.id}") do accept_confirm("Are you sure? Hide author \"#{proposal.author.name}\"") { click_link "Hide author" } - expect(page).to have_current_path(debates_path) + expect(page).to have_current_path(proposals_path) end visit admin_activity_path diff --git a/spec/system/admin/hidden_comments_spec.rb b/spec/system/admin/hidden_comments_spec.rb index 2ac07cfa9..407207fa9 100644 --- a/spec/system/admin/hidden_comments_spec.rb +++ b/spec/system/admin/hidden_comments_spec.rb @@ -16,7 +16,7 @@ describe "Admin hidden comments", :admin do accept_confirm("Are you sure? Hide author \"#{proposal.author.name}\"") { click_link "Hide author" } end - expect(page).to have_current_path debates_path + expect(page).to have_current_path proposals_path visit admin_hidden_comments_path diff --git a/spec/system/moderation/budget_investments_spec.rb b/spec/system/moderation/budget_investments_spec.rb index 6960fc1fc..9cfaf7849 100644 --- a/spec/system/moderation/budget_investments_spec.rb +++ b/spec/system/moderation/budget_investments_spec.rb @@ -25,10 +25,7 @@ describe "Moderate budget investments" do accept_confirm("Are you sure? Hide author \"#{investment.author.name}\"") { click_link "Hide author" } - expect(page).to have_current_path(debates_path) - - visit budget_investments_path(budget.id, heading_id: heading.id) - + expect(page).to have_current_path(budget_investments_path(budget)) expect(page).not_to have_content(investment.title) end