consistency addition for ability check on Module

This commit is contained in:
Sokolov Yura 2010-09-20 04:39:06 +08:00 committed by Ryan Bates
parent cef55c95e7
commit ebef3cc745
2 changed files with 21 additions and 1 deletions

View File

@ -79,7 +79,8 @@ module CanCan
private private
def subject_class?(subject) def subject_class?(subject)
(subject.kind_of?(Hash) ? subject.values.first : subject).class == Class klass = (subject.kind_of?(Hash) ? subject.values.first : subject).class
klass == Class || klass == Module
end end
def matches_action?(action) def matches_action?(action)

View File

@ -24,6 +24,14 @@ describe CanCan::Ability do
@ability.can?(:read, :some_symbol).should == true @ability.can?(:read, :some_symbol).should == true
end end
it "should pass nil to a block when no instance is passed" do
@ability.can :read, Symbol do |sym|
sym.should be_nil
true
end
@ability.can?(:read, Symbol).should be_true
end
it "should pass to previous can definition, if block returns false or nil" do it "should pass to previous can definition, if block returns false or nil" do
@ability.can :read, Symbol @ability.can :read, Symbol
@ability.can :read, Integer do |i| @ability.can :read, Integer do |i|
@ -258,6 +266,17 @@ describe CanCan::Ability do
@ability.can?(:read, A.new).should be_true @ability.can?(:read, A.new).should be_true
end end
it "should pass nil to a block for ability on Module when no instance is passed" do
module B; end
class A; include B; end
@ability.can :read, B do |sym|
sym.should be_nil
true
end
@ability.can?(:read, B).should be_true
@ability.can?(:read, A).should be_true
end
it "passing a hash of subjects should check permissions through association" do it "passing a hash of subjects should check permissions through association" do
@ability.can :read, Range, :string => {:length => 3} @ability.can :read, Range, :string => {:length => 3}
@ability.can?(:read, "foo" => Range).should be_true @ability.can?(:read, "foo" => Range).should be_true