Skip to content

Commit

Permalink
Merge pull request #910 from kuraga/requires-optional
Browse files Browse the repository at this point in the history
Unify Grape::DSL::Parameters#requires and #optional. Breaking changes maybe
  • Loading branch information
dblock committed Jan 25, 2015
2 parents 2ab5266 + ee9c492 commit f3fe0eb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
14 changes: 7 additions & 7 deletions lib/grape/dsl/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ def use(*names)
def requires(*attrs, &block)
orig_attrs = attrs.clone

opts = attrs.last.is_a?(Hash) ? attrs.pop : nil
opts = attrs.last.is_a?(Hash) ? attrs.pop : {}
opts.merge!(presence: true)

if opts && opts[:using]
if opts[:using]
require_required_and_optional_fields(attrs.first, opts)
else
validate_attributes(attrs, opts, &block)
Expand All @@ -34,12 +35,11 @@ def requires(*attrs, &block)
end

def optional(*attrs, &block)
orig_attrs = attrs
orig_attrs = attrs.clone

opts = attrs.last.is_a?(Hash) ? attrs.pop : {}

validations = {}
validations.merge!(attrs.pop) if attrs.last.is_a?(Hash)
validations[:type] ||= Array if block_given?
validates(attrs, validations)
validate_attributes(attrs, opts, &block)

block_given? ? new_scope(orig_attrs, true, &block) :
push_declared_params(attrs)
Expand Down
3 changes: 1 addition & 2 deletions lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ def require_required_and_optional_fields(context, opts)
end

def validate_attributes(attrs, opts, &block)
validations = { presence: true }
validations.merge!(opts) if opts
validations = opts.clone
validations[:type] ||= Array if block
validates(attrs, validations)
end
Expand Down
22 changes: 11 additions & 11 deletions spec/grape/dsl/parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def validate_attributes(*args)
end

# rubocop:disable TrivialAccessors
def validate_attrs
def validate_attributes_reader
@validate_attributes
end
# rubocop:enable TrivialAccessors
Expand All @@ -21,7 +21,7 @@ def push_declared_params(*args)
end

# rubocop:disable TrivialAccessors
def push_declared_paras
def push_declared_params_reader
@push_declared_params
end
# rubocop:enable TrivialAccessors
Expand All @@ -31,7 +31,7 @@ def validates(*args)
end

# rubocop:disable TrivialAccessors
def valids
def validates_reader
@validates
end
# rubocop:enable TrivialAccessors
Expand All @@ -57,49 +57,49 @@ def valids
it 'adds a required parameter' do
subject.requires :id, type: Integer, desc: 'Identity.'

expect(subject.validate_attrs).to eq([[:id], { type: Integer, desc: 'Identity.' }])
expect(subject.push_declared_paras).to eq([[:id]])
expect(subject.validate_attributes_reader).to eq([[:id], { type: Integer, desc: 'Identity.', presence: true }])
expect(subject.push_declared_params_reader).to eq([[:id]])
end
end

describe '#optional' do
it 'adds an optional parameter' do
subject.optional :id, type: Integer, desc: 'Identity.'

expect(subject.valids).to eq([[:id], { type: Integer, desc: 'Identity.' }])
expect(subject.push_declared_paras).to eq([[:id]])
expect(subject.validate_attributes_reader).to eq([[:id], { type: Integer, desc: 'Identity.' }])
expect(subject.push_declared_params_reader).to eq([[:id]])
end
end

describe '#mutually_exclusive' do
it 'adds an mutally exclusive parameter validation' do
subject.mutually_exclusive :media, :audio

expect(subject.valids).to eq([[:media, :audio], { mutual_exclusion: true }])
expect(subject.validates_reader).to eq([[:media, :audio], { mutual_exclusion: true }])
end
end

describe '#exactly_one_of' do
it 'adds an exactly of one parameter validation' do
subject.exactly_one_of :media, :audio

expect(subject.valids).to eq([[:media, :audio], { exactly_one_of: true }])
expect(subject.validates_reader).to eq([[:media, :audio], { exactly_one_of: true }])
end
end

describe '#at_least_one_of' do
it 'adds an at least one of parameter validation' do
subject.at_least_one_of :media, :audio

expect(subject.valids).to eq([[:media, :audio], { at_least_one_of: true }])
expect(subject.validates_reader).to eq([[:media, :audio], { at_least_one_of: true }])
end
end

describe '#all_or_none_of' do
it 'adds an all or none of parameter validation' do
subject.all_or_none_of :media, :audio

expect(subject.valids).to eq([[:media, :audio], { all_or_none_of: true }])
expect(subject.validates_reader).to eq([[:media, :audio], { all_or_none_of: true }])
end
end

Expand Down

0 comments on commit f3fe0eb

Please sign in to comment.