Permalink
Browse files

Added UserModel test for roles_mask accessors or ActiveRecord attribute.

To allow the setting or roles on a class as long as it has a roles_mask.

Signed-off-by: James McCarthy <james2mccarthy@gmail.com>
  • Loading branch information...
1 parent 1f60dff commit 2ec0e8fe7808b76b320bb7ddd87ebb969b6248c2 @james2m committed May 12, 2012
@@ -11,10 +11,23 @@ module UserModel
#
# end
#
+ # If using Canard with a non ActiveRecord class you can still assign roles but you will need to
+ # extend the class with Canard::UserModel and add a roles_mask attribute.
+ #
+ # class User
+ #
+ # extend Canard::UserModel
+ #
+ # attr_accessor :roles_mask
+ #
+ # acts_as_user :roles => :manager, :admin
+ #
+ # end
+ #
# == Scopes
#
# Beyond applying the roles to model acts_as_user also creates some useful scopes on the User
- # model;
+ # model for ActiveRecord models;
#
# User.with_any_role(:manager, :admin)
#
@@ -40,13 +53,13 @@ module UserModel
#
# returns all the users who don't have the manager role.
def acts_as_user(*args)
- if table_exists?
- include RoleModel
+ include RoleModel
- options = args.extract_options!.symbolize_keys
+ options = args.extract_options!.symbolize_keys
- roles options[:roles] if options.has_key?(:roles) && column_names.include?(roles_attribute_name.to_s)
+ roles options[:roles] if options.has_key?(:roles) && has_roles_mask_attribute? || has_roles_mask_accessors?
+ if respond_to?(:table_exists?) && table_exists?
valid_roles.each do |role|
define_scopes_for_role role
end
@@ -63,6 +76,14 @@ def acts_as_user(*args)
private
+ def has_roles_mask_accessors?
+ [roles_attribute_name, :"#{roles_attribute_name}="].all? { |accessor| instance_methods.include?(accessor) }
+ end
+
+ def has_roles_mask_attribute?
+ respond_to?(:column_names) && column_names.include?(roles_attribute_name.to_s)
+ end
+
def define_scopes_for_role(role)
include_scope = role.to_s.pluralize
exclude_scope = "non_#{include_scope}"
@@ -0,0 +1,7 @@
+class PlainRubyNonUser
+
+ extend Canard::UserModel
+
+ acts_as_user :roles => [:viewer, :author, :admin]
+
+end
@@ -0,0 +1,9 @@
+class PlainRubyUser
+
+ extend Canard::UserModel
+
+ attr_accessor :roles_mask
+
+ acts_as_user :roles => [:viewer, :author, :admin]
+
+end
@@ -21,11 +21,11 @@
end
create_table "user_without_roles", :force => true do |t|
- t.string "roles_mask"
+ t.integer "roles_mask"
end
create_table "users", :force => true do |t|
- t.string "roles_mask"
+ t.integer "roles_mask"
end
end
Oops, something went wrong.

0 comments on commit 2ec0e8f

Please sign in to comment.