diff --git a/lib/hanami/action.rb b/lib/hanami/action.rb index 4e1015cb..c2e12c7b 100644 --- a/lib/hanami/action.rb +++ b/lib/hanami/action.rb @@ -301,6 +301,7 @@ def initialize(config: self.class.config) # @since 0.1.0 # @api private def call(env) + env[ACTION_INSTANCE] = self request = nil response = nil diff --git a/lib/hanami/action/constants.rb b/lib/hanami/action/constants.rb index 845cce64..11bc8fa1 100644 --- a/lib/hanami/action/constants.rb +++ b/lib/hanami/action/constants.rb @@ -245,5 +245,9 @@ class Action # @since 2.0.0 # @api private DEFAULT_CHARSET = "utf-8" + + # @since 2.2.0 + # @api private + ACTION_INSTANCE = "hanami.action_instance" end end diff --git a/spec/support/fixtures.rb b/spec/support/fixtures.rb index ac803e03..ae72455c 100644 --- a/spec/support/fixtures.rb +++ b/spec/support/fixtures.rb @@ -585,7 +585,9 @@ def handle(_req, _res) class ParamsAction < Hanami::Action def handle(req, res) - res.body = req.params.to_h.inspect + params = req.params.to_h + params.delete(Hanami::Action::ACTION_INSTANCE.to_sym) + res.body = params.inspect end end diff --git a/spec/unit/hanami/action_spec.rb b/spec/unit/hanami/action_spec.rb index d891ef64..a75aae47 100644 --- a/spec/unit/hanami/action_spec.rb +++ b/spec/unit/hanami/action_spec.rb @@ -21,6 +21,14 @@ expect(response.body).to eq(["Hi from TestAction!"]) end + it "sets the action instance on the request environment object" do + action = CallAction.new + env = {} + action.call(env) + + expect(env[Hanami::Action::ACTION_INSTANCE]).to eq(action) + end + context "when an exception isn't handled" do it "should raise an actual exception" do expect { UncheckedErrorCallAction.new.call({}) }.to raise_error(RuntimeError)