Sha256: 43a7b0023b6eb6b116c7f43ed00bca1575f1c6e39f968b46dd7fbe1e361b2c3d

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

module RoleAuthorization
  module Roles
    module Role
      def self.included(base)
        base.send :extend, ClassMethods
        base.send :include, InstanceMethods
        base.class_eval do
          validates_uniqueness_of :name
          serialize :user_ids
        end
      end

      module InstanceMethods
        def users(scope = nil)
          if user_ids.is_a?(Hash)
            User.where(:id => user_ids[scope])
          else
            User.where(:id => user_ids)
          end
        end

        def add_user(user_id, scope = nil)
          unserialized_user_ids = self.user_ids

          if scope.nil? || scope.is_a?(Symbol) || scope.is_a?(String) || scope.is_a?(Class)
            unserialized_user_ids ||= Array.new
            unserialized_user_ids << user_id
            unserialized_user_ids.uniq!
          else
            unserialized_user_ids ||= Hash.new
            unserialized_user_ids[scope.id] ||= Array.new
            unserialized_user_ids[scope.id] << user_id
            unserialized_user_ids[scope.id].uniq!
          end

          self.user_ids = unserialized_user_ids

          save
        end

        def remove_user(user_id, scope = nil)
          unserialized_user_ids = self.user_ids

          if scope.nil? || scope.is_a?(Symbol) || scope.is_a?(String) || scope.is_a?(Class)
            unserialized_user_ids ||= Array.new
            unserialized_user_ids.delete(user_id)
          else
            unserialized_user_ids ||= Hash.new
            unserialized_user_ids[scope.id] ||= Array.new
            unserialized_user_ids[scope.id].delete(user_id)
          end

          self.user_ids = unserialized_user_ids

          save
        end
      end

      module ClassMethods
        def group(group_name)
          RoleAuthorization::Roles.manager.groups[group_name.to_sym]
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
role_authorization-0.2.1 lib/role_authorization/roles/role.rb
role_authorization-0.2.0 lib/role_authorization/roles/role.rb