active_attr
active_attr copied to clipboard
MultiParameterAttributes
Handle ActionPack's multi-parameter params for complex attributes that come from date fields and such.
The functionality for parsing this in ActiveRecord is hiding in Base, kicked off by assign_multiparameter_attributes.
This functionality depends on typecasting, or at least knowing the type of the attribute.
Any news on this? Would be awesome :)
Has this been implemented yet?
Are you looking for pull requests?
Any updates on this?
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
Whoops, left a bug in there (single parameter attributes would be lost) fixed and updated my comment :smile: