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 Lucky::Params#get_all #1389

Merged
merged 3 commits into from
Jan 19, 2021

Conversation

matthewmcgarvey
Copy link
Member

Purpose

Fixes #1380
Related luckyframework/avram#408

Description

Allows fetching an array of values from Lucky::Params. This is needed to support things like multi-select inputs, and array columns in Avram::Model.

Checklist

  • - An issue already exists detailing the issue/or feature request that this PR fixes
  • - All specs are formatted with crystal tool format spec src
  • - Inline documentation has been added and/or updated
  • - Lucky builds on docker with ./script/setup
  • - All builds and specs pass on docker with ./script/test

end

private def get_all_params(params, key : String)
vals = params.fetch_all(key + "[]")
Copy link
Member Author

Choose a reason for hiding this comment

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

You might ask, "What about if the user doesn't want to use the key[] pattern?"

I'm open to feedback but I think we're pretty clear in Lucky that we prefer convention with a backdoor and the backdoor in this scenario is to access the params directly: params.from_query.fetch_all("keys")

Comment on lines +535 to +540
vals = params.fetch_all(key + "[]")
if vals.any?
vals
else
nil
end
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about this?

Suggested change
vals = params.fetch_all(key + "[]")
if vals.any?
vals
else
nil
end
params.fetch_all(key + "[]").try &.any?

Copy link
Member Author

Choose a reason for hiding this comment

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

Doesn't #any? return a boolean? #fetch_all always returns an array, the goal is to return nil if the array is empty

Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

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

For the most part, this looks good. I just have the one concern as far as the API for it goes. Also just to be clear that the intended use is different from the params.many_nested method, right? This is just for supporting raw arrays in params?

src/lucky/params.cr Show resolved Hide resolved
Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

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

Sweet! Yeah, this feels pretty good. Thanks for making the change 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lucky::Params does not support query param or form data with array of values
3 participants