phlex-reactive 0.18.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 91678969419d0d4fcb917d69f7d1c9e7478877317e8c98e84dfec78fcbf114ca
4
- data.tar.gz: 79b1ab20debf6de7a3c7c80752b15a6bfab89848276fd75ddbadafa0161b7886
3
+ metadata.gz: 60975530dead6bb72c6e179a2407c522c6e859c15a5afc597a267a59faf45178
4
+ data.tar.gz: d71e103dff040f6387ca71e7aef2089f935ccb5be608267c168042a4f936fcee
5
5
  SHA512:
6
- metadata.gz: 4050cef0a111c227ffb5c722e004fed6604e0c63f30cae2ad9f599366aeec956f0bd6776095f963bb9457fd266e782832c0b885e2006e0f9b143de8005c1b6f0
7
- data.tar.gz: cab2bcfb72b76b907e614b248a342bdad48c5c70ca2f447d10767a262676a02c5173f0dec899328d8c1f84ab9db85914d8651f529f1cb198abb3169b472d2726
6
+ metadata.gz: f15ecfa8394087f4bea3454de5d3977f6e04375869f3615c6e9eaa05a86da24be043a38414d37b463ca7af9511c4d1d90334210ce0044e0c54200893fc52e282
7
+ data.tar.gz: d03468cd274a132459eff64683943f403582da921dfc38d1f45f9cc9b22f905a3842ec65ce0c5b1e62e0777d0b7bd079ca033fe8650ff86219089172e81290e3
data/CHANGELOG.md CHANGED
@@ -6,6 +6,37 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.19.0] - 2026-11-28
10
+
11
+ ### Added
12
+
13
+ - **`action :name, transaction: false` runs one action without the endpoint's
14
+ transaction (#481).** For an action that waits on something slow that is not
15
+ the database (a remote HTTP API), which until now held a transaction and its
16
+ connection open for the whole wait. `transaction:` defaults to `true`, takes
17
+ only `true` or `false` (anything else raises `ArgumentError` at class load) and
18
+ is inherited with the action; every other action keeps the transaction. Only
19
+ the endpoint's own wrapper is skipped: an overridden `transaction_wrapper` is
20
+ not called for the action, and a transaction opened further out is untouched.
21
+ `verify_authorized` still raises after an action that never authorized, but
22
+ the endpoint has nothing to roll back, so its writes stay (unless a
23
+ transaction opened outside the endpoint rolls them back): authorize before any
24
+ side effect. `around_action`, `reply.defer` and `reply.pending` behave as before;
25
+ `run_reactive` honours the flag; the action inventory (`phlex_reactive:actions`,
26
+ `find`, the MCP tools) shows it as `(no endpoint transaction)` /
27
+ `transaction: false`.
28
+ `Component::ActionDefinition` gains a `transaction` member that defaults to
29
+ `true`, so existing `ActionDefinition.new(name:, params:, schema:)` callers
30
+ keep working.
31
+
32
+ ### Fixed
33
+
34
+ - **`run_reactive` scopes an authorization mark to the action (#481).** A
35
+ `mark_authorized!` (or an intercepted authorization call) inside an action
36
+ driven by the test helper stayed set on the thread, so a later example could
37
+ see a stale mark. The driver now opens the same tracking window the endpoint
38
+ does; it still does not run `verify_authorized` itself.
39
+
9
40
  ## [0.18.0] - 2026-11-16
10
41
 
11
42
  ### Added
@@ -4658,7 +4689,8 @@ room for dogfood fixes.
4658
4689
  scaffolds a reactive component (and an RSpec spec when the app uses RSpec),
4659
4690
  state-backed by default or record-backed with `--record`.
4660
4691
 
4661
- [Unreleased]: https://github.com/mhenrixon/phlex-reactive/compare/v0.18.0...HEAD
4692
+ [Unreleased]: https://github.com/mhenrixon/phlex-reactive/compare/v0.19.0...HEAD
4693
+ [0.19.0]: https://github.com/mhenrixon/phlex-reactive/compare/v0.18.0...v0.19.0
4662
4694
  [0.18.0]: https://github.com/mhenrixon/phlex-reactive/compare/v0.17.0...v0.18.0
4663
4695
  [0.17.0]: https://github.com/mhenrixon/phlex-reactive/compare/v0.16.14...v0.17.0
4664
4696
  [0.16.14]: https://github.com/mhenrixon/phlex-reactive/compare/v0.16.13...v0.16.14
data/README.md CHANGED
@@ -670,6 +670,7 @@ Use in controllers: `render turbo_stream: Counter.replace(counter)`.
670
670
  | `reactive_record :name` | Record-backed identity (GlobalID). State = the DB. Also defaults `#id` to `dom_id(record)`. |
671
671
  | `reactive_state :a, :b` | Signed instance-var identity. Standalone, or combined with `reactive_record` to sign transient UI state alongside the row. |
672
672
  | `action :name, params: { x: :integer }` | Declare a client-invokable action + its param schema. **Default-deny.** |
673
+ | `action :name, transaction: false` | Run this one action **without the endpoint's transaction** (default `true`), for an action that waits on something slow that is not the database (a remote API). The endpoint rolls nothing back: `verify_authorized` still raises after it, but writes it already made stay (unless a transaction opened outside the endpoint rolls them back), so authorize before any side effect. See [Running without the endpoint transaction](#running-without-the-endpoint-transaction-transaction-false). |
673
674
  | `mark_authorized!` | Inside an action: satisfy the `verify_authorized` guard after a bespoke check the interceptor can't see (a hand-rolled policy). Call it only after your check passes. |
674
675
  | `skip_verify_authorized [ :a, :b ]` | Opt a component (bare) or specific actions out of the default-ON `verify_authorized` guard — for a genuinely public component (a counter, a client-only filter). |
675
676
  | `reactive_root(**overrides)` | Spread onto the root element: emits the component `id` **and** `reactive_attrs` together, so the controller root always carries `#id`. Preferred over `id:` + `reactive_attrs`. `**overrides` (`class:`/`data:`) deep-merge. `compute: :name` binds a client-side compute **at the root** — descriptors plus the `input->reactive#recompute` delegation, so no field needs its own wiring; `nil` collapses to no binding. See [Client-side computes](#client-side-computes-reactive_compute--reactive_text). |
@@ -3262,6 +3263,49 @@ interpolated from the row's live field values (`confirm: "Delete '%{name}'?"`
3262
3263
  `Delete 'Widget'?`) — see [Draft rows for a new
3263
3264
  parent](#draft-rows-for-a-new-parent-reactive_nested_).
3264
3265
 
3266
+ ### Running without the endpoint transaction (`transaction: false`)
3267
+
3268
+ The endpoint runs every action inside a database transaction, so a broadcast
3269
+ or enqueue deferred to commit never fires for a rolled-back change. An action
3270
+ that waits on something slow that is not the database, such as a call to a
3271
+ remote HTTP API, would hold that transaction (and its database connection)
3272
+ open for the whole wait. Declare that one action `transaction: false`:
3273
+
3274
+ ```ruby
3275
+ action :refresh, transaction: false
3276
+ action :rename, params: { title: :string } # unchanged: in a transaction
3277
+ action :sync, params: { force: :boolean }, transaction: false
3278
+
3279
+ def refresh
3280
+ authorize! @widget, :update? # before any side effect
3281
+ data = RemoteService.fetch(@widget) # no transaction open while it waits
3282
+ @widget.update!(synced_at: Time.current, data:)
3283
+ end
3284
+ ```
3285
+
3286
+ - `transaction:` defaults to `true`, takes only `true` or `false` (anything else
3287
+ raises `ArgumentError` at class load) and is inherited with the action. Every
3288
+ other action, on the same component and on the same page, keeps the transaction.
3289
+ - Only the endpoint's own wrapper is skipped (an overridden `transaction_wrapper`
3290
+ is not called for the action). A transaction opened further out, by a
3291
+ transactional test or an `around_action` wrapper, is not affected, and the
3292
+ action may open its own, shorter transactions around its writes.
3293
+ - **The endpoint rolls nothing back.** `verify_authorized` still raises
3294
+ `AuthorizationNotVerified` after an action that never authorized, and the
3295
+ request still fails with no reply, but the writes it already made stay
3296
+ (unless a transaction opened outside the endpoint rolls them back). An
3297
+ error the action raises likewise rolls back only what its own transactions
3298
+ cover. **Authorize before any side effect.**
3299
+ - Commit-deferred work (a `durable:` broadcast, an enqueue under
3300
+ `enqueue_after_transaction_commit`) fires at the action's own commit, or at
3301
+ once when it has none open. "Never fires for a rolled-back change" is then the
3302
+ action's job: wrap the write and its broadcast or enqueue in one transaction.
3303
+ - `Phlex::Reactive.around_action` wrappers, `reply.defer` and `reply.pending`
3304
+ behave as for any action; `run_reactive` skips the transaction for the same
3305
+ actions the endpoint does. The text inventory (`phlex_reactive:actions`) marks
3306
+ them `(no endpoint transaction)`, and `FORMAT=json` and the MCP tools report
3307
+ `transaction: false` for them.
3308
+
3265
3309
  ### `reply` — controlling the action's reply
3266
3310
 
3267
3311
  By default an action re-renders its component in place. To do more, **return**
@@ -4068,7 +4112,8 @@ The endpoint runs your action inside a transaction and renders the reply
4068
4112
  after an enqueue renders from a database the job has not touched yet, and is
4069
4113
  *guaranteed* to draw the pre-job world — rows still present, buttons still live,
4070
4114
  counts unchanged — sitting right beside the "Queued 177 transfers" flash the
4071
- same reply emitted:
4115
+ same reply emitted. (An action declared `transaction: false` is no way out: its
4116
+ enqueue can publish at once, so the morph races the job instead.)
4072
4117
 
4073
4118
  ```ruby
4074
4119
  def restore_all
@@ -4399,7 +4444,8 @@ Phlex::Reactive.authorization_errors = [Pundit::NotAuthorizedError]
4399
4444
 
4400
4445
  # verify_authorized (ON by default): an action that authorizes NOTHING raises
4401
4446
  # AuthorizationNotVerified inside the transaction (the mutation rolls back —
4402
- # fail-closed). Satisfy it by calling one of authorization_methods, calling
4447
+ # fail-closed; an action declared transaction: false has no endpoint transaction,
4448
+ # so the endpoint rolls nothing back). Satisfy it by calling one of authorization_methods, calling
4403
4449
  # mark_authorized! after a bespoke check, or declaring skip_verify_authorized on
4404
4450
  # a genuinely public component/action. Set the method names to match your library:
4405
4451
  Phlex::Reactive.authorization_methods = %i[authorize! authorize allowed_to?]
@@ -4845,7 +4891,8 @@ end
4845
4891
  **`run_reactive` — the no-HTTP action driver.** It runs the action through the
4846
4892
  SAME contract the endpoint enforces — default-deny, the signed identity
4847
4893
  round-trip (a record-backed component's row is re-found), schema coercion, the
4848
- transaction wrapper — with no HTTP, and returns a `Result`. So a unit test can't
4894
+ transaction wrapper (skipped for a `transaction: false` action, as at the
4895
+ endpoint) — with no HTTP, and returns a `Result`. So a unit test can't
4849
4896
  pass on a component that would fail a real click:
4850
4897
 
4851
4898
  ```ruby
@@ -333,7 +333,11 @@ module Phlex
333
333
 
334
334
  # Run the action inside a transaction so transactional broadcasts (pgbus
335
335
  # broadcasts_to ... durable:) defer to after_commit and never fire for a
336
- # rolled-back change. Override to add per-request instrumentation.
336
+ # rolled-back change. Override to add per-request instrumentation. An
337
+ # action declared `transaction: false` (issue #481) skips the endpoint's
338
+ # transaction_wrapper, and with it this guarantee: its broadcasts defer to
339
+ # its own transactions' commits, or fire at once, and nothing the endpoint
340
+ # opened rolls back. The connection id and around_action below are the same.
337
341
  #
338
342
  # The actor's SSE connection id (sent as X-Pgbus-Connection) is exposed
339
343
  # for the duration of the action via Phlex::Reactive.current_connection_id,
@@ -360,32 +364,42 @@ module Phlex
360
364
  end
361
365
 
362
366
  # The action inside the around_action stack, the transaction and the
363
- # verify_authorized window (see run_action).
367
+ # verify_authorized window (see run_action). An action declared
368
+ # `transaction: false` (issue #481) runs the same window without
369
+ # transaction_wrapper, so an overridden wrapper is not called for it.
364
370
  def run_action_wrapped(component, action_def, coerced)
365
371
  with_around_actions(component, action_def, coerced) do
366
- transaction_wrapper do
367
- # verify_authorized (issue #168): open the tracking window, run the
368
- # action, then verify INSIDE the transaction so an unverified action
369
- # rolls its mutation back (fail-closed). with_tracking is a bare
370
- # yield's-worth of overhead; verify! is a no-op when the feature is
371
- # off / the action is skipped / something marked. The interceptor
372
- # (create_action's instrument!) marks the window on a real
373
- # authorization call.
374
- Phlex::Reactive::Authorization.with_tracking do
375
- result = if coerced.any?
376
- component.public_send(action_def.name, **coerced)
377
- else
378
- component.public_send(action_def.name)
379
- end
380
- Phlex::Reactive::Authorization.verify!(component.class, action_def)
381
- result
382
- end
372
+ if action_def.transaction
373
+ transaction_wrapper { run_verified_action(component, action_def, coerced) }
374
+ else
375
+ run_verified_action(component, action_def, coerced)
383
376
  end
384
377
  end
385
378
  end
386
379
 
380
+ # verify_authorized (issue #168): open the tracking window, run the action,
381
+ # then verify. Inside the transaction an unverified action rolls its
382
+ # mutation back (fail-closed); for a `transaction: false` action the
383
+ # endpoint has nothing to roll back, so only the request fails. with_tracking is
384
+ # a bare yield's-worth of overhead; verify! is a no-op when the feature is
385
+ # off / the action is skipped / something marked. The interceptor
386
+ # (create_action's instrument!) marks the window on a real authorization
387
+ # call.
388
+ def run_verified_action(component, action_def, coerced)
389
+ Phlex::Reactive::Authorization.with_tracking do
390
+ result = if coerced.any?
391
+ component.public_send(action_def.name, **coerced)
392
+ else
393
+ component.public_send(action_def.name)
394
+ end
395
+ Phlex::Reactive::Authorization.verify!(component.class, action_def)
396
+ result
397
+ end
398
+ end
399
+
387
400
  # Fold the registered around_action wrappers around `block`, innermost being
388
- # the transactioned action. The empty-stack fast path is a bare `yield` —
401
+ # the action (in its transaction unless declared `transaction: false`).
402
+ # The empty-stack fast path is a bare `yield` —
389
403
  # the default request gains only one Array#empty? check. Each wrapper is
390
404
  # called with the frozen ActionContext and the continuation as its block, so
391
405
  # a well-behaved wrapper returns action.call's value and the action's
@@ -18,7 +18,9 @@
18
18
 
19
19
  # verify_authorized (ON by default): an action that completes WITHOUT any
20
20
  # authorization call raises Phlex::Reactive::AuthorizationNotVerified inside the
21
- # transaction — the mutation rolls back (fail-closed). Satisfy it by calling one
21
+ # transaction — the mutation rolls back (fail-closed; an action declared
22
+ # `transaction: false` has no endpoint transaction to roll back). Satisfy
23
+ # it by calling one
22
24
  # of authorization_methods, calling `mark_authorized!` after a bespoke check, or
23
25
  # declaring `skip_verify_authorized` on a genuinely public component/action.
24
26
  # Set the method names to match your authorization library:
@@ -56,7 +56,10 @@ module Phlex
56
56
  end
57
57
 
58
58
  # The enforcement decision, called by the endpoint AFTER the action runs,
59
- # INSIDE the transaction (so a raise rolls the mutation back). A no-op
59
+ # INSIDE the transaction (so a raise rolls the mutation back) — except for
60
+ # an action declared `transaction: false` (issue #481), which has no
61
+ # endpoint transaction: the raise still fails the request, but the
62
+ # endpoint rolls back nothing the action already wrote. A no-op
60
63
  # unless verify_authorized is on, the action isn't skipped, and nothing
61
64
  # marked the window. Otherwise raises AuthorizationNotVerified naming the
62
65
  # component#action and the three remedies.
@@ -302,8 +302,19 @@ module Phlex
302
302
  # }
303
303
  # Array params accept BOTH a JSON array and a Rails-style index hash
304
304
  # ({ "0" => ..., "1" => ... }), so a fields_for collection works either way.
305
- def action(name, params: {})
305
+ #
306
+ # `transaction: false` (issue #481) runs this one action without the
307
+ # endpoint's transaction, for an action that waits on something slow
308
+ # that is not the database (a remote API). verify_authorized still
309
+ # raises after it, but there is nothing to roll back: authorize before
310
+ # any side effect, and wrap the writes in your own transaction.
311
+ # action :refresh, transaction: false
312
+ def action(name, params: {}, transaction: true)
306
313
  require_server_actions!(:action)
314
+ unless [true, false].include?(transaction)
315
+ raise ArgumentError,
316
+ "action #{name.inspect} transaction: must be true or false, got #{transaction.inspect}"
317
+ end
307
318
  # Issue #276: the endpoint routes `__materialize` to the lazy render
308
319
  # before the action registry is consulted — a declared one would be
309
320
  # dead code that looks invokable.
@@ -323,7 +334,9 @@ module Phlex
323
334
  assert_no_scope_double_nesting!(scope, params, name.to_sym) if scope
324
335
  Registry.write_entry(
325
336
  self, :actions, name.to_sym,
326
- ActionDefinition.new(name: name.to_sym, params: params, schema: Phlex::Reactive::ParamSchema.compile(params))
337
+ ActionDefinition.new(
338
+ name: name.to_sym, params: params, schema: Phlex::Reactive::ParamSchema.compile(params), transaction:
339
+ )
327
340
  )
328
341
  end
329
342
 
@@ -91,7 +91,20 @@ module Phlex
91
91
  # autoloading (Kit's LazyLoader resolves through the calling class's
92
92
  # const_get). Keep every constant here suffixed / implausible as a kit
93
93
  # component name — and never alias the old name back.
94
- ActionDefinition = Data.define(:name, :params, :schema)
94
+ #
95
+ # `transaction` (issue #481) is whether the endpoint runs the action inside
96
+ # its transaction wrapper: true unless declared `transaction: false`. It
97
+ # defaults here too, so a caller that builds a definition with only
98
+ # name/params/schema keeps working.
99
+ ActionDefinition = Data.define(:name, :params, :schema, :transaction) do
100
+ def initialize(name:, params:, schema:, transaction: true)
101
+ unless [true, false].include?(transaction)
102
+ raise ArgumentError, "transaction: must be true or false, got #{transaction.inspect}"
103
+ end
104
+
105
+ super
106
+ end
107
+ end
95
108
 
96
109
  # A declared client-side computation (data binding). `inputs`/`outputs` are
97
110
  # the action-param names of the fields the reducer reads/writes; `reducer`
@@ -60,11 +60,17 @@ module Phlex
60
60
  return [[info.name, "(no actions)", "", location_str(info.path), ""]] if info.actions.empty?
61
61
 
62
62
  info.actions.map do
63
- [info.name, it.name.to_s, params_str(it.params),
63
+ [info.name, action_str(it), params_str(it.params),
64
64
  location_str(it.source_location), auth_str(it)]
65
65
  end
66
66
  end
67
67
 
68
+ # The action's name, marked when it runs without the endpoint's
69
+ # transaction (issue #481). Every other row prints the bare name.
70
+ def action_str(action)
71
+ action.transaction ? action.name.to_s : "#{action.name} (no endpoint transaction)"
72
+ end
73
+
68
74
  # A minimal fixed-column table: each column padded to its widest cell.
69
75
  # No ANSI, no external dependency.
70
76
  def render_table(headers, rows)
@@ -94,6 +100,7 @@ module Phlex
94
100
  header = " action :#{action.name}"
95
101
  header += " params: #{params_str(action.params)}" unless action.params.empty?
96
102
  header += " (#{location_str(action.source_location)}) auth: #{auth_str(action)}"
103
+ header += " transaction: false" unless action.transaction
97
104
  body = action.definition ? indent(action.definition) : " (method not defined)"
98
105
  [header, body, ""]
99
106
  end
@@ -125,7 +132,8 @@ module Phlex
125
132
  source_location: location_str(action.source_location),
126
133
  authorization_call_detected: action.authorization_call_detected?,
127
134
  authorization: action.authorization_state.to_s,
128
- authorization_skip: action.authorization_skip&.to_s
135
+ authorization_skip: action.authorization_skip&.to_s,
136
+ transaction: action.transaction
129
137
  }
130
138
  end
131
139
 
@@ -34,7 +34,12 @@ module Phlex
34
34
  # * authorization_skip — :class (a bare skip_verify_authorized, own or
35
35
  # inherited), :action (this action is named in one),
36
36
  # or nil. The author's declared opt-out (issue #278).
37
- ActionInfo = Data.define(:name, :params, :source_location, :definition, :authorization_skip) do
37
+ # * transaction — false when the action is declared
38
+ # `transaction: false` and runs without the
39
+ # endpoint's transaction (issue #481), else true.
40
+ ActionInfo = Data.define(:name, :params, :source_location, :definition, :authorization_skip, :transaction) do
41
+ def initialize(name:, params:, source_location:, definition:, authorization_skip:, transaction: true) = super
42
+
38
43
  # Was any authorization method (or mark_authorized!) called directly in
39
44
  # the method body? Heuristic — a Prism scan of `definition`. False when
40
45
  # the definition is unavailable.
@@ -176,7 +181,8 @@ module Phlex
176
181
  params: action.params,
177
182
  source_location: location,
178
183
  definition: (extract_definition(location) if location),
179
- authorization_skip: authorization_skip(klass, action.name)
184
+ authorization_skip: authorization_skip(klass, action.name),
185
+ transaction: action.transaction
180
186
  )
181
187
  end
182
188
 
@@ -17,7 +17,9 @@ module Phlex
17
17
  mark_authorized! call was detected in the body — advisory only, since a
18
18
  helper may authorize indirectly), plus `authorization` — detected,
19
19
  skipped (declared skip_verify_authorized; `authorization_skip` says
20
- class or action) or none. Pass `component:` to scope to one
20
+ class or action) or none, and `transaction` — false when the action
21
+ is declared `transaction: false` and runs without the endpoint's
22
+ transaction. Pass `component:` to scope to one
21
23
  component (exact name). Read-only — schemas and paths, never runtime
22
24
  values.
23
25
  DESC
@@ -15,8 +15,9 @@ module Phlex
15
15
  Fuzzy-find reactive components by name (exact > prefix > substring >
16
16
  subsequence, on both the demodulized and full name). Returns the ranked
17
17
  matches, each with its actions' param schema, source location,
18
- authorization (detected / skipped / none, plus the skip form), and the
19
- full method-definition SOURCE
18
+ authorization (detected / skipped / none, plus the skip form), its
19
+ transaction flag (false for `transaction: false`), and the full
20
+ method-definition SOURCE
20
21
  (extracted with Prism). Read-only.
21
22
 
22
23
  NOTE: unlike the other tools (which report names/paths/schemas only),
@@ -56,6 +57,7 @@ module Phlex
56
57
  authorization_call_detected: action.authorization_call_detected?,
57
58
  authorization: action.authorization_state.to_s,
58
59
  authorization_skip: action.authorization_skip&.to_s,
60
+ transaction: action.transaction,
59
61
  # The full method SOURCE — this is the one field that exposes an
60
62
  # action body verbatim (see the tool description). A secret hardcoded
61
63
  # in an action would surface here; keep secrets out of action bodies.
@@ -131,9 +131,10 @@ module Phlex
131
131
 
132
132
  # Mark targets PENDING and let the app's own job settle them (issue #248).
133
133
  # For an action that ENQUEUES the work rather than doing it: the endpoint
134
- # renders the reply inside the transaction while the queue publishes on
135
- # commit, so a `reply.morph` after an enqueue is guaranteed to draw the
136
- # pre-job world. This replies truthfully instead.
134
+ # renders the reply inside the transaction, so publish-on-commit keeps a
135
+ # `reply.morph` after an enqueue on the pre-job world. With
136
+ # `transaction: false` the enqueue can publish at once and the job races
137
+ # the render. This replies truthfully either way.
137
138
  #
138
139
  # def re_execute(transfer_id:)
139
140
  # transfer = @bulk_payment.transfers.re_executable.find(transfer_id)
@@ -67,11 +67,14 @@ module Phlex
67
67
  # integration uses, and it looked like the enqueue-time hook — but under
68
68
  # Rails' `enqueue_after_transaction_commit = true` (the 7.2+ recommended
69
69
  # setting) `job.enqueue` is deferred to
70
- # ActiveRecord.after_all_transactions_commit, and the endpoint runs every
71
- # action inside a transaction. So the deferral — and with it `serialize` —
72
- # always fires AFTER reply.pending's `with_handle` block has exited, with
70
+ # ActiveRecord.after_all_transactions_commit, and the endpoint runs a
71
+ # default action inside a transaction. So for such an action the deferral
72
+ # — and with it `serialize` — fires AFTER reply.pending's `with_handle`
73
+ # block has exited, with
73
74
  # an empty thread-local: no metadata key, a no-op `reactive_settle`, and a
74
- # row left shimmering until someone reloads.
75
+ # row left shimmering until someone reloads. (A `transaction: false`
76
+ # action with no enclosing transaction enqueues at once, inside the block;
77
+ # capturing at instantiation covers both.)
75
78
  #
76
79
  # `new` is the one moment guaranteed to be inside the block:
77
80
  # `perform_later` → `job_or_instantiate` → `new` is synchronous, deferral
@@ -20,7 +20,8 @@ module Phlex
20
20
  #
21
21
  # The driver goes THROUGH the endpoint's security contract on purpose —
22
22
  # default-deny, signed identity round-trip (record re-find), schema coercion,
23
- # the same transaction wrapper. A helper that skipped it would teach users to
23
+ # the same transaction wrapper (skipped for a `transaction: false` action,
24
+ # as the endpoint skips it). A helper that skipped it would teach users to
24
25
  # test a component that would fail at the real endpoint.
25
26
  #
26
27
  # NB: `verbose_errors` defaults ON in the test env (Rails.env.local? — issue
@@ -90,7 +91,8 @@ module Phlex
90
91
  # 3. coercion — params pass through the action's compiled ParamSchema
91
92
  # (issue #109); undeclared keys are dropped, declared ones cast.
92
93
  # 4. transaction — run inside the endpoint's transaction wrapper so
93
- # after_commit broadcasts behave.
94
+ # after_commit broadcasts behave, unless the action is declared
95
+ # `transaction: false` (issue #481), as at the endpoint.
94
96
  # 5. authorization — a registered authorization error RAISES (the endpoint
95
97
  # maps it to 403; a unit test asserts the real exception).
96
98
  #
@@ -185,12 +187,24 @@ module Phlex
185
187
  end
186
188
  end
187
189
 
188
- # Run inside the endpoint's transaction wrapper. `coerced` already has
189
- # symbol keys (ParamSchema#coerce symbolizes to splat as kwargs), so we
190
- # splat it exactly as the endpoint does. Kept tiny and dependency tolerant:
191
- # no ActiveRecord (a state-only component in a bare app) just yields.
190
+ # Run inside the endpoint's transaction wrapper, unless the action is
191
+ # declared `transaction: false` (issue #481), exactly as the endpoint
192
+ # decides. `coerced` already has symbol keys (ParamSchema#coerce
193
+ # symbolizes to splat as kwargs), so we splat it exactly as the endpoint
194
+ # does. Kept tiny and dependency tolerant: no ActiveRecord (a state-only
195
+ # component in a bare app) just yields.
192
196
  def run_reactive_action(component, action_def, coerced)
193
- transaction_wrapper do
197
+ return invoke_reactive_action(component, action_def, coerced) unless action_def.transaction
198
+
199
+ transaction_wrapper { invoke_reactive_action(component, action_def, coerced) }
200
+ end
201
+
202
+ # Inside a verify_authorized tracking window, as at the endpoint, so a mark
203
+ # (mark_authorized!, an intercepted authorization call) is scoped to this
204
+ # action and never left set on the thread for the next test. The driver
205
+ # does not run verify! itself: an unauthorized action still returns.
206
+ def invoke_reactive_action(component, action_def, coerced)
207
+ Phlex::Reactive::Authorization.with_tracking do
194
208
  if coerced.any?
195
209
  component.public_send(action_def.name, **coerced)
196
210
  else
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Phlex
4
4
  module Reactive
5
- VERSION = "0.18.0"
5
+ VERSION = "0.19.0"
6
6
  end
7
7
  end
@@ -59,7 +59,8 @@ module Phlex
59
59
  # Raised when the default-ON verify_authorized guard (issue #168) finds an
60
60
  # action completed WITHOUT any authorization call — the fail-closed
61
61
  # presence-side complement to authorization_errors. It fires INSIDE the
62
- # action's transaction, so the mutation rolls back, and it is NOT rescued
62
+ # action's transaction, so the mutation rolls back (an action declared
63
+ # `transaction: false`, issue #481, has none to roll back), and it is NOT rescued
63
64
  # into a 4xx: it bubbles as a developer error (a 500 your error tracker
64
65
  # must see) because a missing authorize! is a bug, not a client fault. The
65
66
  # message names the component#action and all three remedies (call an
@@ -293,7 +294,8 @@ module Phlex
293
294
  # verify_authorized (issue #168): the default-ON runtime guard. When true,
294
295
  # an action that completes without any authorization call raises
295
296
  # AuthorizationNotVerified inside its transaction (fail-closed — the
296
- # mutation rolls back). Default ON is a deliberate, pre-1.0 breaking change:
297
+ # mutation rolls back; a `transaction: false` action has no endpoint
298
+ # transaction to roll back). Default ON is a deliberate, pre-1.0 breaking change:
297
299
  # a forgotten authorize! becomes a loud 500, not a silent hole. Opt out per
298
300
  # component with `skip_verify_authorized`, mark a bespoke check with
299
301
  # `mark_authorized!`, or turn it off globally here. The `defined?` guard
@@ -6,6 +6,9 @@ module Phlex
6
6
  attr_reader name: Symbol
7
7
  attr_reader params: Hash[Symbol | String, untyped]
8
8
  attr_reader schema: ParamSchema
9
+ attr_reader transaction: bool
10
+
11
+ def initialize: (name: Symbol, params: Hash[Symbol | String, untyped], schema: ParamSchema, ?transaction: bool) -> void
9
12
  end
10
13
 
11
14
  class ComputeDefinition < ::Data
@@ -45,7 +48,7 @@ module Phlex
45
48
  def from_identity: (Hash[String, untyped] payload) -> untyped
46
49
 
47
50
  # --- actions ---
48
- def action: (Symbol | String name, ?params: Hash[Symbol | String, untyped] | Symbol) -> ActionDefinition
51
+ def action: (Symbol | String name, ?params: Hash[Symbol | String, untyped] | Symbol, ?transaction: bool) -> ActionDefinition
49
52
  def reactive_actions: () -> Hash[Symbol, ActionDefinition]
50
53
  def reactive_action: (untyped name) -> bot
51
54
  def reactive_action?: (untyped name) -> bot
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phlex-reactive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.0
4
+ version: 0.19.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikael Henriksson