Skip to content

Commit b760958

Browse files
committed
Merge pull request #550 from dm1try/named_params
Add possibility to define reusable params.
2 parents 19dbe48 + 92d5c46 commit b760958

File tree

5 files changed

+155
-1
lines changed

5 files changed

+155
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Next Release
1414
* [#540](https://github.com/intridea/grape/pull/540): Ruby 2.1.0 is now supported - [@salimane](https://github.com/salimane).
1515
* [#544](https://github.com/intridea/grape/pull/544): The `rescue_from` keyword now handles subclasses of exceptions by default - [@xevix](https://github.com/xevix).
1616
* [#545](https://github.com/intridea/grape/pull/545): Added `type` (Array or Hash) support to `requires`, `optional` and `group` - [@bwalex](https://github.com/bwalex).
17+
* [#550](https://github.com/intridea/grape/pull/550): Added possibility to define reusable params - [@dm1try](https://github.com/dm1try).
1718
* Your contribution here.
1819

1920
#### Fixes

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,58 @@ class API < Grape::API
576576
end
577577
```
578578

579+
You can define reusable `params` using `helpers`.
580+
581+
```ruby
582+
class API < Grape::API
583+
helpers do
584+
params :pagination do
585+
optional :page, type: Integer
586+
optional :per_page, type: Integer
587+
end
588+
end
589+
590+
desc "Get collection"
591+
params do
592+
use :pagination # aliases: includes, use_scope
593+
end
594+
get do
595+
Collection.page(params[:page]).per(params[:per_page])
596+
end
597+
end
598+
```
599+
600+
You can also define reusable `params` using shared helpers.
601+
602+
```ruby
603+
module SharedParams
604+
extend Grape::API::Helpers
605+
606+
params :period do
607+
optional :start_date
608+
optional :end_date
609+
end
610+
611+
params :pagination do
612+
optional :page, type: Integer
613+
optional :per_page, type: Integer
614+
end
615+
end
616+
617+
class API < Grape::API
618+
helpers SharedParams
619+
620+
desc "Get collection"
621+
params do
622+
use :period, :pagination
623+
end
624+
get do
625+
Collection.from(params[:start_date]).to(params[:end_date])
626+
.page(params[:page]).per(params[:per_page])
627+
end
628+
end
629+
```
630+
579631
## Cookies
580632

581633
You can set, get and delete your cookies very simply using `cookies` method.

lib/grape/api.rb

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,16 @@ def helpers(new_mod = nil, &block)
268268
if block_given? || new_mod
269269
mod = settings.peek[:helpers] || Module.new
270270
if new_mod
271+
inject_api_helpers_to_mod(new_mod) if new_mod.is_a?(Helpers)
271272
mod.class_eval do
272273
include new_mod
273274
end
274275
end
275-
mod.class_eval(&block) if block_given?
276+
if block_given?
277+
inject_api_helpers_to_mod(mod) do
278+
mod.class_eval(&block)
279+
end
280+
end
276281
set(:helpers, mod)
277282
else
278283
mod = Module.new
@@ -509,6 +514,12 @@ def inherit_settings(other_stack)
509514
e.options[:app].inherit_settings(other_stack) if e.options[:app].respond_to?(:inherit_settings, true)
510515
end
511516
end
517+
518+
def inject_api_helpers_to_mod(mod, &block)
519+
mod.extend(Helpers)
520+
yield if block_given?
521+
mod.api_changed(self)
522+
end
512523
end
513524

514525
def initialize
@@ -581,5 +592,28 @@ def add_head_not_allowed_methods_and_options_methods
581592
end
582593
end
583594
end
595+
596+
# This module extends user defined helpers
597+
# to provide some API-specific functionality
598+
module Helpers
599+
attr_accessor :api
600+
def params(name, &block)
601+
@named_params ||= {}
602+
@named_params.merge! name => block
603+
end
604+
605+
def api_changed(new_api)
606+
@api = new_api
607+
process_named_params
608+
end
609+
610+
protected
611+
612+
def process_named_params
613+
if @named_params && @named_params.any?
614+
api.imbue(:named_params, @named_params)
615+
end
616+
end
617+
end
584618
end
585619
end

lib/grape/validations.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ def params(params)
147147
params
148148
end
149149

150+
def use(*names)
151+
named_params = @api.settings[:named_params] || {}
152+
names.each do |name|
153+
params_block = named_params.fetch(name) do
154+
raise "Params :#{name} not found!"
155+
end
156+
instance_eval(&params_block)
157+
end
158+
end
159+
alias_method :use_scope, :use
160+
alias_method :includes, :use
161+
150162
def full_name(name)
151163
return "#{@parent.full_name(@element)}[#{name}]" if @parent
152164
name.to_s

spec/grape/validations_spec.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,5 +698,60 @@ def validate_param!(attr_name, params)
698698
end
699699
end
700700
end # end custom validation
701+
702+
context 'named' do
703+
context 'can be defined' do
704+
it 'in helpers' do
705+
subject.helpers do
706+
params :pagination do
707+
end
708+
end
709+
end
710+
711+
it 'in helper module which kind of Grape::API::Helpers' do
712+
module SharedParams
713+
extend Grape::API::Helpers
714+
params :pagination do
715+
end
716+
end
717+
subject.helpers SharedParams
718+
end
719+
end
720+
721+
context 'can be included in usual params' do
722+
before do
723+
module SharedParams
724+
extend Grape::API::Helpers
725+
params :period do
726+
optional :start_date
727+
optional :end_date
728+
end
729+
end
730+
subject.helpers SharedParams
731+
732+
subject.helpers do
733+
params :pagination do
734+
optional :page, type: Integer
735+
optional :per_page, type: Integer
736+
end
737+
end
738+
end
739+
740+
it 'by #use' do
741+
subject.params do
742+
use :pagination
743+
end
744+
subject.settings[:declared_params].should eq [:page, :per_page]
745+
end
746+
747+
it 'by #use with multiple params' do
748+
subject.params do
749+
use :pagination, :period
750+
end
751+
subject.settings[:declared_params].should eq [:page, :per_page, :start_date, :end_date]
752+
end
753+
754+
end
755+
end
701756
end
702757
end

0 commit comments

Comments
 (0)