Module: ViewModel::ActiveRecord::CollectionNestedController
- Extended by:
- ActiveSupport::Concern
- Includes:
- NestedControllerBase
- Included in:
- Controller
- Defined in:
- lib/view_model/active_record/collection_nested_controller.rb
Overview
Inherits the following routes to manipulate children directly: POST /children #create – create or update without parent GET /children #index – list all child models regardless of parent GET /children/:id #show DELETE /children/:id #destroy
Instance Method Summary collapse
-
#append(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context, lock_owner: nil) ⇒ Object
Deserialize items of the associated type and append them to the owner’s collection.
- #disassociate(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context, lock_owner: nil) ⇒ Object
- #disassociate_all(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context, lock_owner: nil) ⇒ Object
- #index_associated(scope: nil, serialize_context: new_serialize_context, lock_owner: nil, &block) ⇒ Object
-
#replace(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context, lock_owner: nil, &block) ⇒ Object
Deserialize items of the associated type and associate them with the owner, replacing previously associated items.
- #replace_bulk(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context, &block) ⇒ Object
Instance Method Details
#append(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context, lock_owner: nil) ⇒ Object
Deserialize items of the associated type and append them to the owner’s collection.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/view_model/active_record/collection_nested_controller.rb', line 44 def append(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context, lock_owner: nil) assoc_view = nil pre_rendered = owner_viewmodel.transaction do update_hash, refs = parse_viewmodel_updates before = parse_relative_position(:before) after = parse_relative_position(:after) if before && after raise ViewModel::DeserializationError::InvalidSyntax.new('Can not provide both `before` and `after` anchors for a collection append') end owner_view = owner_viewmodel.find(owner_viewmodel_id, eager_include: false, lock: lock_owner) assoc_view = owner_view.append_associated(association_name, update_hash, references: refs, before: before, after: after, deserialize_context: deserialize_context) ViewModel::Callbacks.wrap_serialize(owner_view, context: serialize_context) do child_context = owner_view.context_for_child(association_name, context: serialize_context) ViewModel.preload_for_serialization(assoc_view) assoc_view = yield(assoc_view) if block_given? prerender_viewmodel(assoc_view, serialize_context: child_context) end end render_json_string(pre_rendered) assoc_view end |
#disassociate(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context, lock_owner: nil) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/view_model/active_record/collection_nested_controller.rb', line 75 def disassociate(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context, lock_owner: nil) owner_viewmodel.transaction do owner_view = owner_viewmodel.find(owner_viewmodel_id, eager_include: false, lock: lock_owner) owner_view.delete_associated(association_name, viewmodel_id, type: viewmodel_class, deserialize_context: deserialize_context) render_viewmodel(nil) end end |
#disassociate_all(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context, lock_owner: nil) ⇒ Object
38 39 40 |
# File 'lib/view_model/active_record/collection_nested_controller.rb', line 38 def disassociate_all(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context, lock_owner: nil) destroy_association(true, serialize_context: serialize_context, deserialize_context: deserialize_context, lock_owner: lock_owner) end |
#index_associated(scope: nil, serialize_context: new_serialize_context, lock_owner: nil, &block) ⇒ Object
24 25 26 |
# File 'lib/view_model/active_record/collection_nested_controller.rb', line 24 def index_associated(scope: nil, serialize_context: new_serialize_context, lock_owner: nil, &block) show_association(scope: scope, serialize_context: serialize_context, lock_owner: lock_owner, &block) end |
#replace(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context, lock_owner: nil, &block) ⇒ Object
Deserialize items of the associated type and associate them with the owner, replacing previously associated items.
30 31 32 |
# File 'lib/view_model/active_record/collection_nested_controller.rb', line 30 def replace(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context, lock_owner: nil, &block) write_association(serialize_context: serialize_context, deserialize_context: deserialize_context, lock_owner: lock_owner, &block) end |
#replace_bulk(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context, &block) ⇒ Object
34 35 36 |
# File 'lib/view_model/active_record/collection_nested_controller.rb', line 34 def replace_bulk(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context, &block) write_association_bulk(serialize_context: serialize_context, deserialize_context: deserialize_context, &block) end |