Fix rule check on Hash-like subjects

This commit is contained in:
Sam Pohlenz 2011-02-04 16:46:57 +10:30
parent b2028c8aa7
commit f23bbe04ef
2 changed files with 8 additions and 2 deletions

View File

@ -21,7 +21,7 @@ module CanCan
# Matches both the subject and action, not necessarily the conditions # Matches both the subject and action, not necessarily the conditions
def relevant?(action, subject) def relevant?(action, subject)
subject = subject.values.first if subject.kind_of? Hash subject = subject.values.first if subject.class == Hash
@match_all || (matches_action?(action) && matches_subject?(subject)) @match_all || (matches_action?(action) && matches_subject?(subject))
end end
@ -31,7 +31,7 @@ module CanCan
call_block_with_all(action, subject, extra_args) call_block_with_all(action, subject, extra_args)
elsif @block && !subject_class?(subject) elsif @block && !subject_class?(subject)
@block.call(subject, *extra_args) @block.call(subject, *extra_args)
elsif @conditions.kind_of?(Hash) && subject.kind_of?(Hash) elsif @conditions.kind_of?(Hash) && subject.class == Hash
nested_subject_matches_conditions?(subject) nested_subject_matches_conditions?(subject)
elsif @conditions.kind_of?(Hash) && !subject_class?(subject) elsif @conditions.kind_of?(Hash) && !subject_class?(subject)
matches_conditions_hash?(subject) matches_conditions_hash?(subject)

View File

@ -290,6 +290,12 @@ describe CanCan::Ability do
@ability.can?(:read, "foobar" => Range).should be_false @ability.can?(:read, "foobar" => Range).should be_false
@ability.can?(:read, 123 => Range).should be_true @ability.can?(:read, 123 => Range).should be_true
end end
it "should allow to check ability on Hash-like object" do
class Container < Hash; end
@ability.can :read, Container
@ability.can?(:read, Container.new).should be_true
end
it "should have initial attributes based on hash conditions of 'new' action" do it "should have initial attributes based on hash conditions of 'new' action" do
@ability.can :manage, Range, :foo => "foo", :hash => {:skip => "hashes"} @ability.can :manage, Range, :foo => "foo", :hash => {:skip => "hashes"}