adding caching to current_ability class method, if you're overriding this be sure to add caching there too
This commit is contained in:
		
							parent
							
								
									37f482e8d5
								
							
						
					
					
						commit
						ef5900c5b1
					
				@ -1,5 +1,7 @@
 | 
				
			|||||||
1.1.0 (not released)
 | 
					1.1.0 (not released)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					* Adding caching to current_ability controller method, if you're overriding this be sure to add caching too.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* Adding "can" method to Active Record for fetching records matching a specific ability
 | 
					* Adding "can" method to Active Record for fetching records matching a specific ability
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* Adding conditions behavior to Ability#can and fetch with Ability#conditions - see issue #53
 | 
					* Adding conditions behavior to Ability#can and fetch with Ability#conditions - see issue #53
 | 
				
			||||||
 | 
				
			|||||||
@ -203,7 +203,8 @@ CanCan makes two assumptions about your application.
 | 
				
			|||||||
You can override these by overriding the "current_ability" method in your ApplicationController.
 | 
					You can override these by overriding the "current_ability" method in your ApplicationController.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def current_ability
 | 
					  def current_ability
 | 
				
			||||||
     UserAbility.new(current_account) # instead of Ability.new(current_user)
 | 
					    # instead of Ability.new(current_user)
 | 
				
			||||||
 | 
					    @current_ability ||= UserAbility.new(current_account)
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
That's it!
 | 
					That's it!
 | 
				
			||||||
 | 
				
			|||||||
@ -142,20 +142,19 @@ module CanCan
 | 
				
			|||||||
      raise AccessDenied, message
 | 
					      raise AccessDenied, message
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    # Creates and returns the current user's ability. You generally do not invoke
 | 
					    # Creates and returns the current user's ability and caches it. If you
 | 
				
			||||||
    # this method directly, instead you can override this method to change its
 | 
					    # want to override how the Ability is defined then this is the place.
 | 
				
			||||||
    # behavior if the Ability class or current_user method are different.
 | 
					    # Just define the method in the controller to change behavior.
 | 
				
			||||||
    # 
 | 
					    # 
 | 
				
			||||||
    #   def current_ability
 | 
					    #   def current_ability
 | 
				
			||||||
    #     UserAbility.new(current_account) # instead of Ability.new(current_user)
 | 
					    #     # instead of Ability.new(current_user)
 | 
				
			||||||
 | 
					    #     @current_ability ||= UserAbility.new(current_account)
 | 
				
			||||||
    #   end
 | 
					    #   end
 | 
				
			||||||
    # 
 | 
					    # 
 | 
				
			||||||
 | 
					    # Notice it is important to cache the ability object so it is not
 | 
				
			||||||
 | 
					    # recreated every time.
 | 
				
			||||||
    def current_ability
 | 
					    def current_ability
 | 
				
			||||||
      ::Ability.new(current_user)
 | 
					      @current_ability ||= ::Ability.new(current_user)
 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    def cached_current_ability
 | 
					 | 
				
			||||||
      @current_ability ||= current_ability
 | 
					 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    # Use in the controller or view to check the user's permission for a given action
 | 
					    # Use in the controller or view to check the user's permission for a given action
 | 
				
			||||||
@ -171,7 +170,7 @@ module CanCan
 | 
				
			|||||||
    # 
 | 
					    # 
 | 
				
			||||||
    # This simply calls "can?" on the current_ability. See Ability#can?.
 | 
					    # This simply calls "can?" on the current_ability. See Ability#can?.
 | 
				
			||||||
    def can?(*args)
 | 
					    def can?(*args)
 | 
				
			||||||
      cached_current_ability.can?(*args)
 | 
					      current_ability.can?(*args)
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    # Convenience method which works the same as "can?" but returns the opposite value.
 | 
					    # Convenience method which works the same as "can?" but returns the opposite value.
 | 
				
			||||||
@ -179,7 +178,7 @@ module CanCan
 | 
				
			|||||||
    #   cannot? :destroy, @project
 | 
					    #   cannot? :destroy, @project
 | 
				
			||||||
    # 
 | 
					    # 
 | 
				
			||||||
    def cannot?(*args)
 | 
					    def cannot?(*args)
 | 
				
			||||||
      cached_current_ability.cannot?(*args)
 | 
					      current_ability.cannot?(*args)
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user