Permalink
Browse files

more on spec for SimpleRoles::Base

  • Loading branch information...
1 parent ef640c2 commit 24ac7b0c7088677884d20adf83a31d204678eec7 @stanislaw committed Sep 25, 2011
View
@@ -3,16 +3,14 @@ source "http://rubygems.org"
# Declare your gem's dependencies in simple_roles.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
-gemspec
-gem 'activerecord'
-gem 'rake-kit'
gem 'require_all'
-
gem 'sugar-high'
gem 'sweetloader'
group :development, :test do
+ gem 'activerecord'
+ gem 'rake-kit'
gem 'devise'
gem 'mysql2'
gem 'jeweler'
@@ -50,6 +50,7 @@ def has_any_role? *rolez
false
end
+ # TODO: remove duplication in SimpleRoles::Configuration:
SimpleRoles::Configuration.valid_roles.each do |role|
define_method :"#{role}?" do
roles.include?(:"#{role}")
@@ -88,7 +89,7 @@ def roles_array
end
- class RolesArray < Set
+ class ::RolesArray < Set
attr_reader :base
@@ -8,6 +8,7 @@ class << self
def valid_roles= vr
raise "There should be an array of valid roles" if !vr.kind_of?(Array)
@valid_roles = vr
+ distribute_methods
end
def valid_roles
@@ -17,6 +18,17 @@ def valid_roles
def default_roles
[:user, :admin]
end
+
+ def distribute_methods
+ SimpleRoles::Base.class_eval do
+ SimpleRoles::Configuration.valid_roles.each do |role|
+ define_method :"#{role}?" do
+ roles.include?(:"#{role}")
+ end
+ alias_method :"is_#{role}?", :"#{role}?"
+ end
+ end
+ end
end
end
end
@@ -11,6 +11,8 @@
end
it "should get root" do
+ pending
+
get '/'
response.status.should be(200)
end
@@ -42,7 +42,24 @@
@user ||= User.new(:name => "stanislaw")
end
- pending do
+ it "#has_role?, #has_roles?" do
+ subject.roles << :admin
+ subject.has_role?(:admin).should be_true
+ subject.has_role?(:admin, :user).should be_false
+ subject.has_roles?(:editor).should be_false
+ subject.roles << :user
+ subject.has_role?(:admin, :user).should be_true
+ subject.has_role?([:admin, :user]).should be_true
+ end
+
+ it "#admin?, #user?, #editor? ..." do
+ subject.roles << :admin
+ subject.admin?.should be_true
+ subject.is_admin?.should be_true
+ subject.user?.should be_false
+ subject.editor?.should be_false
+ subject.roles << :editor
+ subject.editor?.should be_true
end
end
@@ -51,6 +68,13 @@
@user ||= User.new(:name => "stanislaw")
end
+ it "#roles= should set roles" do
+ subject.roles = :admin
+ subject.roles.should == Set.new([:admin])
+ subject.roles = :user
+ subject.roles.should == Set.new([:user])
+ end
+
it "#roles << should add roles" do
subject.roles << :admin
subject.roles.should == Set.new([:admin])

0 comments on commit 24ac7b0

Please sign in to comment.