Sha256: 6e81addb5ea47e8dbb27d17e13109d18759b542fbe72f430417618e915d8901a

Contents?: true

Size: 941 Bytes

Versions: 1

Compression:

Stored size: 941 Bytes

Contents

module Mongoid
  module Document
    module Roleable
      def self.included(base)
        base.class_eval do |klass|
          klass.field :roles, :type => Array
          klass.index({ roles: 1 }, { background: true })

          include InstanceMethods
          extend ClassMethods
        end
      end

      module InstanceMethods
        def with_roles=(roles)
          self.roles = roles if roles.is_a? Array
        end

        def add_role(role)
          self.roles = Array.new if self.roles.nil?
          self.roles << role if role
        end

        def has_role?(role)
          self.roles.include?(role)
        end

        def remove_role(role)
          self.roles.delete_if { |r| r.casecmp(role) == 0 }
        end
      end

      module ClassMethods
        def find_roles(roles)
          roles = [roles] unless roles.is_a? Array
          criteria.in(:roles => roles).to_a
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-simple-roles-0.0.1 lib/mongoid-simple-roles.rb