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

Moving params in to HTTP::Server::Context #1509

Merged
merged 1 commit into from
May 29, 2021
Merged
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
13 changes: 13 additions & 0 deletions spec/lucky/params_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -683,4 +683,17 @@ describe Lucky::Params do
params.should eq({"filter" => {"name" => "euphonium"}, "page" => "1", "per" => "50"})
end
end

describe "Setting route_params later" do
it "returns the correct values for get?" do
request = build_request body: "", content_type: ""
route_params = {"id" => "from_route"}
params = Lucky::Params.new(request)

params.get?(:id).should eq nil

params.route_params = route_params
params.get?(:id).should eq "from_route"
end
end
end
1 change: 1 addition & 0 deletions src/lucky/action.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ abstract class Lucky::Action
getter :context, :route_params

def initialize(@context : HTTP::Server::Context, @route_params : Hash(String, String))
context.params.route_params = @route_params
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels weird putting this here, but where else would it go?

end

abstract def call
Expand Down
6 changes: 6 additions & 0 deletions src/lucky/context_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ class HTTP::Server::Context
def flash : Lucky::FlashStore
@_flash ||= Lucky::FlashStore.from_session(session)
end

@_params : Lucky::Params?

def params : Lucky::Params
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side bonus, params is now available in all Components thanks to #1488

@_params ||= Lucky::Params.new(request)
end
end
4 changes: 2 additions & 2 deletions src/lucky/http_method_override_handler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class Lucky::HttpMethodOverrideHandler
end

private def override_allowed?(context, http_method) : Bool
["POST"].includes?(context.request.method) && ["PATCH", "PUT", "DELETE"].includes?(http_method)
(context.request.method == "POST") && ["PATCH", "PUT", "DELETE"].includes?(http_method)
end

private def overridden_http_method(context) : String?
Lucky::Params.new(context.request).get?(:_method).try(&.upcase)
context.params.get?(:_method).try(&.upcase)
rescue Lucky::ParamParsingError
nil
end
Expand Down
6 changes: 2 additions & 4 deletions src/lucky/param_helpers.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Lucky::ParamHelpers
@_params : Lucky::Params?

def params : Lucky::Params
@_params ||= Lucky::Params.new(context.request, @route_params)
memoize def params : Lucky::Params
context.params
end
Comment on lines +2 to 4
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is basically just so you can still call params in your actions without having to call context.params.

end
10 changes: 4 additions & 6 deletions src/lucky/params.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
class Lucky::Params
@request : HTTP::Request
@route_params : Hash(String, String) = {} of String => String

# :nodoc:
private getter :request
private getter request : HTTP::Request
# :nodoc:
private getter :route_params
private getter route_params : Hash(String, String)
setter :route_params

# Create a new params object
#
Expand All @@ -20,7 +18,7 @@ class Lucky::Params
#
# Lucky::Params.new(request, route_params)
# ```
def initialize(@request, @route_params = {} of String => String)
def initialize(@request : HTTP::Request, @route_params : Hash(String, String) = empty_params)
end

# Parses the request body as `JSON::Any` or raises `Lucky::ParamParsingError` if JSON is invalid.
Expand Down