Skip to content

Instantly share code, notes, and snippets.

@phillipkoebbe
Created January 18, 2011 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillipkoebbe/784363 to your computer and use it in GitHub Desktop.
Save phillipkoebbe/784363 to your computer and use it in GitHub Desktop.
class BaseController < ApplicationController
# put everything in here that is common to all controllers
end
class User::BaseController < BaseController
before_filter :require_logged_in
private
def require_logged_in
redirect_to(login_path) and return false unless logged_in?
return true
end
end
class User::ProductsController < User::BaseController
end
class Admin::BaseController < BaseController
before_filter :require_admin
private
def require_admin
redirect_to(home_path) and return false unless logged_in?
redirect_to(user_home_path) and return false unless current_user.is_administrator?
return true
end
end
class Admin::ProductsController < Admin::BaseController
end
# spec for base controller
class Admin::BogusController < Admin::BaseController
def index
render :nothing => true
end
end
ActionController::Routing::Routes.draw do |map|
map.namespace :admin do |admin|
admin.resources :bogus
end
end
describe Admin::BogusController do
it 'should deny access to non-administrative users' do
login_as(:user)
get :index
response.should be_redirect
end
it 'should allow access to administrative users' do
login_as(:admin)
get :index
response.should be_success
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment