Class: AgentCode::ResourceScope

Inherits:
Object
  • Object
show all
Defined in:
lib/agentcode/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 < AgentCode::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

Instance Method Details

#apply(relation) ⇒ ActiveRecord::Relation

Subclasses must implement this method.

Parameters:

  • relation (ActiveRecord::Relation)

    the current query scope

Returns:

  • (ActiveRecord::Relation)

    the modified scope

Raises:

  • (NotImplementedError)


55
56
57
# File 'lib/agentcode/resource_scope.rb', line 55

def apply(relation)
  raise NotImplementedError, "#{self.class.name} must implement #apply(relation)"
end

#organizationOrganization?

The current organization, if any.

Returns:

  • (Organization, nil)


37
38
39
# File 'lib/agentcode/resource_scope.rb', line 37

def organization
  RequestStore.store[:agentcode_organization] if defined?(RequestStore)
end

#roleString?

Shortcut: the user's role slug in the current organization.

Returns:

  • (String, nil)


43
44
45
46
47
48
49
# File 'lib/agentcode/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

#userUser?

The current authenticated user, if any.

Returns:

  • (User, nil)


31
32
33
# File 'lib/agentcode/resource_scope.rb', line 31

def user
  RequestStore.store[:agentcode_current_user] if defined?(RequestStore)
end