Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the action instance to the request environment #446

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/hanami/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/hanami/action/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion spec/support/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions spec/unit/hanami/action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down