From 76205f8fcb6e967eb506e62e79443956d2f661c6 Mon Sep 17 00:00:00 2001 From: Sergey Tsvetkov Date: Mon, 24 Sep 2018 20:21:20 +0300 Subject: [PATCH 1/3] Fixed problem with double fail callback call in case of expection during the execution --- lib/performify/base.rb | 2 +- spec/lib/performify/execute_spec.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/performify/base.rb b/lib/performify/base.rb index c3c4ecf..81e9f3a 100644 --- a/lib/performify/base.rb +++ b/lib/performify/base.rb @@ -32,7 +32,7 @@ def execute! fail!(with_callbacks: false) end rescue RuntimeError, ActiveRecord::RecordInvalid => e - fail!(exception: e) + fail!(exception: e, with_callbacks: false) end raise ActiveRecord::Rollback if fail? diff --git a/spec/lib/performify/execute_spec.rb b/spec/lib/performify/execute_spec.rb index a230e18..0bf2ac4 100644 --- a/spec/lib/performify/execute_spec.rb +++ b/spec/lib/performify/execute_spec.rb @@ -49,6 +49,15 @@ end.to yield_control end + context 'when execution raises ActiveRecord::RecordInvalid' do + it 'calls registered fail callback only once' do + expect do |b| + described_class.register_callback(:fail, &b) + subject.execute! { raise ActiveRecord::RecordInvalid } + end.to yield_control.once + end + end + context 'when execution has been already performed' do it 'performes execution only once' do expect do |b| From 0c9d714a4c6376cc499edc99937a8c2f4f10526a Mon Sep 17 00:00:00 2001 From: Sergey Tsvetkov Date: Mon, 24 Sep 2018 20:21:55 +0300 Subject: [PATCH 2/3] Bump version to 0.9.1 --- lib/performify/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/performify/version.rb b/lib/performify/version.rb index 0e5ac11..e1ccb86 100644 --- a/lib/performify/version.rb +++ b/lib/performify/version.rb @@ -1,3 +1,3 @@ module Performify - VERSION = '0.9.0'.freeze + VERSION = '0.9.1'.freeze end From 2088d6a5aae0d3ca5c5d923b597b9a0a0bf21681 Mon Sep 17 00:00:00 2001 From: Sergey Tsvetkov Date: Mon, 24 Sep 2018 20:26:03 +0300 Subject: [PATCH 3/3] Added empty lines after guard if-s according to rubocop rules --- lib/performify/base.rb | 1 + lib/performify/callbacks.rb | 2 ++ lib/performify/validation.rb | 2 ++ 3 files changed, 5 insertions(+) diff --git a/lib/performify/base.rb b/lib/performify/base.rb index 81e9f3a..fece66d 100644 --- a/lib/performify/base.rb +++ b/lib/performify/base.rb @@ -75,6 +75,7 @@ def fail? private def prepare_instance define_singleton_method(:execute!) do |&block| return if defined?(@result) + super(&block) end diff --git a/lib/performify/callbacks.rb b/lib/performify/callbacks.rb index df1a22b..330b2f1 100644 --- a/lib/performify/callbacks.rb +++ b/lib/performify/callbacks.rb @@ -12,6 +12,7 @@ def register_callback(type_of_callback, method_name = nil, &block) unless TYPES_OF_CALLBACK.include?(type_of_callback) raise UnknownTypeOfCallbackError, "Type #{type_of_callback} is not allowed" end + @service_callbacks ||= {} @service_callbacks[type_of_callback] ||= [] @service_callbacks[type_of_callback] << method_name if method_name @@ -23,6 +24,7 @@ def execute_callbacks(type_of_callback, instance) unless TYPES_OF_CALLBACK.include?(type_of_callback) raise UnknownTypeOfCallbackError, "Type #{type_of_callback} is not allowed" end + cbs = (@service_callbacks || {}).fetch(type_of_callback, []) cbs.each { |cb| cb.is_a?(Proc) ? instance.instance_eval(&cb) : instance.send(cb) } nil diff --git a/lib/performify/validation.rb b/lib/performify/validation.rb index d4f28d7..afde65e 100644 --- a/lib/performify/validation.rb +++ b/lib/performify/validation.rb @@ -26,6 +26,7 @@ def schema def validate return args if schema.nil? + result = schema.with(with_options).call(args) if result.success? @inputs = result.output @@ -37,6 +38,7 @@ def validate def errors!(new_errors) raise ArgumentError, 'Errors should be a hash' if new_errors.nil? || !new_errors.respond_to?(:to_h) + new_errors.to_h.each do |key, value| errors[key] = errors.key?(key) ? [errors[key]].flatten(1) + [value].flatten(1) : value end