Class: AttrJson::NestedAttributes::MultiparameterAttributeWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/attr_json/nested_attributes/multiparameter_attribute_writer.rb

Overview

Rails has a weird "multiparameter attribute" thing, that is used for simple_form's date/time html entry (datetime may be ALL it's ever been used for in Rails!), using weird parameters in the HTTP query params like "dateattribute(2i)". It is weird code, and I do NOT really understand the implementation, but it's also very low-churn, hasn't changed much in recent Rails history.

In Rails at present it's only on ActiveRecord, we need it used on our AttrJson::Models too, so we copy and paste extract it here, from: https://github.com/rails/rails/blob/42a16a4d6514f28e05f1c22a5f9125d194d9c7cb/activerecord/lib/active_record/attribute_assignment.rb

We only use it in the #{attr_name}_attributes= methods added by AttrJson::NestedAttributes, that's enough to get what we need for support of this stuff in our stuff, for form submisisons using rails-style date/time inputs as used eg in simple_form. And then we don't need to polute anything outside of NestedAttributes module with this crazy stuff.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ MultiparameterAttributeWriter

Returns a new instance of MultiparameterAttributeWriter.



21
22
23
# File 'lib/attr_json/nested_attributes/multiparameter_attribute_writer.rb', line 21

def initialize(model)
  @model = model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



20
21
22
# File 'lib/attr_json/nested_attributes/multiparameter_attribute_writer.rb', line 20

def model
  @model
end

Instance Method Details

#assign_multiparameter_attributes(pairs) ⇒ Object

Copied from Rails. https://github.com/rails/rails/blob/42a16a4d6514f28e05f1c22a5f9125d194d9c7cb/activerecord/lib/active_record/attribute_assignment.rb#L39

Instantiates objects for all attribute classes that needs more than one constructor parameter. This is done by calling new on the column type or aggregation type (through composed_of) object with these parameters. So having the pairs written_on(1) = "2004", written_on(2) = "6", written_on(3) = "24", will instantiate written_on (a date type) with Date.new("2004", "6", "24"). You can also specify a typecast character in the parentheses to have the parameters typecasted before they're used in the constructor. Use i for Integer and f for Float. If all the values for a given attribute are empty, the attribute will be set to +nil+.



33
34
35
36
37
# File 'lib/attr_json/nested_attributes/multiparameter_attribute_writer.rb', line 33

def assign_multiparameter_attributes(pairs)
  execute_callstack_for_multiparameter_attributes(
    extract_callstack_for_multiparameter_attributes(pairs)
  )
end