From ebef3cc74581ba7fe6ba1a281ce2824b8f6f2242 Mon Sep 17 00:00:00 2001 From: Sokolov Yura Date: Mon, 20 Sep 2010 04:39:06 +0800 Subject: [PATCH] consistency addition for ability check on Module --- lib/cancan/can_definition.rb | 3 ++- spec/cancan/ability_spec.rb | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/cancan/can_definition.rb b/lib/cancan/can_definition.rb index e9ac0fd6..5a4079ae 100644 --- a/lib/cancan/can_definition.rb +++ b/lib/cancan/can_definition.rb @@ -79,7 +79,8 @@ def attributes_from_conditions private 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 def matches_action?(action) diff --git a/spec/cancan/ability_spec.rb b/spec/cancan/ability_spec.rb index 9f8886ec..951245bd 100644 --- a/spec/cancan/ability_spec.rb +++ b/spec/cancan/ability_spec.rb @@ -23,6 +23,14 @@ end @ability.can?(:read, :some_symbol).should == true 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 @ability.can :read, Symbol @@ -257,6 +265,17 @@ class A; include B; end @ability.can?(:read, A).should be_true @ability.can?(:read, A.new).should be_true 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 @ability.can :read, Range, :string => {:length => 3}