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?
@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

View File

@ -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

View File

@ -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