Skip to content
Browse files

setter for role names

  • Loading branch information...
1 parent a7e62dc commit b58254c96878df89142a2735d027edee3502e2db @henning-koch henning-koch committed
Showing with 45 additions and 16 deletions.
  1. +9 −1 lib/aegis/has_role.rb
  2. +36 −15 spec/has_role_spec.rb
View
10 lib/aegis/has_role.rb
@@ -7,12 +7,20 @@ def has_role(options = {})
may_pattern = /^may_(.+?)([\!\?])$/
+ send :define_method, :role_names do
+ (role_name || '').split(/\s*,\s*/)
+ end
+
+ send :define_method, :role_names= do |role_names|
+ self.role_name = role_names.join(',')
+ end
+
send :define_method, :role do
roles.first
end
send :define_method, :roles do
- (role_name || '').split(/\s*,\s*/).collect do |role_name|
+ role_names.collect do |role_name|
permissions.call.find_role_by_name(role_name)
end.compact
end
View
51 spec/has_role_spec.rb
@@ -24,6 +24,13 @@
user.should respond_to(:roles)
end
+ it "should allow a default for new records" do
+ permissions_class = @permissions_class
+ @user_class.class_eval { has_role :permissions => permissions_class, :default => "admin" }
+ user = @user_class.new
+ user.role_name.should == 'admin'
+ end
+
end
describe 'role' do
@@ -44,41 +51,55 @@
describe 'roles' do
+ it "should return the corresponding role for each role name" do
+ user = @user_class.new
+ user.should_receive(:role_names).and_return(['admin', 'user'])
+ user.roles.collect(&:name).should == ['admin', 'user']
+ end
+
+ it "should ignore unknown role names that doesn't match a known role" do
+ user = @user_class.new
+ user.should_receive(:role_names).and_return(['unknown role', 'user'])
+ user.roles.collect(&:name).should == ['user']
+ end
+
+ end
+
+ describe 'role_names' do
+
it "should be empty if the role name is blank" do
user = @user_class.new(:role_name => '')
- user.roles.should be_empty
+ user.role_names.should be_empty
end
it "should be empty if the role_name is nil" do
user = @user_class.new(:role_name => nil)
- user.roles.should be_empty
+ user.role_names.should be_empty
end
- it "should return a single role as an array with a single element" do
+ it "should deserialize a single role name into an array with a single element" do
user = @user_class.new(:role_name => 'admin')
- user.roles.collect(&:name).should == ['admin']
+ user.role_names.should == ['admin']
end
- it "should return multiple, comma-separated roles as an array" do
+ it "should deserialize multiple, comma-separated role names into an array" do
user = @user_class.new(:role_name => 'admin,user')
- user.roles.collect(&:name).should == ['admin', 'user']
+ user.role_names.should == ['admin', 'user']
end
it "should ignore whitespace around the comma-separator" do
user = @user_class.new(:role_name => 'admin , user')
- user.roles.collect(&:name).should == ['admin', 'user']
+ user.role_names.should == ['admin', 'user']
end
+
+ end
- it "should ignore unknown role names that doesn't match a known role" do
- user = @user_class.new(:role_name => 'admin,nonexisting_role_name,user')
- user.roles.collect(&:name).should == ['admin', 'user']
- end
+ describe 'role_names=' do
- it "should honor the default for new records" do
- permissions_class = @permissions_class
- @user_class.class_eval { has_role :permissions => permissions_class, :default => "admin,user" }
+ it "should serialize the given array into a comma-separated string and store it into #role_name" do
user = @user_class.new
- user.roles.collect(&:name).should == ['admin', 'user']
+ user.should_receive(:role_name=).with("first,second")
+ user.role_names = ['first', 'second']
end
end

0 comments on commit b58254c

Please sign in to comment.
Something went wrong with that request. Please try again.