class ViewModel::ActiveRecord

def association(association_name,

(only applies to when +through+ is set).
- +through_order_attr+ the through model is ordered by the given attribute

ActiveRecord +has_many:through:+.
- +through+ names an ActiveRecord association that will be used like an

External associations may only be made to root viewmodels.
must be independently manipulated using `AssociationManipulation`.
associations are not included in (de)serializations of the parent, and
- +external+ indicates an association external to the view. Externalized

association
- +viewmodel+, +viewmodels+ specifies the viewmodel(s) to use for the

- +as+ sets the name of the association in the viewmodel

indirect reference, while a child viewmodel type will be directly nested.
An association to a root viewmodel type will be serialized with an

be inferred from the model name, or may be explicitly specified.
will be recursively (de)serialized by its own viewmodel type, which will
Adds an association from the model to this viewmodel. The associated model
def association(association_name,
                as: nil,
                viewmodel: nil,
                viewmodels: nil,
                external: false,
                read_only: false,
                through: nil,
                through_order_attr: nil)
  vm_association_name = (as || association_name).to_s
  if through
    direct_association_name   = through
    indirect_association_name = association_name
  else
    direct_association_name   = association_name
    indirect_association_name = nil
  end
  target_viewmodels = Array.wrap(viewmodel || viewmodels)
  association_data = AssociationData.new(
    owner:                     self,
    association_name:          vm_association_name,
    direct_association_name:   direct_association_name,
    indirect_association_name: indirect_association_name,
    target_viewmodels:         target_viewmodels,
    external:                  external,
    read_only:                 read_only,
    through_order_attr:        through_order_attr)
  _members[vm_association_name] = association_data
  @generated_accessor_module.module_eval do
    define_method vm_association_name do
      _read_association(vm_association_name)
    end
    define_method :"serialize_#{vm_association_name}" do |json, serialize_context: self.class.new_serialize_context|
      _serialize_association(vm_association_name, json, serialize_context: serialize_context)
    end
  end
end