Class: Rhino::PendingScopedContext
- Inherits:
-
Object
- Object
- Rhino::PendingScopedContext
- 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
-
#for_user(user) ⇒ Object
Set the explicit user for this context.
- #in_organization(organization) ⇒ Object
-
#in_route_group(route_group) ⇒ Object
Act as the named route group for this context (see Rhino.in_route_group).
-
#initialize(user:, organization: nil, route_group: nil) ⇒ PendingScopedContext
constructor
A new instance of PendingScopedContext.
-
#query(model_class) ⇒ Object
Build a fully-baked relation for
model_classunder this explicit context. -
#run(&block) ⇒ Object
Run
blockwith the explicit context installed into RequestStore. -
#scoped_query(model_class, scope_name = nil, *args) ⇒ Object
Build a fully-baked, named-scoped relation under this explicit context.
Constructor Details
#initialize(user:, organization: nil, route_group: nil) ⇒ PendingScopedContext
Returns a new instance of PendingScopedContext.
110 111 112 113 114 |
# File 'lib/rhino/query.rb', line 110 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.
128 129 130 131 |
# File 'lib/rhino/query.rb', line 128 def for_user(user) @user = user self end |
#in_organization(organization) ⇒ Object
116 117 118 119 |
# File 'lib/rhino/query.rb', line 116 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).
122 123 124 125 |
# File 'lib/rhino/query.rb', line 122 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.
139 140 141 142 143 |
# File 'lib/rhino/query.rb', line 139 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.
155 156 157 |
# File 'lib/rhino/query.rb', line 155 def run(&block) Rhino::Context.with(user: @user, organization: @organization, route_group: @route_group, &block) end |
#scoped_query(model_class, scope_name = nil, *args) ⇒ Object
Build a fully-baked, named-scoped relation under this explicit context.
146 147 148 149 150 |
# File 'lib/rhino/query.rb', line 146 def scoped_query(model_class, scope_name = nil, *args) Rhino::Context.with(user: @user, organization: @organization, route_group: @route_group) do Rhino.scoped_query(model_class, scope_name, *args) end end |