From f23bbe04ef7662765c1c75557dac4344a1a103c5 Mon Sep 17 00:00:00 2001 From: Sam Pohlenz Date: Fri, 4 Feb 2011 16:46:57 +1030 Subject: [PATCH] Fix rule check on Hash-like subjects --- lib/cancan/rule.rb | 4 ++-- spec/cancan/ability_spec.rb | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/cancan/rule.rb b/lib/cancan/rule.rb index 7da0075..2a5ee74 100644 --- a/lib/cancan/rule.rb +++ b/lib/cancan/rule.rb @@ -21,7 +21,7 @@ module CanCan # Matches both the subject and action, not necessarily the conditions 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)) end @@ -31,7 +31,7 @@ module CanCan call_block_with_all(action, subject, extra_args) elsif @block && !subject_class?(subject) @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) elsif @conditions.kind_of?(Hash) && !subject_class?(subject) matches_conditions_hash?(subject) diff --git a/spec/cancan/ability_spec.rb b/spec/cancan/ability_spec.rb index a77311b..abfaaea 100644 --- a/spec/cancan/ability_spec.rb +++ b/spec/cancan/ability_spec.rb @@ -290,6 +290,12 @@ describe CanCan::Ability do @ability.can?(:read, "foobar" => Range).should be_false @ability.can?(:read, 123 => Range).should be_true 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 @ability.can :manage, Range, :foo => "foo", :hash => {:skip => "hashes"}