Sha256: c2c8908ee26f5ee2567b65e910d87afe1f7a684a29da15834a46b36c6eaaf5f5

Contents?: true

Size: 977 Bytes

Versions: 1

Compression:

Stored size: 977 Bytes

Contents

module Roles
  module Resource
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods 
      def users_with_role(role_name = nil)
        if role_name.nil?
          self.user_class.includes(:roles).where("roles.resource_type LIKE '%s'", self.to_s).where("roles.resource_id IS NULL")
        else
          self.user_class.includes(:roles).where("roles.resource_type LIKE '%s'", self.to_s).where("roles.resource_id IS NULL").where("roles.name LIKE '%s'", role_name.to_s)
        end
      end
    end

    def users_with_role(role_name = nil)
      if role_name.nil?
        self.class.user_class.includes(:roles).where("roles.resource_type LIKE '%s'", self.class.to_s).where("roles.resource_id = %s", self.id)
      else
        self.class.user_class.includes(:roles).where("roles.resource_type LIKE '%s'", self.class.to_s).where("roles.resource_id = %s", self.id).where("roles.name LIKE '%s'", role_name.to_s)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
roles-0.1.0 lib/roles/resource.rb