Skip to content

Instantly share code, notes, and snippets.

@zdennis
Created October 31, 2008 17:44
Show Gist options
  • Save zdennis/21365 to your computer and use it in GitHub Desktop.
Save zdennis/21365 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
has_many :roles
# can answer... has_role?, as(...), etc
include Roles::RoleMethods
end
class ApplicationController
rescue_from Roles::RoleNotFound, AccessDenied do
# send to 401.html, send back some error text/json, whatever...
end
end
class InvoicesController < ApplicationController
before_filter :login_required
def show
@invoice = current_user.as('fiscal staff').invoices.find params[:id]
end
def create
@invoice = current_user.as('fiscal admin').create_invoice params[:invoice]
end
def index
@invoices = current_user.as('fiscal staff').invoices.all
end
def update
@invoice = current_user.as('fiscal admin').invoices.find params[:id]
end
end
class FiscalAdmin < Roles::Base
alias :user, :source
def invoices
Invoice.owned_by(user)
end
def create_invoice attrs
Invoice.create! attrs
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment