Module: ViewModel::Controller

Extended by:
ActiveSupport::Concern
Included in:
ActiveRecord::ControllerBase
Defined in:
lib/view_model/controller.rb

Defined Under Namespace

Classes: CompiledJson

Instance Method Summary collapse

Instance Method Details

#prerender_json_view(json_view, json_references: {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/view_model/controller.rb', line 45

def prerender_json_view(json_view, json_references: {})
  json_view = wrap_json_view(json_view)
  json_references = wrap_json_view(json_references)

  encode_jbuilder do |json|
    json.data json_view
    if json_references.present?
      json.references do
        json_references.sort.each do |key, value|
          json.set!(key, value)
        end
      end
    end
    yield(json) if block_given?
  end
end

#prerender_viewmodel(viewmodel, status: nil, serialize_context: viewmodel.class.try(:new_serialize_context)) ⇒ Object

Render viewmodel(s) to a JSON API response as a String



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/view_model/controller.rb', line 21

def prerender_viewmodel(viewmodel, status: nil, serialize_context: viewmodel.class.try(:new_serialize_context))
  encode_jbuilder do |json|
    json.data do
      ViewModel.serialize(viewmodel, json, serialize_context: serialize_context)
    end

    if serialize_context && serialize_context.has_references?
      json.references do
        serialize_context.serialize_references(json)
      end
    end

    yield(json) if block_given?
  end
end

#render_error(error_view, status = 500) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/view_model/controller.rb', line 62

def render_error(error_view, status = 500)
  unless error_view.is_a?(ViewModel)
    raise "Expected ViewModel error view, received #{error_view.inspect}"
  end

  render_jbuilder(status: status) do |json|
    json.error do
      ctx = error_view.class.new_serialize_context(access_control: ViewModel::AccessControl::Open.new)
      ViewModel.serialize(error_view, json, serialize_context: ctx)
    end
  end
end

#render_json_view(json_view, json_references: {}, status: nil, &block) ⇒ Object

Render an arbitrarily nested tree of hashes and arrays with pre-rendered JSON string terminals. Useful for rendering cached views without parsing then re-serializing the cached JSON.



40
41
42
43
# File 'lib/view_model/controller.rb', line 40

def render_json_view(json_view, json_references: {}, status: nil, &block)
  prerender = prerender_json_view(json_view, json_references: json_references, &block)
  render_json_string(prerender, status: status)
end

#render_viewmodel(viewmodel, status: nil, serialize_context: viewmodel.class.try(:new_serialize_context), &block) ⇒ Object



15
16
17
18
# File 'lib/view_model/controller.rb', line 15

def render_viewmodel(viewmodel, status: nil, serialize_context: viewmodel.class.try(:new_serialize_context), &block)
  prerender = prerender_viewmodel(viewmodel, serialize_context: serialize_context, &block)
  render_json_string(prerender, status: status)
end