A platform for community discussion. Free, open, simple.
DEV: Allow using an AR relation as a model in services
Loïc Guitaut063685570692
diff --git a/plugins/chat/app/jobs/service_job.rb b/app/jobs/service_job.rbsimilarity index 100%rename from plugins/chat/app/jobs/service_job.rbrename to app/jobs/service_job.rbdiff --git a/app/services/service/base.rb b/app/services/service/base.rbindex 531169f44cd..08f60c6a487 100644--- a/app/services/service/base.rb+++ b/app/services/service/base.rb@@ -139,13 +139,15 @@ module Servicedef call(instance, context)context[name] = super- raise ArgumentError, "Model not found" if !optional && context[name].blank?+ if !optional && (!context[name] || context[name].try(:empty?))+ raise ArgumentError, "Model not found"+ endif context[name].try(:invalid?)context[result_key].fail(invalid: true)context.fail!endrescue ArgumentError => exception- context[result_key].fail(exception: exception)+ context[result_key].fail(exception: exception, not_found: true)context.fail!endenddiff --git a/lib/service_runner.rb b/lib/service_runner.rbindex 7dbec6e7a67..05c9f507709 100644--- a/lib/service_runner.rb+++ b/lib/service_runner.rb@@ -80,7 +80,9 @@ class ServiceRunnerdefault_name: "default",},on_model_not_found: {- condition: ->(name = "model") { failure_for?("result.model.#{name}") && result[name].blank? },+ condition: ->(name = "model") do+ failure_for?("result.model.#{name}") && result["result.model.#{name}"].not_found+ end,key: %w[result model],default_name: "model",},diff --git a/plugins/chat/spec/lib/service_runner_spec.rb b/spec/lib/service_runner_spec.rbsimilarity index 90%rename from plugins/chat/spec/lib/service_runner_spec.rbrename to spec/lib/service_runner_spec.rbindex 9afa8d39722..bfd6eb653c4 100644--- a/plugins/chat/spec/lib/service_runner_spec.rb+++ b/spec/lib/service_runner_spec.rb@@ -136,6 +136,18 @@ RSpec.describe ServiceRunner doendend+ class RelationModelService+ include Service::Base++ model :fake_model++ private++ def fetch_fake_model+ User.where(admin: false)+ end+ end+describe ".call(service, &block)" dosubject(:runner) { described_class.call(service, object, &actions_block) }@@ -145,7 +157,9 @@ RSpec.describe ServiceRunner dolet(:actions) { "proc {}" }let(:object) doClass- .new(Chat::ApiController) do+ .new(ApplicationController) do+ include WithServiceHelper+def requestOpenStruct.newend@@ -352,6 +366,34 @@ RSpec.describe ServiceRunner doendendend++ context "when fetching an ActiveRecord relation" do+ let(:service) { RelationModelService }++ context "when the service does not fail" do+ before { Fabricate(:user) }++ it "does not run the provided block" do+ expect(runner).not_to eq :no_model+ end++ it "does not fetch records from the relation" do+ runner+ expect(result[:fake_model]).not_to be_loaded+ end+ end++ context "when the service fails" do+ it "runs the provided block" do+ expect(runner).to eq :no_model+ end++ it "does not fetch records from the relation" do+ runner+ expect(result[:fake_model]).not_to be_loaded+ end+ end+ endendcontext "when using the on_model_errors action" do