A platform for community discussion. Free, open, simple.
DEV: Make the `model` step compatible with collections
Loïc Guitautbc4e016b49c9
diff --git a/plugins/chat/app/services/base.rb b/plugins/chat/app/services/base.rbindex 78915cf09b3..ad440c2ac09 100644--- a/plugins/chat/app/services/base.rb+++ b/plugins/chat/app/services/base.rb@@ -219,7 +219,7 @@ module Chatclass ModelStep < Stepdef call(instance, context)context[name] = super- raise ArgumentError, "Model not found" if !context[name]+ raise ArgumentError, "Model not found" if context[name].blank?rescue ArgumentError => exceptioncontext[result_key].fail(exception: exception)context.fail!diff --git a/plugins/chat/spec/lib/service_runner_spec.rb b/plugins/chat/spec/lib/service_runner_spec.rbindex 8c601de838f..4c3fd28d813 100644--- a/plugins/chat/spec/lib/service_runner_spec.rb+++ b/plugins/chat/spec/lib/service_runner_spec.rb@@ -76,6 +76,30 @@ RSpec.describe Chat::ServiceRunner doendend+ class FailureWithCollectionModelService+ include Chat::Service::Base++ model :fake_model, :fetch_fake_model++ private++ def fetch_fake_model+ []+ end+ end++ class SuccessWithCollectionModelService+ include Chat::Service::Base++ model :fake_model, :fetch_fake_model++ private++ def fetch_fake_model+ [:models_found]+ end+ end+describe ".call(service, &block)" dosubject(:runner) { described_class.call(service, object, &actions_block) }@@ -207,19 +231,39 @@ RSpec.describe Chat::ServiceRunner doendBLOCK- context "when the service failed without a model" do- let(:service) { FailureWithModelService }+ context "when fetching a single model" do+ context "when the service fails without a model" do+ let(:service) { FailureWithModelService }- it "runs the provided block" do- expect(runner).to eq :no_model+ it "runs the provided block" do+ expect(runner).to eq :no_model+ end+ end++ context "when the service does not fail with a model" do+ let(:service) { SuccessWithModelService }++ it "does not run the provided block" do+ expect(runner).not_to eq :no_model+ endendend- context "when the service does not fail with a model" do- let(:service) { SuccessWithModelService }+ context "when fetching a collection" do+ context "when the service fails without a model" do+ let(:service) { FailureWithCollectionModelService }- it "does not run the provided block" do- expect(runner).not_to eq :no_model+ it "runs the provided block" do+ expect(runner).to eq :no_model+ end+ end++ context "when the service does not fail with a model" do+ let(:service) { SuccessWithCollectionModelService }++ it "does not run the provided block" do+ expect(runner).not_to eq :no_model+ endendendend