Permalink
Browse files

added default_role concept

  • Loading branch information...
1 parent bb31656 commit 62b6265599e7d8a17a20ad57981605703efc07c7 @kristianmandrup committed May 9, 2011
View
@@ -1,6 +1,12 @@
h1. Changelog
-h2. V. 0.4.6.5
+h2. May 9, 2011
+
+*version 0.4.9*
+
+- Added #default_role and #default_role instance methods and #default_role and #default_role= class methods.
+- The #default_role class method attempts to set the default role of the class to an available role that matches the class name, or the class name with User at the end removed (fx GuestUser becomes :guest).
+
+
-* Feb 9/2011 - merged johanlunds/master to support Rubygems 1.5 and resolve dependencies
View
@@ -50,9 +50,16 @@ h3. Strategy: admin_flag
strategy :admin_flag, :default
valid_roles_are :admin, :guest
+
+ def initialize attributes = {}
+ super
+ add_role default_role
+ end
end
</pre>
+Here the initializer initially sets the role of the user tothe default_role, which is the available role matching the class name (or nil in case no match)
+
h3. Strategy: one_role
For strategies that use a separate Role model you must call the class method #role_class with the name of the role class
View
@@ -1 +1 @@
-0.4.8
+0.4.9
@@ -10,10 +10,28 @@ module Roles::ActiveRecord
def self.included(base)
base.extend Roles::Base
base.extend ClassMethods
+ base.send :include, InstanceMethods
base.orm_name = :active_record
end
+ module InstanceMethods
+ def default_role?
+ has_role?(default_role)
+ end
+
+ def default_role
+ self.class.default_role
+ end
+ end
+
module ClassMethods
+ def default_role
+ self.to_s.gsub(/(.+)User$/, '\1').underscore.to_sym
+ end
+
+ def default_role= drole
+ @default_role = drole
+ end
def valid_single_strategies
[:admin_flag, :one_role, :role_string]
@@ -10,7 +10,7 @@ def valid_roles_are(*role_list)
end
end
end
- end
+ end
end
module RoleClass
@@ -1,6 +1,6 @@
module Roles::ActiveRecord
module Strategy
- module Shared
+ module Shared
def set_role role
update_attributes(role_attribute => new_role(role))
end
@@ -9,7 +9,8 @@ def admin?
module Roles::ActiveRecord
module Strategy
module Single
- include Shared
+ include Shared
+
# assigns first valid role from list of roles
def add_roles *role_names
new_roles = select_valid_roles(role_names)
@@ -28,6 +28,7 @@ module Implementation
def set_role role
# set_empty_role
role = role.first if role.kind_of? Array
+ # puts "set_role: #{role.inspect}"
self.send("#{role_attribute}=", role)
# role.users << self
end
@@ -3,4 +3,10 @@ class User < ActiveRecord::Base
strategy :many_roles
valid_roles_are :admin, :guest, :user
+
+ def initialize attributes = {}
+ super
+# role = default_role
+ add_role default_role
+ end
end
@@ -3,4 +3,10 @@ class User < ActiveRecord::Base
strategy :one_role
valid_roles_are :admin, :guest, :user
+
+ def initialize attributes = {}
+ super
+# role = default_role
+ add_role default_role
+ end
end
@@ -24,7 +24,6 @@ class Role
end
end
-
# it "should be true that a User that includes Roles::Generic has a complete Roles::Generic interface" do
# # mutation API
# [:roles=, :role=, :add_roles, :add_role, :remove_role, :remove_roles, :exchange_roles, :exchange_role].each do |api_method|
@@ -47,6 +47,29 @@
@admin_user.class.respond_to?(class_api_method).should be_true
end
end
+
+ describe '#default_role' do
+ it "should be true that the default user role is empty when no valid role matches class name in lowercase (no :user)" do
+ @default_user.class.default_role.should == :user
+ @default_user.default_role.should == :user
+ @default_user.has_role?(:user).should be_true
+ end
+
+ it "should be true that the User class has a valid role of :guest" do
+ @default_user.valid_role?(:user).should be_true
+ end
+ end
+
+ describe '#default_role?' do
+ it "should be true that the default User has a role that is the default role" do
+ @default_user.default_role?.should be_true
+ end
+
+ it "should not be that after changing the role it is still the default role" do
+ @default_user.role = :admin
+ @default_user.default_role?.should be_false
+ end
+ end
describe '#valid_role?' do
it "should be true that the admin user has a valid role of :guest" do
@@ -197,7 +220,7 @@
u.role = :admin
u.has_role?(:admin).should be_true
end
-
+
context 'the guest user' do
it "should be valid after setting the role" do
@guest_user.should be_valid
@@ -232,7 +255,7 @@
end
end
end
-
+
describe '#add_role' do
it "should add user role :admin and :guest using #add_roles" do
@empty_user.add_role :admin
@@ -249,7 +272,7 @@
end
end
end
-
+
describe '#add_roles' do
it "should add user role :admin and :guest using #add_roles" do
if @empty_user.class.role_strategy.multiplicity == :multi
@@ -13,6 +13,8 @@ def default_user_setup
@admin_user = User.create(:name => 'Admin user')
@admin_user.roles = :admin
@admin_user.save
-
+
+ @default_user = User.create(:name => 'def User')
+ @default_user.save
# puts "Admin roles: #{@admin_user.roles_list}, #{@admin_user.inspect}, #{Role.all}"
end

0 comments on commit 62b6265

Please sign in to comment.