From 8121b200aa7e73b888216cee03e98655600b310a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Tue, 20 Mar 2018 15:05:24 -0300 Subject: [PATCH 1/5] Use latest ruby in `.ruby-version` --- .ruby-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ruby-version b/.ruby-version index 5859406..73462a5 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.2.3 +2.5.1 From d48cf30a8868fd7c6cb0edb0f156c229ccce4265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Tue, 20 Mar 2018 17:14:57 -0300 Subject: [PATCH 2/5] Fix nested indexed arrays support Follow up to 862d4dfd3b56d30b95a05f25bb9255a37cbdb1da. --- lib/rectify/format_attributes_hash.rb | 20 ++++++++++++++---- spec/fixtures/forms/contact_form.rb | 3 +++ spec/fixtures/forms/phone_form.rb | 4 ++++ spec/lib/rectify/form_spec.rb | 30 +++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 spec/fixtures/forms/phone_form.rb diff --git a/lib/rectify/format_attributes_hash.rb b/lib/rectify/format_attributes_hash.rb index c1e5a29..23e4b67 100644 --- a/lib/rectify/format_attributes_hash.rb +++ b/lib/rectify/format_attributes_hash.rb @@ -16,16 +16,28 @@ def format(params) attr_reader :attribute_set def convert_indexed_hashes_to_arrays(attributes_hash) - array_attribute_names.each do |name| + array_attributes.each do |array_attribute| + name = array_attribute.name attribute = attributes_hash[name] next unless attribute.is_a?(Hash) - attributes_hash[name] = attribute.values + attributes_hash[name] = transform_values_for_type( + attribute.values, + array_attribute.member_type.primitive + ) end end - def array_attribute_names - attribute_set.select { |a| a.primitive == Array }.map { |a| a.name.to_s } + def transform_values_for_type(values, element_type) + return values unless element_type < Rectify::Form + + values.map do |value| + self.class.new(element_type.attribute_set).format(value) + end + end + + def array_attributes + attribute_set.select { |a| a.primitive == Array } end def convert_hash_keys(value) diff --git a/spec/fixtures/forms/contact_form.rb b/spec/fixtures/forms/contact_form.rb index aff160f..3983cab 100644 --- a/spec/fixtures/forms/contact_form.rb +++ b/spec/fixtures/forms/contact_form.rb @@ -1,6 +1,9 @@ +require_relative "phone_form" + class ContactForm < Rectify::Form attribute :name, String attribute :number, String + attribute :phones, Array[PhoneForm] validates :name, :presence => true end diff --git a/spec/fixtures/forms/phone_form.rb b/spec/fixtures/forms/phone_form.rb new file mode 100644 index 0000000..0a18214 --- /dev/null +++ b/spec/fixtures/forms/phone_form.rb @@ -0,0 +1,4 @@ +class PhoneForm < Rectify::Form + attribute :number, String + attribute :country_code, String +end diff --git a/spec/lib/rectify/form_spec.rb b/spec/lib/rectify/form_spec.rb index c32f14b..3a90c49 100644 --- a/spec/lib/rectify/form_spec.rb +++ b/spec/lib/rectify/form_spec.rb @@ -125,6 +125,36 @@ expect(form.contacts[2].number).to eq("789") end + it "populates nested indexed arrays of attributes" do + params = ActionController::Parameters.new( + "user" => { + "contacts" => { + "0" => { + "name" => "Amber", + "number" => "123", + "phones" => { + "0" => { "number" => "111111111", "country_code" => "+34" }, + "1" => { "number" => "222222222", "country_code" => "+34" } + } + }, + "1" => { "name" => "Megan", "number" => "456" } + } + } + ) + + form = UserForm.from_params(params) + + expect(form.contacts).to have(2).items + expect(form.contacts[0].name).to eq("Amber") + expect(form.contacts[0].number).to eq("123") + expect(form.contacts[0].phones[0].number).to eq("111111111") + expect(form.contacts[0].phones[0].country_code).to eq("+34") + expect(form.contacts[0].phones[1].number).to eq("222222222") + expect(form.contacts[0].phones[1].country_code).to eq("+34") + expect(form.contacts[1].name).to eq("Megan") + expect(form.contacts[1].number).to eq("456") + end + it "populates a derived form" do params["user"]["school"] = "Rutlish" From 0ddc4f18ff3f8b3aa62f47d815b2224224a4408a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Tue, 20 Mar 2018 17:44:36 -0300 Subject: [PATCH 3/5] Test against the latest rubies --- .travis.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4931a64..dc37ee0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: ruby rvm: - - "2.2.2" - - "2.3.0" + - "2.2.10" + - "2.3.7" + - "2.4.4" - "2.5.1" script: bundle exec rspec From 644e37085cf84bdd60702cd91816e88e03bb4ea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Tue, 20 Mar 2018 18:08:41 -0300 Subject: [PATCH 4/5] Regenerate rubocop TODO config --- .rubocop.yml | 2 ++ .rubocop_todo.yml | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 .rubocop_todo.yml diff --git a/.rubocop.yml b/.rubocop.yml index 776073f..57f9486 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,3 +1,5 @@ +inherit_from: .rubocop_todo.yml + AllCops: Exclude: - 'bin/**/*' diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..ff401a2 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,68 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2018-05-27 10:45:35 -0300 using RuboCop version 0.56.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +# Cop supports --auto-correct. +Lint/UnneededCopDisableDirective: + Exclude: + - 'lib/rectify/command.rb' + +# Offense count: 6 +# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames. +# AllowedNames: io, id, to, by, on, in, at +Naming/UncommunicativeMethodParamName: + Exclude: + - 'lib/rectify/format_attributes_hash.rb' + - 'lib/rectify/rspec/database_reporter/reporter.rb' + - 'spec/fixtures/command/args_command.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Performance/RegexpMatch: + Exclude: + - 'lib/rectify/rspec/matchers.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +Style/ExpandPathArguments: + Exclude: + - 'rectify.gemspec' + - 'spec/spec_helper.rb' + +# Offense count: 73 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: when_needed, always, never +Style/FrozenStringLiteralComment: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +Style/IfUnlessModifier: + Exclude: + - 'lib/rectify/rspec/matchers.rb' + +# Offense count: 1 +Style/MethodMissingSuper: + Exclude: + - 'lib/rectify/command.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: AutoCorrect, EnforcedStyle. +# SupportedStyles: predicate, comparison +Style/NumericPredicate: + Exclude: + - 'spec/**/*' + - 'lib/rectify/form.rb' + +# Offense count: 1 +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. +# URISchemes: http, https +Metrics/LineLength: + Max: 100 From 2e403128161931f1522231b2fa46dc8e77978be9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Tue, 20 Mar 2018 18:13:20 -0300 Subject: [PATCH 5/5] Fix reek issue --- lib/rectify/format_attributes_hash.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rectify/format_attributes_hash.rb b/lib/rectify/format_attributes_hash.rb index 23e4b67..b9533a5 100644 --- a/lib/rectify/format_attributes_hash.rb +++ b/lib/rectify/format_attributes_hash.rb @@ -37,7 +37,7 @@ def transform_values_for_type(values, element_type) end def array_attributes - attribute_set.select { |a| a.primitive == Array } + attribute_set.select { |attribute| attribute.primitive == Array } end def convert_hash_keys(value)