Module: Superglue::Rendering

Extended by:
ActiveSupport::Concern
Defined in:
lib/superglue/rendering.rb

Defined Under Namespace

Classes: UnsupportedOption

Instance Method Summary collapse

Instance Method Details

#_ensure_react_page!(template, prefixes) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/superglue/rendering.rb', line 74

def _ensure_react_page!(template, prefixes)
  found_template = lookup_context.find(template, prefixes, false, [], formats: [], handlers: [], variants: [], locale: [])
  ## This variable was created for props_template to pick up
  @_active_template_virtual_path = found_template.virtual_path
  found_template
rescue ActionView::MissingTemplate => e
  raise ActionView::MissingTemplate.new(e.paths, e.path, e.prefixes, e.partial, "extension JSX or TSX")
end

#_jsx_defaultsObject



66
67
68
# File 'lib/superglue/rendering.rb', line 66

def _jsx_defaults
  @_use_jsx_rendering_defaults && (request.format.html? || request.format.json?)
end

#_props_defaultsObject



70
71
72
# File 'lib/superglue/rendering.rb', line 70

def _props_defaults
  @_use_jsx_rendering_defaults && request.format.json?
end

#_render_template(options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/superglue/rendering.rb', line 19

def _render_template(options = {})
  if @_capture_options_before_render
    @_capture_options_before_render = false

    if options.keys.intersect? [:file, :partial, :body, :plain, :html, :inline]
      raise UnsupportedOption.new("`template:` and `action:` are the only options supported with `use_jsx_rendering_defaults`")
    end

    @_render_options = options
    _ensure_react_page!(options[:template], options[:prefixes]) if request.format.html?

    target_template_exists = template_exists?(options[:template], options[:prefixes], false)

    if request.format.html? && !target_template_exists
      # this uses the default superglue html template, which is super basic so
      # we don't' need to create a separate one for each view
      super(options.merge(
        template: _superglue_template,
        prefixes: []
      ))
    elsif request.format.json? && !target_template_exists
      # This fixes an issue with rendering json direclty but teh template doesn't exist
      # we still want ot see the layout rendered
      super(options.merge(
        inline: "",
        layout: _layout_for_option(true)
      ))
    else
      super
    end
  else
    super
  end
end

#default_renderObject



83
84
85
86
87
88
89
# File 'lib/superglue/rendering.rb', line 83

def default_render
  if _jsx_defaults
    render
  else
    super
  end
end

#renderObject



54
55
56
57
58
59
60
# File 'lib/superglue/rendering.rb', line 54

def render(...)
  if _jsx_defaults
    @_capture_options_before_render = true
  end

  super
end

#render_propsObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/superglue/rendering.rb', line 91

def render_props
  if @_render_options
    options = @_render_options

    if template_exists?(options[:template], options[:prefixes], formats: [:json])
      _render_template(options.merge({formats: [:json], layout: _layout_for_option(true)})).strip.html_safe
    else
      options.delete(:template)
      options.delete(:action)
      _render_template(options.merge({inline: "", formats: [:json], layout: _layout_for_option(true)})).strip.html_safe
    end
  else
    ""
  end
end

#use_jsx_rendering_defaultsObject



62
63
64
# File 'lib/superglue/rendering.rb', line 62

def use_jsx_rendering_defaults
  @_use_jsx_rendering_defaults = true
end