Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested arrays support #45

Merged
merged 5 commits into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
inherit_from: .rubocop_todo.yml

AllCops:
Exclude:
- 'bin/**/*'
Expand Down
68 changes: 68 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.3
2.5.1
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
20 changes: 16 additions & 4 deletions lib/rectify/format_attributes_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 { |attribute| attribute.primitive == Array }
end

def convert_hash_keys(value)
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/forms/contact_form.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions spec/fixtures/forms/phone_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class PhoneForm < Rectify::Form
attribute :number, String
attribute :country_code, String
end
30 changes: 30 additions & 0 deletions spec/lib/rectify/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down