Skip to content

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

DEV: Don’t check model validity when no changes have been made

Loïc Guitaut1a9f5770442e
diff --git a/lib/service/base.rb b/lib/service/base.rb
index 3bece326a95..8a065da4c3d 100644
--- a/lib/service/base.rb
+++ b/lib/service/base.rb
@@ -196,7 +196,7 @@ module Service
def run_step
context[name] = super
raise NotFound if !optional && (!context[name] || context[name].try(:empty?))
- if context[name].try(:invalid?)
+ if context[name].try(:has_changes_to_save?) && context[name].try(:invalid?)
context[result_key].fail(invalid: true)
context.fail!
end
diff --git a/spec/lib/service/runner_spec.rb b/spec/lib/service/runner_spec.rb
index ca76f3225ad..bf1a54b654d 100644
--- a/spec/lib/service/runner_spec.rb
+++ b/spec/lib/service/runner_spec.rb
@@ -83,7 +83,7 @@ RSpec.describe Service::Runner do
private
def fetch_fake_model
- OpenStruct.new(invalid?: true)
+ OpenStruct.new(has_changes_to_save?: true, invalid?: true)
end
end
@@ -444,7 +444,7 @@ RSpec.describe Service::Runner do
context "when using the block argument" do
let(:actions) { <<-BLOCK }
proc do
- on_model_errors(:fake_model) { |model| model == OpenStruct.new(invalid?: true) }
+ on_model_errors(:fake_model) { |model| model.invalid? }
end
BLOCK
diff --git a/spec/lib/service/steps_inspector_spec.rb b/spec/lib/service/steps_inspector_spec.rb
index 3f1e6f2452e..67f33b6478f 100644
--- a/spec/lib/service/steps_inspector_spec.rb
+++ b/spec/lib/service/steps_inspector_spec.rb
@@ -281,7 +281,11 @@ RSpec.describe Service::StepsInspector do
before do
class DummyService
def fetch_model
- OpenStruct.new(invalid?: true, errors: ActiveModel::Errors.new(nil))
+ OpenStruct.new(
+ has_changes_to_save?: true,
+ invalid?: true,
+ errors: ActiveModel::Errors.new(nil),
+ )
end
end
end