-
Notifications
You must be signed in to change notification settings - Fork 22
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
Conversation
d5877fc
to
38dab69
Compare
1c0f0d6
to
54d8f8b
Compare
54d8f8b
to
cc2162f
Compare
There was a problem hiding this 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 🥳
lib/rage/controller/api.rb
Outdated
wrap_options = self.class.__wrap_parameters_options | ||
|
||
wrapped_params = if wrap_options[:include] | ||
@__params.slice(*[wrap_options[:include]].flatten) |
There was a problem hiding this comment.
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 *
?
@__params.slice(*[wrap_options[:include]].flatten) | |
@__params.slice(*wrap_options[:include]) |
There was a problem hiding this comment.
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
lib/rage/controller/api.rb
Outdated
wrapped_params = if wrap_options[:include] | ||
@__params.slice(*[wrap_options[:include]].flatten) | ||
elsif wrap_options[:exclude] | ||
@__params.except(*[wrap_options[:exclude]].flatten) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@__params.except(*[wrap_options[:exclude]].flatten) | |
@__params.except(*wrap_options[:exclude]) |
lib/rage/controller/api.rb
Outdated
@__params | ||
end | ||
|
||
@__params = @__params.merge({wrap_key => wrapped_params}) |
There was a problem hiding this comment.
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.
@__params = @__params.merge({wrap_key => wrapped_params}) | |
@__params[wrap_key] = wrapped_params |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
totally makes sense 👍
lib/rage/controller/api.rb
Outdated
# wrap_parameters :user, include: [:name, :age] | ||
# @example | ||
# wrap_parameters :user, exclude: :address | ||
def wrap_parameters(key, options = {}) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
Implementation of wrappers for controller parameters.
Controllers got new class method
wrap_parameters
that accepts 2 parameters:key
(mandatory) andoptions
(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'}}
wherewrapping_key
is the key set with thewrap_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: