From 961b8c2477463ede2115e6d014b8501df6770511 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Fri, 6 Aug 2010 10:06:37 -0700 Subject: [PATCH] consider ancestors when matching classes in Ability#can, this way it works with STI - closes #55 --- lib/cancan/can_definition.rb | 6 +++++- spec/cancan/ability_spec.rb | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/cancan/can_definition.rb b/lib/cancan/can_definition.rb index 7cb56f1..718ad38 100644 --- a/lib/cancan/can_definition.rb +++ b/lib/cancan/can_definition.rb @@ -68,7 +68,11 @@ module CanCan end def matches_subject?(subject) - @subjects.include?(:all) || @subjects.include?(subject) || @subjects.any? { |sub| sub.kind_of?(Class) && subject.kind_of?(sub) } + @subjects.include?(:all) || @subjects.include?(subject) || matches_subject_class?(subject) + end + + def matches_subject_class?(subject) + @subjects.any? { |sub| sub.kind_of?(Class) && (subject.kind_of?(sub) || subject.kind_of?(Class) && subject.ancestors.include?(sub)) } end def matches_conditions_hash?(subject, conditions = @conditions) diff --git a/spec/cancan/ability_spec.rb b/spec/cancan/ability_spec.rb index d070c65..1402e59 100644 --- a/spec/cancan/ability_spec.rb +++ b/spec/cancan/ability_spec.rb @@ -135,6 +135,13 @@ describe CanCan::Ability do @ability.can?(:read, :nonstats).should be_false end + it "should check ancestors of class" do + @ability.can :read, Numeric + @ability.can?(:read, Integer).should be_true + @ability.can?(:read, 1.23).should be_true + @ability.can?(:read, "foo").should be_false + end + it "should support 'cannot' method to define what user cannot do" do @ability.can :read, :all @ability.cannot :read, Integer