Permalink
Browse files

consider ancestors when matching classes in Ability#can, this way it …

…works with STI - closes #55
  • Loading branch information...
1 parent a157b65 commit 961b8c2477463ede2115e6d014b8501df6770511 @ryanb committed Aug 6, 2010
Showing with 12 additions and 1 deletion.
  1. +5 −1 lib/cancan/can_definition.rb
  2. +7 −0 spec/cancan/ability_spec.rb
View
6 lib/cancan/can_definition.rb
@@ -68,7 +68,11 @@ def matches_action?(action)
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)
View
7 spec/cancan/ability_spec.rb
@@ -135,6 +135,13 @@
@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

0 comments on commit 961b8c2

Please sign in to comment.