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

Adding a new method to action classes to return permitted param declarations #1122

Merged
merged 2 commits into from
Apr 29, 2020
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
16 changes: 16 additions & 0 deletions spec/lucky/action_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,22 @@ describe Lucky::Action do
end
end

describe ".query_param_declarations" do
it "returns an empty array" do
PlainText::Index.query_param_declarations.size.should eq 0
end

it "returns required param declarations" do
RequiredParams::Index.query_param_declarations.size.should eq 1
RequiredParams::Index.query_param_declarations.first.should eq "required_page : Int32"
end

it "returns optional param declarations" do
OptionalParams::Index.query_param_declarations.size.should eq 6
OptionalParams::Index.query_param_declarations.should contain "bool_with_false_default : Bool | ::Nil"
end
end

describe "params" do
it "can get params" do
action = PlainText::Index.new(build_context(path: "/?q=test"), params)
Expand Down
4 changes: 4 additions & 0 deletions src/lucky/routable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ module Lucky::Routable
macro included
PARAM_DECLARATIONS = [] of Crystal::Macros::TypeDeclaration

@@query_param_declarations : Array(String) = [] of String
class_getter query_param_declarations : Array(String)

macro inherited
inherit_param_declarations
end
Expand Down Expand Up @@ -345,6 +348,7 @@ module Lucky::Routable
# `/user_confirmations?token=abc123`
macro param(type_declaration)
{% PARAM_DECLARATIONS << type_declaration %}
@@query_param_declarations << "{{ type_declaration.var }} : {{ type_declaration.type }}"

def {{ type_declaration.var }} : {{ type_declaration.type }}
{% is_nilable_type = type_declaration.type.is_a?(Union) %}
Expand Down