Created
November 9, 2008 08:30
-
-
Save ryan-allen/23222 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The point of this prototype is to launch the rails app, and then hit it with urls with a | |
bunch of parameters to see the library in action: | |
* http://localhost:3000/roles/index (200) | |
* http://localhost:3000/roles/reviewer_stuff (404) | |
* http://localhost:3000/roles/reviewer_stuff?reviewer=1 (200) | |
* http://localhost:3000/roles/reviewer_stuff?admin=1 (200) | |
* http://localhost:3000/roles/admin_stuff (404) | |
* http://localhost:3000/roles/admin_stuff?reviewer=1 (404) | |
* http://localhost:3000/roles/admin_stuff?admin=1 (200) | |
* http://localhost:3000/roles/look_at_number?n=1 (404) | |
* http://localhost:3000/roles/look_at_number?n=2 (200) | |
Unfortunatley Rails' dependencies reloading junk in the development environment clobbers | |
the User class on each request, therefore losing the can/can?/cannot/cannot? methods that | |
were dynamically added. I'm trying to figure out how to force a reload of | |
config/initializers/superheroes.rb on each request, and that'd solve the problem. Damn I | |
hate dependencies.rb with a passion... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in app/controllers | |
class RolesController < ApplicationController | |
# filters | |
before_filter :setup_user, :except => :index | |
def setup_user | |
@user = User.new | |
@user.reviewer = params[:reviewer] | |
@user.admin = params[:admin] | |
end | |
before_filter :setup_number, :only => :look_at_number | |
def setup_number | |
@number = params[:n].to_i | |
end | |
# checks | |
check(:user).can.view_reviewer_actions?(:only => :reviewer_stuff) | |
check(:user).can.view_admin_actions?(:only => :admin_stuff) | |
check(:user).can.look_at_number?(:number, :only => :look_at_number) | |
# actions | |
def index | |
render :text => params[:action] | |
end | |
def assigns_check | |
render :text => instance_variable_get("@user").inspect | |
end | |
def admin_stuff | |
render :text => params[:action] | |
end | |
def reviewer_stuff | |
render :text => params[:action] | |
end | |
def look_at_number | |
render :text => params[:n] | |
end | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in config/initializers | |
SuperHeroes.pretending_to_be_a User do | |
special_ability :view_admin_actions do | |
admin | |
end | |
special_ability :view_reviewer_actions do | |
reviewer or admin | |
end | |
special_ability :look_at_number do |n| | |
n % 2 == 0 # only even numbers allowed | |
end | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in app/models | |
class User | |
attr_accessor :reviewer, :admin | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment