Module: ViewModel::ActiveRecord::Controller

Extended by:
ActiveSupport::Concern
Includes:
CollectionNestedController, ControllerBase, SingularNestedController
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

Constant Summary collapse

MIGRATION_VERSION_HEADER =
'X-ViewModel-Versions'

Instance Method Summary collapse

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
# File 'lib/view_model/active_record/controller.rb', line 58

def destroy(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context)
  viewmodel_class.transaction do
    view = viewmodel_class.find(viewmodel_id, eager_include: false)
    view.destroy!(deserialize_context: deserialize_context)
  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_updatesObject



70
71
72
73
74
75
76
77
# File 'lib/view_model/active_record/controller.rb', line 70

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_viewmodelObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/view_model/active_record/controller.rb', line 79

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