From ee9c492e916e2d4865fda8e8031e887a6547c1e2 Mon Sep 17 00:00:00 2001 From: Kurakin Alexander Date: Sun, 25 Jan 2015 16:01:33 +0300 Subject: [PATCH] Unify Grape::DSL::Parameters#requires and #optional. Breaking changes maybe. --- lib/grape/dsl/parameters.rb | 14 +++++++------- lib/grape/validations/params_scope.rb | 3 +-- spec/grape/dsl/parameters_spec.rb | 22 +++++++++++----------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/grape/dsl/parameters.rb b/lib/grape/dsl/parameters.rb index 56d5f3b4cb..23e9c2cf2c 100644 --- a/lib/grape/dsl/parameters.rb +++ b/lib/grape/dsl/parameters.rb @@ -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) @@ -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) diff --git a/lib/grape/validations/params_scope.rb b/lib/grape/validations/params_scope.rb index 49565f50bc..bc2e97c95d 100644 --- a/lib/grape/validations/params_scope.rb +++ b/lib/grape/validations/params_scope.rb @@ -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 diff --git a/spec/grape/dsl/parameters_spec.rb b/spec/grape/dsl/parameters_spec.rb index cb8a3036be..651c2294d7 100644 --- a/spec/grape/dsl/parameters_spec.rb +++ b/spec/grape/dsl/parameters_spec.rb @@ -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 @@ -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 @@ -31,7 +31,7 @@ def validates(*args) end # rubocop:disable TrivialAccessors - def valids + def validates_reader @validates end # rubocop:enable TrivialAccessors @@ -57,8 +57,8 @@ 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 @@ -66,8 +66,8 @@ def valids 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 @@ -75,7 +75,7 @@ def valids 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 @@ -83,7 +83,7 @@ def valids 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 @@ -91,7 +91,7 @@ def valids 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 @@ -99,7 +99,7 @@ def valids 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