Skip to content
This repository has been archived by the owner on Jul 13, 2022. It is now read-only.

Commit

Permalink
Require district in district admin
Browse files Browse the repository at this point in the history
  • Loading branch information
hartsick committed Jul 1, 2020
1 parent 5f73f28 commit d7512f1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
11 changes: 7 additions & 4 deletions app/controllers/admin/police_districts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Admin::PoliceDistrictsController < Admin::ApplicationController
include GoogleCalendarable

before_action :set_district, except: [:new, :create, :index]

def new
@district = PoliceDistrict.new

Expand All @@ -19,17 +21,14 @@ def create
end

def show
@district = PoliceDistrict.find_by_slug(params[:id])
render :show
end

def edit
@district = PoliceDistrict.find_by_slug(params[:id])

render :edit
end

def update
@district = PoliceDistrict.find_by_slug(params[:id])
if @district.update(district_params)
redirect_to admin_police_districts_path
else
Expand All @@ -43,6 +42,10 @@ def index

private

def set_district
@district = PoliceDistrict.find_by_slug!(params[:id])
end

def district_params
params.require(:police_district).permit(
:name,
Expand Down
46 changes: 46 additions & 0 deletions spec/controllers/admin/police_districts_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'rails_helper'

RSpec.describe Admin::PoliceDistrictsController, type: :controller do
let(:user) { FactoryBot.create(:user) }
let!(:district) { FactoryBot.create(:police_district, slug: 'oakland', timezone: 'Pacific Time (US & Canada)') }

context 'when user is signed in' do
before do
sign_in user
end

describe '#update' do
it 'returns 404 if district not present' do
expect do
post :update, params: { id: 'asldkfjaslkfdj' }
end.to raise_exception(ActiveRecord::RecordNotFound)
end
end

describe '#edit' do
render_views

it 'returns 404 if district not present' do
expect do
post :edit, params: { id: 'asldkfjaslkfdj' }
end.to raise_exception(ActiveRecord::RecordNotFound)
end
end

describe '#destroy' do
it 'returns 404 if district not present' do
expect do
post :update, params: { id: 'asldkfjaslkfdj' }
end.to raise_exception(ActiveRecord::RecordNotFound)
end
end

describe '#show' do
it 'returns 404 if district not present' do
expect do
get :show, params: { id: 'asldkfjaslkfdj' }
end.to raise_exception(ActiveRecord::RecordNotFound)
end
end
end
end

0 comments on commit d7512f1

Please sign in to comment.