discourse/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
..
action_base.rb
base.rb DEV: Add only_if step to DRSF (#35247) 2025-10-08 17:20:22 +02:00
contract_base.rb DEV: Remove hash-like access from service contracts 2024-10-29 16:02:51 +01:00
options_base.rb DEV: Provide user input to services using params key 2024-10-25 09:57:59 +02:00
policy_base.rb
runner.rb DEV: Follow-up to the lock step for services 2025-02-06 11:38:15 +01:00
steps_inspector.rb DEV: Add only_if step to DRSF (#35247) 2025-10-08 17:20:22 +02:00