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, route_group: nil) ⇒ PendingScopedContext

Returns a new instance of PendingScopedContext.



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

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

Instance Method Details

#for_user(user) ⇒ Object

Set the explicit user for this context.



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

def for_user(user)
  @user = user
  self
end

#in_organization(organization) ⇒ Object



113
114
115
116
# File 'lib/rhino/query.rb', line 113

def in_organization(organization)
  @organization = organization
  self
end

#in_route_group(route_group) ⇒ Object

Act as the named route group for this context (see Rhino.in_route_group).



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

def in_route_group(route_group)
  @route_group = route_group
  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.



136
137
138
139
140
# File 'lib/rhino/query.rb', line 136

def query(model_class)
  Rhino::Context.with(user: @user, organization: @organization, route_group: @route_group) 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.



152
153
154
# File 'lib/rhino/query.rb', line 152

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

#scoped_query(model_class, scope_name = nil) ⇒ Object

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



143
144
145
146
147
# File 'lib/rhino/query.rb', line 143

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