Class: Rhino::ResourceScope
- Inherits:
-
Object
- Object
- Rhino::ResourceScope
- Defined in:
- lib/rhino/resource_scope.rb
Overview
Base class for auto-discovered model scopes.
Provides access to the current user and organization from RequestStore, so scopes can implement role-based or user-specific filtering.
Usage:
# app/models/scopes/project_scope.rb
module Scopes
class ProjectScope < Rhino::ResourceScope
def apply(relation)
if role == "viewer"
relation.where(status: "active")
else
relation
end
end
end
end
Available methods inside apply:
- +user+ — the current authenticated user (or nil)
- +organization+ — the current organization (or nil)
- +role+ — shortcut for the user's role slug in the current org (or nil)
Instance Method Summary collapse
-
#apply(relation) ⇒ ActiveRecord::Relation
Subclasses must implement this method.
-
#organization ⇒ Organization?
The current organization, if any.
-
#role ⇒ String?
Shortcut: the user’s role slug in the current organization.
-
#user ⇒ User?
The current authenticated user, if any.
Instance Method Details
#apply(relation) ⇒ ActiveRecord::Relation
Subclasses must implement this method.
55 56 57 |
# File 'lib/rhino/resource_scope.rb', line 55 def apply(relation) raise NotImplementedError, "#{self.class.name} must implement #apply(relation)" end |
#organization ⇒ Organization?
The current organization, if any.
37 38 39 |
# File 'lib/rhino/resource_scope.rb', line 37 def organization RequestStore.store[:rhino_organization] if defined?(RequestStore) end |
#role ⇒ String?
Shortcut: the user’s role slug in the current organization.
43 44 45 46 47 48 49 |
# File 'lib/rhino/resource_scope.rb', line 43 def role return nil unless user && organization if user.respond_to?(:role_slug_for_validation) user.role_slug_for_validation(organization) end end |
#user ⇒ User?
The current authenticated user, if any.
31 32 33 |
# File 'lib/rhino/resource_scope.rb', line 31 def user RequestStore.store[:rhino_current_user] if defined?(RequestStore) end |