Admin accounts controller cleanup (#1664)
* Remove unused account_params method in admin/accounts controller * Introduce AccountFilter to find accounts * Use AccountFilter in admin/accounts controller * Use more restful routes admin silence and suspension area * Add admin/silences and admin/suspensions controllersmaster
parent
0e39cc6a35
commit
3a9eb81a80
@ -0,0 +1,23 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
module Admin |
||||
class SilencesController < BaseController |
||||
before_action :set_account |
||||
|
||||
def create |
||||
@account.update(silenced: true) |
||||
redirect_to admin_accounts_path |
||||
end |
||||
|
||||
def destroy |
||||
@account.update(silenced: false) |
||||
redirect_to admin_accounts_path |
||||
end |
||||
|
||||
private |
||||
|
||||
def set_account |
||||
@account = Account.find(params[:account_id]) |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,23 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
module Admin |
||||
class SuspensionsController < BaseController |
||||
before_action :set_account |
||||
|
||||
def create |
||||
Admin::SuspensionWorker.perform_async(@account.id) |
||||
redirect_to admin_accounts_path |
||||
end |
||||
|
||||
def destroy |
||||
@account.update(suspended: false) |
||||
redirect_to admin_accounts_path |
||||
end |
||||
|
||||
private |
||||
|
||||
def set_account |
||||
@account = Account.find(params[:account_id]) |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,36 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
class AccountFilter |
||||
attr_reader :params |
||||
|
||||
def initialize(params) |
||||
@params = params |
||||
end |
||||
|
||||
def results |
||||
scope = Account.alphabetic |
||||
params.each do |key, value| |
||||
scope = scope.merge scope_for(key, value) |
||||
end |
||||
scope |
||||
end |
||||
|
||||
def scope_for(key, value) |
||||
case key |
||||
when /local/ |
||||
Account.local |
||||
when /remote/ |
||||
Account.remote |
||||
when /by_domain/ |
||||
Account.where(domain: value) |
||||
when /silenced/ |
||||
Account.silenced |
||||
when /recent/ |
||||
Account.recent |
||||
when /suspended/ |
||||
Account.suspended |
||||
else |
||||
raise "Unknown filter: #{key}" |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,24 @@ |
||||
require 'rails_helper' |
||||
|
||||
describe Admin::SilencesController do |
||||
let(:account) { Fabricate(:account) } |
||||
before do |
||||
sign_in Fabricate(:user, admin: true), scope: :user |
||||
end |
||||
|
||||
describe 'POST #create' do |
||||
it 'redirects to admin accounts page' do |
||||
post :create, params: { account_id: account.id } |
||||
|
||||
expect(response).to redirect_to(admin_accounts_path) |
||||
end |
||||
end |
||||
|
||||
describe 'DELETE #destroy' do |
||||
it 'redirects to admin accounts page' do |
||||
delete :destroy, params: { account_id: account.id } |
||||
|
||||
expect(response).to redirect_to(admin_accounts_path) |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,24 @@ |
||||
require 'rails_helper' |
||||
|
||||
describe Admin::SuspensionsController do |
||||
let(:account) { Fabricate(:account) } |
||||
before do |
||||
sign_in Fabricate(:user, admin: true), scope: :user |
||||
end |
||||
|
||||
describe 'POST #create' do |
||||
it 'redirects to admin accounts page' do |
||||
post :create, params: { account_id: account.id } |
||||
|
||||
expect(response).to redirect_to(admin_accounts_path) |
||||
end |
||||
end |
||||
|
||||
describe 'DELETE #destroy' do |
||||
it 'redirects to admin accounts page' do |
||||
delete :destroy, params: { account_id: account.id } |
||||
|
||||
expect(response).to redirect_to(admin_accounts_path) |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,31 @@ |
||||
require 'rails_helper' |
||||
|
||||
describe AccountFilter do |
||||
describe 'with empty params' do |
||||
it 'defaults to alphabetic account list' do |
||||
filter = AccountFilter.new({}) |
||||
|
||||
expect(filter.results).to eq Account.alphabetic |
||||
end |
||||
end |
||||
|
||||
describe 'with invalid params' do |
||||
it 'raises with key error' do |
||||
filter = AccountFilter.new(wrong: true) |
||||
|
||||
expect { filter.results }.to raise_error(/wrong/) |
||||
end |
||||
end |
||||
|
||||
describe 'with valid params' do |
||||
it 'combines filters on Account' do |
||||
filter = AccountFilter.new(by_domain: 'test.com', silenced: true) |
||||
|
||||
allow(Account).to receive(:where).and_return(Account.none) |
||||
allow(Account).to receive(:silenced).and_return(Account.none) |
||||
filter.results |
||||
expect(Account).to have_received(:where).with(domain: 'test.com') |
||||
expect(Account).to have_received(:silenced) |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue