moving class dependency inline in specs

This commit is contained in:
Ryan Bates 2009-11-16 15:04:57 -08:00
parent 0b8b51b4fc
commit d5f6e0570c

View File

@ -1,37 +1,14 @@
require File.dirname(__FILE__) + '/../spec_helper'
class Ability
include CanCan::Ability
alias_action :update, :destroy, :to => :modify
can :read, :all
can :read, Symbol do |sym|
sym
end
can :preview, :all do |object_class, object|
[object_class, object]
end
can :manage, Array do |action, object|
[action, object]
end
can :modify, :all do |object_class, object|
:modify_called
end
end
class AdminAbility
include CanCan::Ability
can :manage, :all do |action, object_class, object|
[action, object_class, object]
end
end
describe Ability do
describe CanCan::Ability do
before(:each) do
@ability = Ability.new
@ability_class = Class.new
@ability_class.send(:include, CanCan::Ability)
@ability = @ability_class.new
end
it "should be able to :read anything" do
@ability_class.can :read, :all
@ability.can?(:read, String).should be_true
@ability.can?(:read, 123).should be_true
end
@ -41,32 +18,48 @@ describe Ability do
end
it "should return what block returns on a can call" do
@ability_class.can :read, Symbol do |sym|
sym
end
@ability.can?(:read, Symbol).should be_nil
@ability.can?(:read, :some_symbol).should == :some_symbol
end
it "should pass class with object if :all objects are accepted" do
@ability_class.can :preview, :all do |object_class, object|
[object_class, object]
end
@ability.can?(:preview, 123).should == [Fixnum, 123]
end
it "should pass class with no object if :all objects are accepted and class is passed directly" do
@ability_class.can :preview, :all do |object_class, object|
[object_class, object]
end
@ability.can?(:preview, Hash).should == [Hash, nil]
end
it "should pass action and object for global manage actions" do
@ability_class.can :manage, Array do |action, object|
[action, object]
end
@ability.can?(:stuff, [1, 2]).should == [:stuff, [1, 2]]
@ability.can?(:stuff, Array).should == [:stuff, nil]
end
it "should alias update or destroy actions to modify action" do
@ability_class.alias_action :update, :destroy, :to => :modify
@ability_class.can :modify, :all do |object_class, object|
:modify_called
end
@ability.can?(:update, 123).should == :modify_called
@ability.can?(:destroy, 123).should == :modify_called
end
end
describe AdminAbility do
it "should return block result for action, object_class, and object for any action" do
@ability = AdminAbility.new
@ability_class.can :manage, :all do |action, object_class, object|
[action, object_class, object]
end
@ability.can?(:foo, 123).should == [:foo, Fixnum, 123]
@ability.can?(:bar, Fixnum).should == [:bar, Fixnum, nil]
end