discourse/spec/lib/service
Loïc Guitaut a39785f3dd
DEV: Add only_if step to DRSF (#35247)
This patch introduces a new step to the Ruby Service Framework.

`only_if` will execute its block (other steps) only if the provided
condition evaluates to `true`. As for the other steps, the name provided
is the name of the method that will be executed.

```ruby
only_if(:can_update) do
  model :user
  step :update_user
  step :notify
end

private

def can_update(guardian:)
  …
end
```

This step cannot fail and the whole block will be skipped if the method
evaluates to a falsy value.

This is rendered by the steps inspector:

```
[ 1/13] [options] default 
[ 2/13] [model] model 
[ 3/13] [policy] policy 
[ 4/13] [params] default 
[ 5/13] [lock] parameter:other_param 
[ 6/13]   [transaction]
[ 7/13]     [step] in_transaction_step_1 
[ 8/13]     [step] in_transaction_step_2 
[ 9/13] [try]
[10/13]   [step] might_raise 
[11/13] [only_if] condition ⏭️ (condition was not met)
[12/13]   [step] optional_step
[13/13] [step] final_step 
```

The inspector also renders text with colors now.

<img width="419" height="336" alt="Copie d'écran_20251008_162920"
src="https://github.com/user-attachments/assets/add4fd16-076e-4f30-ae1a-3e933494967e"
/>
2025-10-08 17:20:22 +02:00
..
runner_spec.rb DEV: Don’t check model validity when no changes have been made 2025-06-20 09:09:33 +02:00
steps_inspector_spec.rb DEV: Add only_if step to DRSF (#35247) 2025-10-08 17:20:22 +02:00