From 2c160a957f7f453c3056dcbc53175a593122492f Mon Sep 17 00:00:00 2001 From: "Josep M. Blanquer" Date: Wed, 10 Aug 2016 22:12:49 -0700 Subject: [PATCH 1/4] Fix corner case bug rendering nil subjects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Note: It would only manifest when include_nil is true and there’s an explicit subsection of fields. --- CHANGELOG.md | 1 + lib/praxis-blueprints/renderer.rb | 5 ++++- spec/praxis-blueprints/renderer_spec.rb | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 829f63e..1abbb64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Include Attributor::Dumpable in Blueprint, so it renders (semi) correctly if rendered with just `true` specified for fields. +* Fix bug rendering subobjects with nil values (manifested when `include_nil: true` there’s an explicit subsection of fields) ## 3.2 diff --git a/lib/praxis-blueprints/renderer.rb b/lib/praxis-blueprints/renderer.rb index 8299b73..b112683 100644 --- a/lib/praxis-blueprints/renderer.rb +++ b/lib/praxis-blueprints/renderer.rb @@ -79,7 +79,10 @@ def _render(object, fields, view = nil, context: Attributor::DEFAULT_ROOT_CONTEX raise Attributor::DumpError, context: context, name: key, type: object.class, original_exception: e end - next if value.nil? && !include_nil + if value.nil? + hash[key] = nil if self.include_nil + next + end if subfields == true hash[key] = case value diff --git a/spec/praxis-blueprints/renderer_spec.rb b/spec/praxis-blueprints/renderer_spec.rb index ca657ab..2770b56 100644 --- a/spec/praxis-blueprints/renderer_spec.rb +++ b/spec/praxis-blueprints/renderer_spec.rb @@ -100,6 +100,7 @@ context 'with include_nil: true' do let(:renderer) { Praxis::Renderer.new(include_nil: true) } + let(:address) { nil } it 'renders attributes with nil values' do output.should have_key :email @@ -108,6 +109,11 @@ output.should have_key :work_address output[:work_address].should be nil end + + it 'renders nil directly for nil subobjects' do + output.should have_key :address + output[:address].should be nil + end end context '#render_collection' do From f7489781c4d47efda41f370249a7f24697732f21 Mon Sep 17 00:00:00 2001 From: "Josep M. Blanquer" Date: Wed, 10 Aug 2016 22:35:21 -0700 Subject: [PATCH 2/4] Bump ruby 2.2.x version for travis. Signed-off-by: Josep M. Blanquer --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3a8e055..1938e08 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: ruby cache: bundler sudo: false rvm: - - 2.2.3 + - 2.2.5 - 2.3.1 script: - bundle exec rspec spec From cf5c6533b8d40a1ae7799e9f72f2847889d0596c Mon Sep 17 00:00:00 2001 From: "Josep M. Blanquer" Date: Wed, 10 Aug 2016 23:09:53 -0700 Subject: [PATCH 3/4] Use `.concat` to appease the cops a little bit. Signed-off-by: Josep M. Blanquer --- lib/praxis-blueprints/blueprint.rb | 4 ++-- lib/praxis-blueprints/field_expander.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/praxis-blueprints/blueprint.rb b/lib/praxis-blueprints/blueprint.rb index 18ff3cf..f6308d3 100644 --- a/lib/praxis-blueprints/blueprint.rb +++ b/lib/praxis-blueprints/blueprint.rb @@ -337,11 +337,11 @@ def validate(context = Attributor::DEFAULT_ROOT_CONTEXT) if value.respond_to?(:validating) # really, it's a thing with sub-attributes next if value.validating end - errors.push(*sub_attribute.validate(value, sub_context)) + errors.concat(sub_attribute.validate(value, sub_context)) end self.class.attribute.type.requirements.each do |req| validation_errors = req.validate(keys_with_values, context) - errors.push(*validation_errors) unless validation_errors.empty? + errors.concat(validation_errors) unless validation_errors.empty? end errors ensure diff --git a/lib/praxis-blueprints/field_expander.rb b/lib/praxis-blueprints/field_expander.rb index 6633e6f..f52171d 100644 --- a/lib/praxis-blueprints/field_expander.rb +++ b/lib/praxis-blueprints/field_expander.rb @@ -108,7 +108,7 @@ def expand_with_member_attribute(object, fields = true) new_fields = fields.is_a?(Array) ? fields[0] : fields result = [expand(object.member_attribute.type, new_fields)] - history[object][fields].push(*result) + history[object][fields].concat(result) result end From 62f12373855ea599a16697a8268eed3a00822542 Mon Sep 17 00:00:00 2001 From: "Josep M. Blanquer" Date: Wed, 10 Aug 2016 23:18:28 -0700 Subject: [PATCH 4/4] Try to appease the cops about method_missing. Signed-off-by: Josep M. Blanquer --- lib/praxis-blueprints/config_hash.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/praxis-blueprints/config_hash.rb b/lib/praxis-blueprints/config_hash.rb index f92f88b..ef90084 100644 --- a/lib/praxis-blueprints/config_hash.rb +++ b/lib/praxis-blueprints/config_hash.rb @@ -17,6 +17,10 @@ def to_hash @hash end + def respond_to_missing?(method_name, _include_private = false) + true || super # silly, but it'll appease rubocop + end + def method_missing(name, value, *rest, &block) if (existing = @hash[name]) if block