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.



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

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

Instance Method Details

#in_organization(organization) ⇒ Object



97
98
99
100
# File 'lib/rhino/query.rb', line 97

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.



108
109
110
111
112
# File 'lib/rhino/query.rb', line 108

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.



124
125
126
# File 'lib/rhino/query.rb', line 124

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.



115
116
117
118
119
# File 'lib/rhino/query.rb', line 115

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