Admin base controller (#1465)

* Add Admin::BaseController to wrap admin area

Extracts the setting of the `admin` layout and verifying that users are admins
to a common base class for the admin/ controllers.

* Add basic coverage for admin/reports and admin/settings controllers
This commit is contained in:
Matt Jankowski 2017-04-10 15:27:03 -04:00 committed by Eugen
parent 1be6aa0c7f
commit dbe9f33fdc
8 changed files with 154 additions and 125 deletions

View File

@ -1,11 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
class Admin::AccountsController < ApplicationController module Admin
before_action :require_admin! class AccountsController < BaseController
before_action :set_account, except: :index before_action :set_account, except: :index
layout 'admin'
def index def index
@accounts = Account.alphabetic.paginate(page: params[:page], per_page: 40) @accounts = Account.alphabetic.paginate(page: params[:page], per_page: 40)
@ -49,3 +47,4 @@ class Admin::AccountsController < ApplicationController
params.require(:account).permit(:silenced, :suspended) params.require(:account).permit(:silenced, :suspended)
end end
end end
end

View File

@ -0,0 +1,9 @@
# frozen_string_literal: true
module Admin
class BaseController < ApplicationController
before_action :require_admin!
layout 'admin'
end
end

View File

@ -1,10 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
class Admin::DomainBlocksController < ApplicationController module Admin
before_action :require_admin! class DomainBlocksController < BaseController
layout 'admin'
def index def index
@blocks = DomainBlock.paginate(page: params[:page], per_page: 40) @blocks = DomainBlock.paginate(page: params[:page], per_page: 40)
end end
@ -30,3 +27,4 @@ class Admin::DomainBlocksController < ApplicationController
params.require(:domain_block).permit(:domain, :severity) params.require(:domain_block).permit(:domain, :severity)
end end
end end
end

View File

@ -1,11 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
class Admin::PubsubhubbubController < ApplicationController module Admin
before_action :require_admin! class PubsubhubbubController < BaseController
layout 'admin'
def index def index
@subscriptions = Subscription.order('id desc').includes(:account).paginate(page: params[:page], per_page: 40) @subscriptions = Subscription.order('id desc').includes(:account).paginate(page: params[:page], per_page: 40)
end end
end end
end

View File

@ -1,11 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
class Admin::ReportsController < ApplicationController module Admin
before_action :require_admin! class ReportsController < BaseController
before_action :set_report, except: [:index] before_action :set_report, except: [:index]
layout 'admin'
def index def index
@reports = Report.includes(:account, :target_account).order('id desc').paginate(page: params[:page], per_page: 40) @reports = Report.includes(:account, :target_account).order('id desc').paginate(page: params[:page], per_page: 40)
@reports = params[:action_taken].present? ? @reports.resolved : @reports.unresolved @reports = params[:action_taken].present? ? @reports.resolved : @reports.unresolved
@ -43,3 +41,4 @@ class Admin::ReportsController < ApplicationController
@report = Report.find(params[:id]) @report = Report.find(params[:id])
end end
end end
end

View File

@ -1,10 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
class Admin::SettingsController < ApplicationController module Admin
before_action :require_admin! class SettingsController < BaseController
layout 'admin'
def index def index
@settings = Setting.all_as_records @settings = Setting.all_as_records
end end
@ -33,3 +30,4 @@ class Admin::SettingsController < ApplicationController
params.require(:setting).permit(:value) params.require(:setting).permit(:value)
end end
end end
end

View File

@ -0,0 +1,14 @@
require 'rails_helper'
RSpec.describe Admin::ReportsController, type: :controller do
describe 'GET #index' do
before do
sign_in Fabricate(:user, admin: true), scope: :user
end
it 'returns http success' do
get :index
expect(response).to have_http_status(:success)
end
end
end

View File

@ -0,0 +1,14 @@
require 'rails_helper'
RSpec.describe Admin::SettingsController, type: :controller do
describe 'GET #index' do
before do
sign_in Fabricate(:user, admin: true), scope: :user
end
it 'returns http success' do
get :index
expect(response).to have_http_status(:success)
end
end
end