Module: ViewModel::ActiveRecord::Controller
- Extended by:
- ActiveSupport::Concern
- Includes:
- CollectionNestedController, ControllerBase, SingularNestedController, Controller::MigrationVersions
- Defined in:
- lib/view_model/active_record/controller.rb
Overview
Controller for accessing an ViewModel::ActiveRecord Provides for the following routes: POST /models #create – create or update one or more models GET /models #index GET /models/:id #show DELETE /models/:id #destroy
Defined Under Namespace
Classes: ViewmodelIdSerializer
Constant Summary
Constants included from Controller::MigrationVersions
Controller::MigrationVersions::MIGRATION_VERSION_HEADER
Instance Method Summary collapse
- #create(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context) ⇒ Object
- #destroy(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context) ⇒ Object
- #index(scope: nil, viewmodel_class: self.viewmodel_class, serialize_context: new_serialize_context(viewmodel_class: viewmodel_class)) ⇒ Object
- #parse_viewmodel_updates ⇒ Object
- #prerender_viewmodel ⇒ Object
- #show(scope: nil, viewmodel_class: self.viewmodel_class, serialize_context: new_serialize_context(viewmodel_class: viewmodel_class)) ⇒ Object
Methods included from Controller::MigrationVersions
#migration_versions, #specified_migration_versions
Methods included from SingularNestedController
#create_associated, #create_associated_bulk, #destroy_associated, #show_associated
Methods included from CollectionNestedController
#append, #disassociate, #disassociate_all, #index_associated, #replace, #replace_bulk
Methods included from Controller
#prerender_json_view, #render_error, #render_json_view, #render_viewmodel
Instance Method Details
#create(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/view_model/active_record/controller.rb', line 44 def create(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context) update_hash, refs = parse_viewmodel_updates view = nil pre_rendered = viewmodel_class.transaction do view = viewmodel_class.deserialize_from_view(update_hash, references: refs, deserialize_context: deserialize_context) ViewModel.preload_for_serialization(view) view = yield(view) if block_given? prerender_viewmodel(view, serialize_context: serialize_context) end render_json_string(pre_rendered) view end |
#destroy(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/view_model/active_record/controller.rb', line 58 def destroy(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context) viewmodel_ids = parse_param( :id, with: IknowParams::Serializer::ArrayOf.new(ViewmodelIdSerializer, allow_singleton: true)) viewmodel_class.transaction do views = viewmodel_class.find(viewmodel_ids, eager_include: false) views.each do |view| view.destroy!(deserialize_context: deserialize_context) end end render_viewmodel(nil) end |
#index(scope: nil, viewmodel_class: self.viewmodel_class, serialize_context: new_serialize_context(viewmodel_class: viewmodel_class)) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/view_model/active_record/controller.rb', line 33 def index(scope: nil, viewmodel_class: self.viewmodel_class, serialize_context: new_serialize_context(viewmodel_class: viewmodel_class)) views = nil pre_rendered = viewmodel_class.transaction do views = viewmodel_class.load(scope: scope) views = yield(views) if block_given? prerender_viewmodel(views, serialize_context: serialize_context) end render_json_string(pre_rendered) views end |
#parse_viewmodel_updates ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/view_model/active_record/controller.rb', line 75 def parse_viewmodel_updates super.tap do |update_hash, refs| if migration_versions.present? migrator = ViewModel::UpMigrator.new(migration_versions) migrator.migrate!({ 'data' => update_hash, 'references' => refs }) end end end |
#prerender_viewmodel ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/view_model/active_record/controller.rb', line 84 def prerender_viewmodel(...) super do |jbuilder| yield(jbuilder) if block_given? # migrate the resulting structure before it's serialized to a json string if migration_versions.present? tree = jbuilder.attributes! migrator = ViewModel::DownMigrator.new(migration_versions) migrator.migrate!(tree) end end end |
#show(scope: nil, viewmodel_class: self.viewmodel_class, serialize_context: new_serialize_context(viewmodel_class: viewmodel_class)) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/view_model/active_record/controller.rb', line 22 def show(scope: nil, viewmodel_class: self.viewmodel_class, serialize_context: new_serialize_context(viewmodel_class: viewmodel_class)) view = nil pre_rendered = viewmodel_class.transaction do view = viewmodel_class.find(viewmodel_id, scope: scope) view = yield(view) if block_given? prerender_viewmodel(view, serialize_context: serialize_context) end render_json_string(pre_rendered) view end |