active_attr icon indicating copy to clipboard operation
active_attr copied to clipboard

MultiParameterAttributes

Open cgriego opened this issue 13 years ago • 8 comments

Handle ActionPack's multi-parameter params for complex attributes that come from date fields and such.

cgriego avatar Sep 29 '11 23:09 cgriego

The functionality for parsing this in ActiveRecord is hiding in Base, kicked off by assign_multiparameter_attributes.

cgriego avatar Nov 16 '11 04:11 cgriego

This functionality depends on typecasting, or at least knowing the type of the attribute.

cgriego avatar Nov 23 '11 22:11 cgriego

Any news on this? Would be awesome :)

stefanoverna avatar May 07 '12 21:05 stefanoverna

Has this been implemented yet?

ryanatwork avatar Aug 10 '12 21:08 ryanatwork

Are you looking for pull requests?

quinn avatar Jun 16 '13 19:06 quinn

Any updates on this?

lunks avatar Jun 09 '14 13:06 lunks

FWIW here's an implementation from one of our apps:

# spec/form_objects/form_object_base_spec.rb
require 'rails_helper'

describe FormObjectBase do
  class FooForm < FormObjectBase
    attribute :name
    attribute :cool_date, type: Date
    attribute :something_else, type: Date
  end

  it 'handles multiparameter assignment' do
    form = FooForm.new(
      'cool_date(1i)'      => '1993',
      'cool_date(2i)'      => '2',
      'cool_date(3i)'      => '27',
      'name'               => 'Doofy',
      'something_else(1i)' => '2014',
      'something_else(2i)' => nil,
      'something_else(3i)' => ''
    )

    expect(form.cool_date).to eq Date.new(1993, 2, 27)
    expect(form.name).to eq 'Doofy'
    expect(form.something_else).to be_nil
  end
end
# app/form_objects/form_object_base.rb
class FormObjectBase
  include ActiveAttr::Attributes
  include ActiveAttr::MassAssignment
  include ActiveAttr::BasicModel
  include ActiveAttr::TypecastedAttributes

  def assign_attributes(new_attributes, options = {})
    super(
      expand_multiparameter_attributes(new_attributes),
      options
    )
  end

  private

  def expand_multiparameter_attributes(attributes)
    single_parameter_attributes = {}
    multi_parameter_attributes = {}

    attributes.each do |key, value|
      matches = key.match(/^(?<key>[^\(]+)\((?<index>\d+)i\)$/)

      unless matches
        single_parameter_attributes[key] = value
        next
      end

      args = (multi_parameter_attributes[matches['key']] ||= [])
      args[matches['index'].to_i - 1] = (value.present? ? value.to_i : nil)
    end

    single_parameter_attributes.merge(
      multi_parameter_attributes.inject({}) do |hash, (key, args)|
        if args.all?(&:present?)
          hash.merge(key => _attribute_type(key).new(*args))
        else
          hash
        end
      end
    )
  end
end

boxofrad avatar Aug 18 '14 13:08 boxofrad

Whoops, left a bug in there (single parameter attributes would be lost) fixed and updated my comment :smile:

boxofrad avatar Aug 18 '14 13:08 boxofrad