aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorGenadi Samokovarov <gsamokovarov@gmail.com>2019-01-01 12:07:20 +0200
committerGenadi Samokovarov <gsamokovarov@gmail.com>2019-04-19 14:15:23 +0900
commit963fef7b37d6bc31dd385c92cbe8be934aa2871f (patch)
tree5e485a0ca86d3497c943dddb89ac4e633372b5d6 /activesupport
parenta3110fe20bc418034332bd01165df7fe6f20258e (diff)
downloadrails-963fef7b37d6bc31dd385c92cbe8be934aa2871f.tar.gz
rails-963fef7b37d6bc31dd385c92cbe8be934aa2871f.tar.bz2
rails-963fef7b37d6bc31dd385c92cbe8be934aa2871f.zip
Simplify the ActionableError.{dispatch,action} boundries
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/actionable_error.rb18
-rw-r--r--activesupport/test/actionable_error_test.rb25
2 files changed, 14 insertions, 29 deletions
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