Skip to content

Commit

Permalink
Merge pull request #1237 from ochagata/feature/allow_chained_input_to…
Browse files Browse the repository at this point in the history
…_given

Changed the input of `given`
  • Loading branch information
dblock committed Jan 6, 2016
2 parents 857b331 + bafeaf9 commit 87cd364
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

* [#1227](https://github.com/ruby-grape/grape/pull/1227): Store `message_key` on Grape::Exceptions::Validation - [@stjhimy](https://github.com/sthimy).
* [#1232](https://github.com/ruby-grape/grape/pull/1232): Helpers are now available inside `rescue_from` - [@namusyaka](https://github.com/namusyaka).
* [#1237](https://github.com/ruby-grape/grape/pull/1237): Allow multiple parameters in `given`, which behaves as if the scopes were nested in the inputted order - [@ochagata](https://github.com/ochagata).
* Your contribution here.

#### Fixes
Expand Down
8 changes: 5 additions & 3 deletions lib/grape/dsl/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ def all_or_none_of(*attrs)
# @raise Grape::Exceptions::UnknownParameter if `attr` has not been
# defined in this scope yet
# @yield a parameter definition DSL
def given(attr, &block)
fail Grape::Exceptions::UnknownParameter.new(attr) unless declared_param?(attr)
new_lateral_scope(dependent_on: attr, &block)
def given(*attrs, &block)
attrs.each do |attr|
fail Grape::Exceptions::UnknownParameter.new(attr) unless declared_param?(attr)
end
new_lateral_scope(dependent_on: attrs, &block)
end

# Test for whether a certain parameter has been defined in this params
Expand Down
4 changes: 3 additions & 1 deletion lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def initialize(opts, &block)
# validated
def should_validate?(parameters)
return false if @optional && params(parameters).respond_to?(:all?) && params(parameters).all?(&:blank?)
return false if @dependent_on && params(parameters).try(:[], @dependent_on).blank?
@dependent_on.each do |dependency|
return false if params(parameters).try(:[], dependency).blank?
end if @dependent_on
return true if parent.nil?
parent.should_validate?(parameters)
end
Expand Down
26 changes: 26 additions & 0 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,32 @@ def initialize(value)
expect(last_response.status).to eq(200)
end

it 'applies the validations of multiple parameters' do
subject.params do
optional :a, :b
given :a, :b do
requires :c
end
end
subject.get('/multiple') { declared(params).to_json }

get '/multiple'
expect(last_response.status).to eq(200)

get '/multiple', a: true
expect(last_response.status).to eq(200)

get '/multiple', b: true
expect(last_response.status).to eq(200)

get '/multiple', a: true, b: true
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('c is missing')

get '/multiple', a: true, b: true, c: true
expect(last_response.status).to eq(200)
end

it 'raises an error if the dependent parameter was never specified' do
expect do
subject.params do
Expand Down

0 comments on commit 87cd364

Please sign in to comment.