From a3110fe20bc418034332bd01165df7fe6f20258e Mon Sep 17 00:00:00 2001 From: Genadi Samokovarov Date: Sat, 29 Dec 2018 17:27:33 +0200 Subject: Drop the ambiguous `ActiveSupport::ActionableError#===` check --- .../lib/active_support/actionable_error.rb | 23 +++++++++++++--------- activesupport/test/actionable_error_test.rb | 15 ++++++++++---- 2 files changed, 25 insertions(+), 13 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/actionable_error.rb b/activesupport/lib/active_support/actionable_error.rb index aeee2177aa..be4972759b 100644 --- a/activesupport/lib/active_support/actionable_error.rb +++ b/activesupport/lib/active_support/actionable_error.rb @@ -15,20 +15,25 @@ module ActiveSupport NonActionable = Class.new(StandardError) - included do - class_attribute :_actions, default: Hash.new do |_, label| - raise NonActionable, "Cannot find action \"#{label}\" for #{self}" - end + NoActions = Hash.new do |_, label| # :nodoc: + raise NonActionable, "Cannot find action \"#{label}\" for #{self}" end - def self.===(other) # :nodoc: - super || Module === other && other.ancestors.include?(self) + included do + class_attribute :_actions, default: NoActions.dup end def self.actions(error) # :nodoc: - error = error.constantize if String === error - raise NonActionable, "#{error.name} is non-actionable" unless self === error - error._actions + 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" + end end def self.dispatch(error, label) # :nodoc: diff --git a/activesupport/test/actionable_error_test.rb b/activesupport/test/actionable_error_test.rb index 66ba94e0dd..80614b4700 100644 --- a/activesupport/test/actionable_error_test.rb +++ b/activesupport/test/actionable_error_test.rb @@ -4,8 +4,7 @@ require "abstract_unit" require "active_support/actionable_error" class ActionableErrorTest < ActiveSupport::TestCase - class NonActionableError < StandardError - end + NonActionableError = Class.new(StandardError) class DispatchableError < StandardError include ActiveSupport::ActionableError @@ -22,15 +21,23 @@ class ActionableErrorTest < ActiveSupport::TestCase end end - test "can get all action of an actionable error" do + test "lists 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 "cannot get actions from non-actionable errors" do + 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 + assert ActiveSupport::ActionableError.actions(Exception.new).empty? end test "dispatches actions from class and a label" do -- cgit v1.2.3