support additional arguments to can? which get passed to the block - closes #48
This commit is contained in:
		
							parent
							
								
									f027b2ebb3
								
							
						
					
					
						commit
						69f7a65914
					
				@ -1,3 +1,8 @@
 | 
				
			|||||||
 | 
					1.1.0 (not released)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					* Support additional arguments to can? which get passed to the block - see issue #48
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
1.0.2 (Dec 30, 2009)
 | 
					1.0.2 (Dec 30, 2009)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* Adding clear_aliased_actions to Ability which removes previously defined actions including defaults - see issue #20
 | 
					* Adding clear_aliased_actions to Ability which removes previously defined actions including defaults - see issue #20
 | 
				
			||||||
 | 
				
			|||||||
@ -40,12 +40,12 @@ module CanCan
 | 
				
			|||||||
    #     assert ability.cannot?(:destroy, Project.new)
 | 
					    #     assert ability.cannot?(:destroy, Project.new)
 | 
				
			||||||
    #   end
 | 
					    #   end
 | 
				
			||||||
    # 
 | 
					    # 
 | 
				
			||||||
    def can?(action, noun)
 | 
					    def can?(action, noun, *extra_args)
 | 
				
			||||||
      (@can_definitions || []).reverse.each do |base_behavior, defined_action, defined_noun, defined_block|
 | 
					      (@can_definitions || []).reverse.each do |base_behavior, defined_action, defined_noun, defined_block|
 | 
				
			||||||
        defined_actions = expand_actions(defined_action)
 | 
					        defined_actions = expand_actions(defined_action)
 | 
				
			||||||
        defined_nouns = [defined_noun].flatten
 | 
					        defined_nouns = [defined_noun].flatten
 | 
				
			||||||
        if includes_action?(defined_actions, action) && includes_noun?(defined_nouns, noun)
 | 
					        if includes_action?(defined_actions, action) && includes_noun?(defined_nouns, noun)
 | 
				
			||||||
          result = can_perform_action?(action, noun, defined_actions, defined_nouns, defined_block)
 | 
					          result = can_perform_action?(action, noun, defined_actions, defined_nouns, defined_block, extra_args)
 | 
				
			||||||
          return base_behavior ? result : !result
 | 
					          return base_behavior ? result : !result
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
@ -190,7 +190,7 @@ module CanCan
 | 
				
			|||||||
      end.flatten
 | 
					      end.flatten
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    def can_perform_action?(action, noun, defined_actions, defined_nouns, defined_block)
 | 
					    def can_perform_action?(action, noun, defined_actions, defined_nouns, defined_block, extra_args)
 | 
				
			||||||
      if defined_block.nil?
 | 
					      if defined_block.nil?
 | 
				
			||||||
        true
 | 
					        true
 | 
				
			||||||
      else
 | 
					      else
 | 
				
			||||||
@ -198,6 +198,7 @@ module CanCan
 | 
				
			|||||||
        block_args << action if defined_actions.include?(:manage)
 | 
					        block_args << action if defined_actions.include?(:manage)
 | 
				
			||||||
        block_args << (noun.class == Class ? noun : noun.class) if defined_nouns.include?(:all)
 | 
					        block_args << (noun.class == Class ? noun : noun.class) if defined_nouns.include?(:all)
 | 
				
			||||||
        block_args << (noun.class == Class ? nil : noun)
 | 
					        block_args << (noun.class == Class ? nil : noun)
 | 
				
			||||||
 | 
					        block_args += extra_args
 | 
				
			||||||
        return defined_block.call(*block_args)
 | 
					        return defined_block.call(*block_args)
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
				
			|||||||
@ -132,4 +132,12 @@ describe CanCan::Ability do
 | 
				
			|||||||
    @ability.clear_aliased_actions
 | 
					    @ability.clear_aliased_actions
 | 
				
			||||||
    @ability.aliased_actions[:modify].should be_nil
 | 
					    @ability.aliased_actions[:modify].should be_nil
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  it "should pass additional arguments to block from can?" do
 | 
				
			||||||
 | 
					    @ability.can :read, Integer do |int, x|
 | 
				
			||||||
 | 
					      int > x
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					    @ability.can?(:read, 2, 1).should be_true
 | 
				
			||||||
 | 
					    @ability.can?(:read, 2, 3).should be_false
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user