From a03d35272bfac0c944e59a9ad4c27d7f155b2680 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Thu, 24 Mar 2011 11:52:54 -0700 Subject: [PATCH] allow strings along with symbols in Ability definition and checking --- lib/cancan/rule.rb | 10 +++++----- spec/cancan/ability_spec.rb | 7 +++++++ spec/cancan/rule_spec.rb | 4 ++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/cancan/rule.rb b/lib/cancan/rule.rb index f4af9cd..769f006 100644 --- a/lib/cancan/rule.rb +++ b/lib/cancan/rule.rb @@ -14,8 +14,8 @@ module CanCan raise Error, "You are not able to supply a block with a hash of conditions in #{action} #{subject} ability. Use either one." if conditions.kind_of?(Hash) && !block.nil? @match_all = action.nil? && subject.nil? @base_behavior = base_behavior - @actions = [action].flatten - @subjects = [subject].flatten + @actions = [action].flatten.compact.map(&:to_sym) + @subjects = [subject].flatten.compact.map(&:to_sym) @conditions = conditions || {} @block = block end @@ -75,16 +75,16 @@ module CanCan def subject_object?(subject) # klass = (subject.kind_of?(Hash) ? subject.values.first : subject).class # klass == Class || klass == Module - !subject.kind_of?(Symbol) + !subject.kind_of?(Symbol) && !subject.kind_of?(String) end def matches_action?(action) - @expanded_actions.include?(:access) || @expanded_actions.include?(action) + @expanded_actions.include?(:access) || @expanded_actions.include?(action.to_sym) end def matches_subject?(subject) subject = subject_name(subject) if subject_object? subject - @expanded_subjects.include?(:all) || @expanded_subjects.include?(subject) # || matches_subject_class?(subject) + @expanded_subjects.include?(:all) || @expanded_subjects.include?(subject.to_sym) # || matches_subject_class?(subject) end # TODO deperecate this diff --git a/spec/cancan/ability_spec.rb b/spec/cancan/ability_spec.rb index 66c393a..68c3753 100644 --- a/spec/cancan/ability_spec.rb +++ b/spec/cancan/ability_spec.rb @@ -35,6 +35,13 @@ describe CanCan::Ability do @ability.can?(:paint, :cars).should be_false end + it "allows strings instead of symbols" do + @ability.can "paint", "fences" + @ability.can?("paint", "fences").should be_true + @ability.can "access", "all" + @ability.can?("wax", "cars").should be_true + end + # Aliases diff --git a/spec/cancan/rule_spec.rb b/spec/cancan/rule_spec.rb index ca2464e..c679eff 100644 --- a/spec/cancan/rule_spec.rb +++ b/spec/cancan/rule_spec.rb @@ -4,7 +4,7 @@ require "spec_helper" describe CanCan::Rule do before(:each) do @conditions = {} - @rule = CanCan::Rule.new(true, :read, Integer, @conditions, nil) + @rule = CanCan::Rule.new(true, :read, :integers, @conditions, nil) end it "should return no association joins if none exist" do @@ -33,7 +33,7 @@ describe CanCan::Rule do end it "should return no association joins if conditions is nil" do - rule = CanCan::Rule.new(true, :read, Integer, nil, nil) + rule = CanCan::Rule.new(true, :read, :integers, nil, nil) rule.associations_hash.should == {} end end