Module: Hyrax::BasedNearFieldBehavior
- Included in:
- Forms::ResourceForm
- Defined in:
- app/forms/concerns/hyrax/based_near_field_behavior.rb
Overview
Form-side handling for the based_near (location) controlled vocabulary
field. The form expects a ControlledVocabularies::Location object as
input and produces a hash like those used with accepts_nested_attributes_for.
The deserialize! override below is the canonical Field Behavior pattern
for nested-attribute properties: a module prepended onto every
ResourceForm subclass that strips its own key from the rewritten params
so Reform's from_hash doesn't write the raw _attributes payload to a
same-named property. Calling super first lets the chain compose — every
Field Behavior runs its own delete, then control falls through to Reform's
base deserialize! (the <name>_attributes rename pass).
Class Method Summary collapse
Instance Method Summary collapse
-
#deserialize!(params) ⇒ Object
Skipping based_near in deserialize avoids a race condition where it would otherwise end up in an inconsistent state during validation; the field is handled exclusively by the populator on
based_near_attributes.
Class Method Details
.included(descendant) ⇒ Object
15 16 17 |
# File 'app/forms/concerns/hyrax/based_near_field_behavior.rb', line 15 def self.included(descendant) descendant.property :based_near_attributes, virtual: true, populator: :based_near_attributes_populator, prepopulator: :based_near_attributes_prepopulator end |
Instance Method Details
#deserialize!(params) ⇒ Object
Skipping based_near in deserialize avoids a race condition where it
would otherwise end up in an inconsistent state during validation; the
field is handled exclusively by the populator on
based_near_attributes.
Override deserialize! (not deserialize) so the strip runs after
Reform's FormBuilderMethods#deserialize! has renamed
based_near_attributes to based_near, but before from_hash
reads property values from the params. Stripping in deserialize
would happen before the rename, and the rename would put the key
back; from_hash would then write raw fragment hashes onto the
form's based_near field, breaking the populator's contract.
Mutate params in place — never replace it. Reform's validate(params)
exposes the same hash via form.input_params, and downstream callers
(WorksControllerBehavior#update_valkyrie_work reading
form.input_params["permissions"]) read from that exact reference
after the rename.
37 38 39 40 41 42 43 44 |
# File 'app/forms/concerns/hyrax/based_near_field_behavior.rb', line 37 def deserialize!(params) result = super if result.respond_to?(:delete) result.delete('based_near') result.delete(:based_near) end result end |