Module: Frails::Monkey::ActionView::AbstractRenderer

Defined in:
lib/frails/monkey/action_view/abstract_renderer.rb

Instance Method Summary collapse

Instance Method Details

#side_load_assets(view, tpl) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/frails/monkey/action_view/abstract_renderer.rb', line 7

def side_load_assets(view, tpl)
  path = tpl.short_identifier.delete_prefix('app/').delete_suffix('.html.erb')

  instrument :side_loaded_assets, identifier: tpl.identifier, asset_types: [] do |payload|
    side_load_javascript path, view, payload
    side_load_stylesheet path, view, payload
  end

  path
end

#side_load_javascript(path, view, payload) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/frails/monkey/action_view/abstract_renderer.rb', line 18

def side_load_javascript(path, view, payload)
  # Render the JS - if any.
  view.content_for :side_loaded_js do
    view.javascript_pack_tag(path, soft_lookup: true).tap do |tag|
      !tag.nil? && (payload[:asset_types] << :js)
    end
  end
end

#side_load_stylesheet(path, view, payload) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/frails/monkey/action_view/abstract_renderer.rb', line 27

def side_load_stylesheet(path, view, payload)
  loaded = side_loaded_stylesheets(view)

  # Don't inline the styles if already inlined.
  return if loaded.include?(path)

  # Render the CSS inline - if any.
  Frails.manifest.read(path, :stylesheet) do |href, src|
    view.content_for :side_loaded_css do
      view. :style, src, { data: { href: href } }, false
    end

    loaded << path
    payload[:asset_types] << :css
  end
end

#side_loaded_stylesheets(view) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/frails/monkey/action_view/abstract_renderer.rb', line 44

def side_loaded_stylesheets(view)
  if view.instance_variable_defined?(:@side_loaded_stylesheets)
    view.instance_variable_get(:@side_loaded_stylesheets)
  else
    view.instance_variable_set :@side_loaded_stylesheets, []
  end
end