app/controllers/step_controller.rb
# Converts multi-param Rails date attributes to an array that can be coerced more easily.
# Uses the `(1i)` part to know the position in the array (index 0 being nil).
#
# Example: {'birth_date(1i)' => '2008', 'birth_date(2i)' => '11', 'birth_date(3i)' => '22'}
# will be converted to an array attribute named `birth_date` with values: [nil, 2008, 11, 22]
#
Converts multi-param Rails date attributes to an array that can be coerced more easily.
Uses the `(1i)` part to know the position in the array (index 0 being nil).


Converts multi - param Rails date attributes to an array that can be coerced more easily . Uses the ` ( 1i ) ` part to know the position in the array ( index 0 being nil ) .
def normalise_date_attributes!(hash)
    regex = /(?<name>.+)\((?<index>[1-3])i\)$/ 
    new_hash = {}
    hash.each do |key, value|
      next unless key =~ regex
      hash.delete(key)
      name  = Regexp.last_match(:name)
      index = Regexp.last_match(:index).to_i
      new_hash[name] ||= []
      new_hash[name][index] = value.to_i
    end.merge!(
      new_hash
    )
  end
def normalise_date_attributes! ( hash ) regex = / (?<name>.+) \( (?<index>[1-3])i \) $ / new_hash = { } hash . each do | key , value | next unless key =~ regex hash . delete ( key ) name = Regexp . last_match ( :name ) index = Regexp . last_match ( :index ) . to_i new_hash [ name ] ||= [ ] new_hash [ name ] [ index ] = value . to_i end . merge! ( new_hash ) end
Converts multi-param Rails date attributes to an array that can be coerced more easily.
Converts multi - param Rails date attributes to an array that can be coerced more easily .
# captures the attribute name and index (1 to 3)
hash