Module: RoleAuthorization::Roles::Role::InstanceMethods
- Defined in:
- lib/role_authorization/roles/role.rb
Instance Method Summary collapse
- #add_user(user_id, scope = nil) ⇒ Object
- #remove_user(user_id, scope = nil) ⇒ Object
- #scope_with(scope) ⇒ Object
- #unserialized_user_ids ⇒ Object
- #users(scope = nil) ⇒ Object
Instance Method Details
#add_user(user_id, scope = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/role_authorization/roles/role.rb', line 58 def add_user(user_id, scope = nil) hash = unserialized_user_ids hash ||= Hash.new if scope.nil? || scope.is_a?(Symbol) || scope.is_a?(String) || scope.is_a?(Class) hash[scope_with(scope)] ||= Array.new hash[scope_with(scope)] << user_id hash[scope_with(scope)].uniq! else hash[scope_with(scope)] ||= Array.new hash[scope_with(scope)] << user_id hash[scope_with(scope)].uniq! end self.user_ids = hash save end |
#remove_user(user_id, scope = nil) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/role_authorization/roles/role.rb', line 77 def remove_user(user_id, scope = nil) hash = unserialized_user_ids hash ||= Hash.new if scope.nil? || scope.is_a?(Symbol) || scope.is_a?(String) || scope.is_a?(Class) hash[scope_with(scope)] ||= Array.new hash[scope_with(scope)].delete(user_id) else hash[scope_with(scope)] ||= Array.new hash[scope_with(scope)].delete(user_id) end self.user_ids = hash save end |
#scope_with(scope) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/role_authorization/roles/role.rb', line 37 def scope_with(scope) if scope.blank? || scope.is_a?(Symbol) || scope.is_a?(String) || scope.is_a?(Integer) scope || :all else scope.id end end |
#unserialized_user_ids ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/role_authorization/roles/role.rb', line 19 def unserialized_user_ids result = if Rails::VERSION::MAJOR >= 4 if self.user_ids.is_a?(Hash) self.user_ids else self.user_ids.unserialized_value end else self.user_ids end if result.is_a?(Hash) result else Hash.new end end |
#users(scope = nil) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/role_authorization/roles/role.rb', line 45 def users(scope = nil) @users ||= {} @users[scope] ||= if user_ids.blank? [] else if scope.nil? RoleAuthorization::Roles::Manager.user_klass.where(:id => unserialized_user_ids.values.flatten.uniq).all else RoleAuthorization::Roles::Manager.user_klass.where(:id => unserialized_user_ids[scope_with(scope)]).all end end end |