Skip to content

Commit d68f9e8

Browse files
author
Ryan Bigg
committed
Section 7.3.2: Add admin namespaced users controller
1 parent 3f24cb0 commit d68f9e8

File tree

9 files changed

+50
-0
lines changed

9 files changed

+50
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://coffeescript.org/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the admin/users controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Admin::BaseController < ApplicationController
2+
before_action :authorize_admin!
3+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Admin::UsersController < Admin::BaseController
2+
def index
3+
end
4+
end

ticketee/app/controllers/application_controller.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,15 @@ class ApplicationController < ActionController::Base
22
# Prevent CSRF attacks by raising an exception.
33
# For APIs, you may want to use :null_session instead.
44
protect_from_forgery with: :exception
5+
6+
private
7+
8+
def authorize_admin!
9+
authenticate_user!
10+
11+
unless current_user.admin?
12+
flash[:alert] = "You must be an admin to do that."
13+
redirect_to root_path
14+
end
15+
end
516
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module Admin::UsersHelper
2+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Admin::Users#index</h1>
2+
<p>Find me in app/views/admin/users/index.html.erb</p>

ticketee/config/routes.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@
55
resources :projects do
66
resources :tickets
77
end
8+
9+
namespace :admin do
10+
resources :users
11+
end
812
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require "rails_helper"
2+
3+
RSpec.describe Admin::UsersController, :type => :controller do
4+
let(:user) { FactoryGirl.create(:user) }
5+
6+
context "standard users" do
7+
before do
8+
allow(controller).to receive(:authenticate_user!)
9+
allow(controller).to receive(:current_user).and_return(user)
10+
end
11+
12+
it "are not able to access the index action" do
13+
get 'index'
14+
expect(response).to redirect_to('/')
15+
expect(flash[:alert]).to eql("You must be an admin to do that.")
16+
end
17+
end
18+
end

0 commit comments

Comments
 (0)