Skip to content

Commit

Permalink
Merge pull request #28 from trickstersio/double_errors_issue_1
Browse files Browse the repository at this point in the history
Double errors issue
  • Loading branch information
kimrgrey committed Sep 24, 2018
2 parents dd84f06 + 2088d6a commit 8cc2694
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/performify/base.rb
Expand Up @@ -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?
Expand Down Expand Up @@ -75,6 +75,7 @@ def fail?
private def prepare_instance
define_singleton_method(:execute!) do |&block|
return if defined?(@result)

super(&block)
end

Expand Down
2 changes: 2 additions & 0 deletions lib/performify/callbacks.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/performify/validation.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/performify/version.rb
@@ -1,3 +1,3 @@
module Performify
VERSION = '0.9.0'.freeze
VERSION = '0.9.1'.freeze
end
9 changes: 9 additions & 0 deletions spec/lib/performify/execute_spec.rb
Expand Up @@ -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|
Expand Down

0 comments on commit 8cc2694

Please sign in to comment.