allow strings along with symbols in Ability definition and checking

This commit is contained in:
Ryan Bates 2011-03-24 11:52:54 -07:00
parent 7ee942c334
commit a03d35272b
3 changed files with 14 additions and 7 deletions

View File

@ -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? 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? @match_all = action.nil? && subject.nil?
@base_behavior = base_behavior @base_behavior = base_behavior
@actions = [action].flatten @actions = [action].flatten.compact.map(&:to_sym)
@subjects = [subject].flatten @subjects = [subject].flatten.compact.map(&:to_sym)
@conditions = conditions || {} @conditions = conditions || {}
@block = block @block = block
end end
@ -75,16 +75,16 @@ module CanCan
def subject_object?(subject) def subject_object?(subject)
# klass = (subject.kind_of?(Hash) ? subject.values.first : subject).class # klass = (subject.kind_of?(Hash) ? subject.values.first : subject).class
# klass == Class || klass == Module # klass == Class || klass == Module
!subject.kind_of?(Symbol) !subject.kind_of?(Symbol) && !subject.kind_of?(String)
end end
def matches_action?(action) def matches_action?(action)
@expanded_actions.include?(:access) || @expanded_actions.include?(action) @expanded_actions.include?(:access) || @expanded_actions.include?(action.to_sym)
end end
def matches_subject?(subject) def matches_subject?(subject)
subject = subject_name(subject) if subject_object? 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 end
# TODO deperecate this # TODO deperecate this

View File

@ -35,6 +35,13 @@ describe CanCan::Ability do
@ability.can?(:paint, :cars).should be_false @ability.can?(:paint, :cars).should be_false
end 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 # Aliases

View File

@ -4,7 +4,7 @@ require "spec_helper"
describe CanCan::Rule do describe CanCan::Rule do
before(:each) do before(:each) do
@conditions = {} @conditions = {}
@rule = CanCan::Rule.new(true, :read, Integer, @conditions, nil) @rule = CanCan::Rule.new(true, :read, :integers, @conditions, nil)
end end
it "should return no association joins if none exist" do it "should return no association joins if none exist" do
@ -33,7 +33,7 @@ describe CanCan::Rule do
end end
it "should return no association joins if conditions is nil" do 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 == {} rule.associations_hash.should == {}
end end
end end