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

Controller parameters wrapping feature #89

Merged
merged 5 commits into from
Jul 25, 2024

Conversation

alex-rogachev
Copy link
Contributor

@alex-rogachev alex-rogachev commented Jun 19, 2024

Implementation of wrappers for controller parameters.

Controllers got new class method wrap_parameters that accepts 2 parameters: key(mandatory) and options(optional). If called, all parameters are wrapped into a nested hash like this:
{query_param: 'queryparam', body_param: 'bodyparam', action: 'action', controller: 'controller', wrapping_key: {query_param: 'queryparam', body_param: 'bodyparam', action: 'action', controller: 'controller'}} where wrapping_key is the key set with the wrap_parameters method.

Parameters get wrapped only if there is no param that is named like the key passed during wrap_parameters call. Also, the 'Content_Type' header has to be present for the parameters to be wrapped.

Currently 2 options are available: include and exclude. When called with include option, the logic will only wrap parameters that are passed along with this option. If called with exclude option, all parameters except those that were passed with the option will be wrapped.

Possible enhancements:

  • wrap only request body params
  • add request_type options

@alex-rogachev alex-rogachev force-pushed the params-wrapper branch 3 times, most recently from d5877fc to 38dab69 Compare June 19, 2024 07:28
@alex-rogachev alex-rogachev force-pushed the params-wrapper branch 4 times, most recently from 1c0f0d6 to 54d8f8b Compare July 7, 2024 08:37
@alex-rogachev alex-rogachev marked this pull request as ready for review July 19, 2024 12:27
Copy link
Member

@rsamoilov rsamoilov left a comment

Choose a reason for hiding this comment

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

Great job, Alex!! Looking forward to merging this 🥳

wrap_options = self.class.__wrap_parameters_options

wrapped_params = if wrap_options[:include]
@__params.slice(*[wrap_options[:include]].flatten)
Copy link
Member

Choose a reason for hiding this comment

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

Not sure what this line means. Can we just use *?

Suggested change
@__params.slice(*[wrap_options[:include]].flatten)
@__params.slice(*wrap_options[:include])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, I added flatten to support both symbol and array options, like exclude: :param and exclude: [:param1, :param2] but now I think that this is redundant and Rails supports only array options anyway so I'm going to remove flatten

wrapped_params = if wrap_options[:include]
@__params.slice(*[wrap_options[:include]].flatten)
elsif wrap_options[:exclude]
@__params.except(*[wrap_options[:exclude]].flatten)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@__params.except(*[wrap_options[:exclude]].flatten)
@__params.except(*wrap_options[:exclude])

@__params
end

@__params = @__params.merge({wrap_key => wrapped_params})
Copy link
Member

Choose a reason for hiding this comment

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

Simple assign should be much faster.

Suggested change
@__params = @__params.merge({wrap_key => wrapped_params})
@__params[wrap_key] = wrapped_params

Copy link
Contributor Author

Choose a reason for hiding this comment

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

totally makes sense 👍

# wrap_parameters :user, include: [:name, :age]
# @example
# wrap_parameters :user, exclude: :address
def wrap_parameters(key, options = {})
Copy link
Member

Choose a reason for hiding this comment

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

Any specific reason you are not using keyword arguments here?

end
end

context 'and :include option is set as Array' do
Copy link
Member

Choose a reason for hiding this comment

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

Could you please also add a test case for when the :include option is not present in the params?

end
end

context 'and :exclude option is set as Array' do
Copy link
Member

Choose a reason for hiding this comment

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

Could you please also add a test case for when the :exclude option is not present in the params?

@rsamoilov rsamoilov merged commit 7b07d32 into rage-rb:master Jul 25, 2024
5 checks passed
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.

2 participants