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

Custom options for reusable params blocks #639

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Next Release

#### Features

* [#639](https://github.com/intridea/grape/pull/639): Adds options for reusable params blocks
* [#637](https://github.com/intridea/grape/pull/637): Added 'exactly_one_of' validation - [@Morred](https://github.com/Morred).
* [#626](https://github.com/intridea/grape/pull/626): Mutually exclusive params - [@oliverbarnes](https://github.com/oliverbarnes).
* [#617](https://github.com/intridea/grape/pull/617): Running tests on Ruby 2.1.1, Rubinius 2.1 and 2.2, Ruby and JRuby HEAD - [@dblock](https://github.com/dblock).
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,11 @@ module SharedParams
optional :end_date
end

params :order do |options|
optional :order_by, type:Symbol, values:options[:order_by], default:options[:default_order_by]
optional :order, type:Symbol, values:%i(asc desc), default:options[:default_order]
end

params :pagination do
optional :page, type: Integer
optional :per_page, type: Integer
Expand All @@ -731,7 +736,9 @@ class API < Grape::API
desc "Get collection"
params do
use :period, :pagination
use :order, order_by:%i(id created_at), default_order_by: :created_at, default_order: :asc
end

get do
Collection.from(params[:start_date]).to(params[:end_date])
.page(params[:page]).per(params[:per_page])
Expand Down
3 changes: 2 additions & 1 deletion lib/grape/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,12 @@ def params(params)

def use(*names)
named_params = @api.settings[:named_params] || {}
options = names.last.is_a?(Hash) ? names.pop : {}
names.each do |name|
params_block = named_params.fetch(name) do
raise "Params :#{name} not found!"
end
instance_eval(&params_block)
instance_exec(options, &params_block)
end
end
alias_method :use_scope, :use
Expand Down
9 changes: 7 additions & 2 deletions spec/grape/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -800,18 +800,23 @@ module SharedParams
subject.helpers SharedParams

subject.helpers do
params :pagination do
params :pagination do |options|
optional :page, type: Integer
optional :per_page, type: Integer
end
params :order do |options|
optional :order, type: Symbol, values: [:asc, :desc], default: options[:default_order]
optional :order_by, type: Symbol, values: options[:order_by], default: options[:default_order_by]
end
end
end

it 'by #use' do
subject.params do
use :pagination
use :order, default_order: :asc, order_by: [:name, :created_at], default_order_by: :created_at
end
expect(subject.settings[:declared_params]).to eq [:page, :per_page]
expect(subject.settings[:declared_params]).to eq [:page, :per_page, :order, :order_by]
end

it 'by #use with multiple params' do
Expand Down