adding controller additions with basic behavior.
This commit is contained in:
		
							parent
							
								
									c663effc06
								
							
						
					
					
						commit
						44b36ce2fc
					
				@ -1,3 +1,8 @@
 | 
				
			|||||||
$:.unshift(File.dirname(__FILE__))
 | 
					$:.unshift(File.dirname(__FILE__))
 | 
				
			||||||
require 'cancan/ability'
 | 
					 | 
				
			||||||
require 'cancan/instance_exec'
 | 
					require 'cancan/instance_exec'
 | 
				
			||||||
 | 
					require 'cancan/ability'
 | 
				
			||||||
 | 
					require 'cancan/controller_additions'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module CanCan
 | 
				
			||||||
 | 
					  class AccessDenied < StandardError; end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
							
								
								
									
										23
									
								
								lib/cancan/controller_additions.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								lib/cancan/controller_additions.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,23 @@
 | 
				
			|||||||
 | 
					module CanCan
 | 
				
			||||||
 | 
					  module ControllerAdditions
 | 
				
			||||||
 | 
					    def self.included(base)
 | 
				
			||||||
 | 
					      base.helper_method :can?
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    def unauthorized!
 | 
				
			||||||
 | 
					      raise AccessDenied, "You are unable to access this page."
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    def current_ability
 | 
				
			||||||
 | 
					      ::Ability.for_user(current_user)
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    def can?(*args)
 | 
				
			||||||
 | 
					      (@current_ability ||= current_ability).can?(*args)
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class ActionController::Base
 | 
				
			||||||
 | 
					  include CanCan::ControllerAdditions
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
							
								
								
									
										32
									
								
								spec/cancan/controller_additions_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								spec/cancan/controller_additions_spec.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,32 @@
 | 
				
			|||||||
 | 
					require File.dirname(__FILE__) + '/../spec_helper'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Ability
 | 
				
			||||||
 | 
					  include CanCan::Ability
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					describe CanCan::ControllerAdditions do
 | 
				
			||||||
 | 
					  before(:each) do
 | 
				
			||||||
 | 
					    @controller_class = Class.new
 | 
				
			||||||
 | 
					    @controller = @controller_class.new
 | 
				
			||||||
 | 
					    mock(@controller_class).helper_method(:can?)
 | 
				
			||||||
 | 
					    @controller_class.send(:include, CanCan::ControllerAdditions)
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  it "should read from the cache with request uri as key and render that text" do
 | 
				
			||||||
 | 
					    lambda {
 | 
				
			||||||
 | 
					      @controller.unauthorized!
 | 
				
			||||||
 | 
					    }.should raise_error(CanCan::AccessDenied)
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  it "should have a current_ability method which generates an ability for the current user" do
 | 
				
			||||||
 | 
					    stub(@controller).current_user { :current_user }
 | 
				
			||||||
 | 
					    @controller.current_ability.should be_kind_of(Ability)
 | 
				
			||||||
 | 
					    @controller.current_ability.user.should == :current_user
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  it "should provide a can? method which goes through the current ability" do
 | 
				
			||||||
 | 
					    stub(@controller).current_user { :current_user }
 | 
				
			||||||
 | 
					    @controller.current_ability.should be_kind_of(Ability)
 | 
				
			||||||
 | 
					    @controller.can?(:foo, :bar).should be_false
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user