Class: Rhino::PendingScopedContext

Inherits:
Object
  • Object
show all
Defined in:
lib/rhino/query.rb

Overview

Fluent explicit-context builder. Holds a user (and, once chained, an org) and resolves queries with that context installed into RequestStore at build time.

Instance Method Summary collapse

Constructor Details

#initialize(user:, organization: nil) ⇒ PendingScopedContext

Returns a new instance of PendingScopedContext.



87
88
89
90
# File 'lib/rhino/query.rb', line 87

def initialize(user:, organization: nil)
  @user = user
  @organization = organization
end

Instance Method Details

#in_organization(organization) ⇒ Object



92
93
94
95
# File 'lib/rhino/query.rb', line 92

def in_organization(organization)
  @organization = organization
  self
end

#query(model_class) ⇒ Object

Build a fully-baked relation for model_class under this explicit context.

Because Rails default_scopes bake at BUILD time, we install the user+org into RequestStore, build the relation via Rhino.query, then restore RequestStore. The org+user are baked into the returned relation — no stickiness, fully isolated: a later Rhino.query with no context still fails closed.



103
104
105
106
107
# File 'lib/rhino/query.rb', line 103

def query(model_class)
  Rhino::Context.with(user: @user, organization: @organization) do
    Rhino.query(model_class)
  end
end

#run(&block) ⇒ Object

Run block with the explicit context installed into RequestStore. Queries inside the block see the context; RequestStore is restored afterward. Returns the block's value.



119
120
121
# File 'lib/rhino/query.rb', line 119

def run(&block)
  Rhino::Context.with(user: @user, organization: @organization, &block)
end

#scoped_query(model_class, scope_name = nil) ⇒ Object

Build a fully-baked, named-scoped relation under this explicit context.



110
111
112
113
114
# File 'lib/rhino/query.rb', line 110

def scoped_query(model_class, scope_name = nil)
  Rhino::Context.with(user: @user, organization: @organization) do
    Rhino.scoped_query(model_class, scope_name)
  end
end