diff --git a/app/controllers/admin/police_districts_controller.rb b/app/controllers/admin/police_districts_controller.rb index 0088700..ecff7be 100644 --- a/app/controllers/admin/police_districts_controller.rb +++ b/app/controllers/admin/police_districts_controller.rb @@ -1,6 +1,8 @@ class Admin::PoliceDistrictsController < Admin::ApplicationController include GoogleCalendarable + before_action :set_district, except: [:new, :create, :index] + def new @district = PoliceDistrict.new @@ -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 @@ -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, diff --git a/spec/controllers/admin/police_districts_controller_spec.rb b/spec/controllers/admin/police_districts_controller_spec.rb new file mode 100644 index 0000000..73f1b08 --- /dev/null +++ b/spec/controllers/admin/police_districts_controller_spec.rb @@ -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