Skip to content

A platform for community discussion. Free, open, simple.

DEV: Fill defaulted keyword arguments in service runner blocks (#42264)

Régis Hanole27a3a4542ba
diff --git a/docs/developer-guides/docs/03-code-internals/19-service-objects.md b/docs/developer-guides/docs/03-code-internals/19-service-objects.md
index 175b4c63d93..fe53007b6ea 100644
--- a/docs/developer-guides/docs/03-code-internals/19-service-objects.md
+++ b/docs/developer-guides/docs/03-code-internals/19-service-objects.md
@@ -534,6 +534,12 @@ on_failed_contract { |contract, my_model:| do_something(contract, my_model) }
You could do all this by only using the result object, but it’s a bit nicer this way (and will ensure the key you’re trying to access actually exists).
+A required keyword argument (`my_model:`) will raise if the service didn’t set that key, which is what you want most of the time. For a key that’s only set on some paths (a model fetched by a step that didn’t run, or steps wrapped in an `only_if` block), give the argument a default value and it will be used as a fallback:
+
+```rb
+on_failure { |my_model: nil| do_something(my_model) }
+```
+
### `on_success`
Will execute the provided block if the service succeeds.
diff --git a/lib/service/runner.rb b/lib/service/runner.rb
index 980f060e20a..f4a794cf1d8 100644
--- a/lib/service/runner.rb
+++ b/lib/service/runner.rb
@@ -32,6 +32,11 @@
# argument to their block. `on_model_errors` receives the actual model so it’s
# easier to inspect it, and `on_exceptions` receives the actual exception.
#
+# Blocks can also declare keyword arguments, which are filled from the
+# service result (e.g. +on_success do |model:|+). A required keyword argument
+# raises if the service didn’t set that key, while a keyword argument with a
+# default uses its default as a fallback when the key is absent.
+#
# @example In a controller
# def create
# MyService.call do
@@ -165,7 +170,7 @@ class Service::Runner
result[
[*action[:key], action[:name] || args.first || action[:default_name]].join(".")
].public_send(action[:property] || :itself),
- **result.slice(*block.parameters.filter_map { it.last if it.first == :keyreq }),
+ **result.slice(*block.parameters.filter_map { it.last if it.first.in?(%i[keyreq key]) }),
&block
)
end,
diff --git a/spec/lib/service/runner_spec.rb b/spec/lib/service/runner_spec.rb
index d1a97879906..7fb060e0283 100644
--- a/spec/lib/service/runner_spec.rb
+++ b/spec/lib/service/runner_spec.rb
@@ -247,6 +247,27 @@ RSpec.describe Service::Runner do
expect(runner.last).to eq :model_found
end
+ context "when a keyword arg has a default value" do
+ let(:actions) { <<-BLOCK }
+ proc do
+ on_success { |fake_model: :default| fake_model }
+ on_failure { |fake_model: :default| fake_model }
+ end
+ BLOCK
+
+ it "uses the value from the result when the key is present" do
+ expect(runner).to eq :model_found
+ end
+
+ context "when the key is not present in the result" do
+ let(:service) { FailureService }
+
+ it "uses the default value" do
+ expect(runner).to eq :default
+ end
+ end
+ end
+
context "when using the on_success action" do
let(:actions) { <<-BLOCK }
proc do