From 963fef7b37d6bc31dd385c92cbe8be934aa2871f Mon Sep 17 00:00:00 2001 From: Genadi Samokovarov Date: Tue, 1 Jan 2019 12:07:20 +0200 Subject: Simplify the ActionableError.{dispatch,action} boundries --- .../lib/active_support/actionable_error.rb | 18 ++++++---------- activesupport/test/actionable_error_test.rb | 25 ++++++---------------- 2 files changed, 14 insertions(+), 29 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/actionable_error.rb b/activesupport/lib/active_support/actionable_error.rb index be4972759b..88a3eaa514 100644 --- a/activesupport/lib/active_support/actionable_error.rb +++ b/activesupport/lib/active_support/actionable_error.rb @@ -15,8 +15,8 @@ module ActiveSupport NonActionable = Class.new(StandardError) - NoActions = Hash.new do |_, label| # :nodoc: - raise NonActionable, "Cannot find action \"#{label}\" for #{self}" + NoActions = Hash.new do |_, name| # :nodoc: + raise NonActionable, "Cannot find action \"#{name}\"" end included do @@ -25,19 +25,15 @@ module ActiveSupport def self.actions(error) # :nodoc: case error - when String - actions(error.constantize) when ActionableError, -> it { Class === it && it < ActionableError } error._actions - when Exception - NoActions else - raise NonActionable, "#{error} is non-actionable" + NoActions end end - def self.dispatch(error, label) # :nodoc: - actions(error)[label].call + def self.dispatch(error, name) # :nodoc: + actions(error.is_a?(String) ? error.constantize : error)[name].call end module ClassMethods @@ -50,8 +46,8 @@ module ActiveSupport # ActiveRecord::Tasks::DatabaseTasks.migrate # end # end - def action(label, &block) - _actions[label] = block + def action(name, &block) + _actions[name] = block end end end diff --git a/activesupport/test/actionable_error_test.rb b/activesupport/test/actionable_error_test.rb index 80614b4700..fb2abf2bcf 100644 --- a/activesupport/test/actionable_error_test.rb +++ b/activesupport/test/actionable_error_test.rb @@ -21,44 +21,33 @@ class ActionableErrorTest < ActiveSupport::TestCase end end - test "lists all action of an actionable error" do + test "returns all action of an actionable error" do assert_equal ["Flip 1", "Flip 2"], ActiveSupport::ActionableError.actions(DispatchableError).keys assert_equal ["Flip 1", "Flip 2"], ActiveSupport::ActionableError.actions(DispatchableError.new).keys end - test "raises an error when trying to get actions from non-actionable error classes" do - assert_raises ActiveSupport::ActionableError::NonActionable do - ActiveSupport::ActionableError.actions(NonActionableError) - end - - assert_raises ActiveSupport::ActionableError::NonActionable do - ActiveSupport::ActionableError.actions(NonActionableError.name) - end - end - - test "returns no actions from non-actionable exception instances" do + test "returns no actions for non-actionable errors" do + assert ActiveSupport::ActionableError.actions(Exception).empty? assert ActiveSupport::ActionableError.actions(Exception.new).empty? end - test "dispatches actions from class and a label" do + test "dispatches actions from error and name" do assert_changes "DispatchableError.flip1", from: false, to: true do ActiveSupport::ActionableError.dispatch DispatchableError, "Flip 1" end end - test "dispatches actions from class name and a label" do + test "dispatches actions from error class as string and name" do assert_changes "DispatchableError.flip2", from: false, to: true do ActiveSupport::ActionableError.dispatch DispatchableError.name, "Flip 2" end end - test "cannot dispatch errors that do not include ActiveSupport::ActionableError" do + test "cannot dispatch missing actions" do err = assert_raises ActiveSupport::ActionableError::NonActionable do ActiveSupport::ActionableError.dispatch NonActionableError, "action" end - assert_equal <<~EXPECTED.chop, err.to_s - ActionableErrorTest::NonActionableError is non-actionable - EXPECTED + assert_equal 'Cannot find action "action"', err.to_s end end -- cgit v1.2.3