Class: ViewModel::ActiveRecord::NestedControllerBase::ParentProxyModel

Inherits:
ViewModel
  • Object
show all
Defined in:
lib/view_model/active_record/nested_controller_base.rb

Constant Summary

Constants inherited from ViewModel

BULK_UPDATES_ATTRIBUTE, BULK_UPDATE_ATTRIBUTE, BULK_UPDATE_TYPE, ID_ATTRIBUTE, MIGRATED_ATTRIBUTE, NEW_ATTRIBUTE, REFERENCE_ATTRIBUTE, TYPE_ATTRIBUTE, VERSION_ATTRIBUTE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ViewModel

#==, accepts_schema_version?, add_view_alias, attribute, attributes, #blame_reference, #context_for_child, deserialize_context_class, deserialize_from_view, deserialize_members_from_view, eager_includes, encode_json, extract_reference_metadata, extract_reference_only_metadata, extract_viewmodel_metadata, #hash, #id, inherited, initialize_as_viewmodel, is_update_hash?, lock_attribute_inheritance, member_names, #model, new_deserialize_context, new_serialize_context, #preload_for_serialization, preload_for_serialization, root!, root?, schema_hash, schema_versions, serialize, serialize_as_reference, serialize_context_class, serialize_from_cache, serialize_to_hash, #serialize_to_hash, #serialize_view, #stable_id?, #to_json, #to_reference, #validate!, #view_name

Constructor Details

#initialize(parent, association_data, changed_children) ⇒ ParentProxyModel

Returns a new instance of ParentProxyModel.



16
17
18
19
20
# File 'lib/view_model/active_record/nested_controller_base.rb', line 16

def initialize(parent, association_data, changed_children)
  @parent = parent
  @association_data = association_data
  @changed_children = changed_children
end

Instance Attribute Details

#association_dataObject (readonly)

Returns the value of attribute association_data.



14
15
16
# File 'lib/view_model/active_record/nested_controller_base.rb', line 14

def association_data
  @association_data
end

#changed_childrenObject (readonly)

Returns the value of attribute changed_children.



14
15
16
# File 'lib/view_model/active_record/nested_controller_base.rb', line 14

def changed_children
  @changed_children
end

#parentObject (readonly)

Returns the value of attribute parent.



14
15
16
# File 'lib/view_model/active_record/nested_controller_base.rb', line 14

def parent
  @parent
end

Instance Method Details

#serialize(json, serialize_context:) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/view_model/active_record/nested_controller_base.rb', line 22

def serialize(json, serialize_context:)
  ViewModel::Callbacks.wrap_serialize(parent, context: serialize_context) do
    child_context = parent.context_for_child(association_data.association_name, context: serialize_context)

    json.set!(ViewModel::ID_ATTRIBUTE, parent.id)
    json.set!(ViewModel::BULK_UPDATE_ATTRIBUTE) do
      if association_data.referenced? && !association_data.owned?
        if association_data.collection?
          json.array!(changed_children) do |child|
            ViewModel.serialize_as_reference(child, json, serialize_context: child_context)
          end
        else
          ViewModel.serialize_as_reference(changed_children, json, serialize_context: child_context)
        end
      else
        ViewModel.serialize(changed_children, json, serialize_context: child_context)
      end
    end
  end
end