Class: Proscenium::SideLoad

Inherits:
Object show all
Defined in:
lib/proscenium/side_load.rb

Defined Under Namespace

Modules: Controller

Constant Summary collapse

JS_COMMENT =
'<!-- [PROSCENIUM_JAVASCRIPTS] -->'
CSS_COMMENT =
'<!-- [PROSCENIUM_STYLESHEETS] -->'
LAZY_COMMENT =
'<!-- [PROSCENIUM_LAZY_SCRIPTS] -->'

Class Method Summary collapse

Class Method Details

.sideload_inheritance_chain(obj, options) ⇒ Object

Side loads the class, and its super classes that respond to ‘.source_path`.

Set the ‘abstract_class` class variable to true in any class, and it will not be side loaded.

If the class responds to ‘.sideload`, it will be called instead of the regular side loading. You can use this to customise what is side loaded.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/proscenium/side_load.rb', line 141

def sideload_inheritance_chain(obj, options)
  return unless Proscenium.config.side_load

  options = {} if options.nil?
  options = { js: options, css: options } unless options.is_a?(Hash)

  unless obj.sideload_assets_options.nil?
    tpl_options = obj.sideload_assets_options
    options = if tpl_options.is_a?(Hash)
                options.deep_merge tpl_options
              else
                { js: tpl_options, css: tpl_options }
              end
  end

  %i[css js].each do |k|
    options[k] = obj.instance_eval(&options[k]) if options[k].is_a?(Proc)
  end

  css_imports = []

  klass = obj.class
  while klass.respond_to?(:source_path) && klass.source_path && !klass.abstract_class
    if klass.respond_to?(:sideload)
      klass.sideload options
    elsif options[:css] == false
      Importer.sideload klass.source_path, **options
    else
      Importer.sideload_js klass.source_path, **options
      css_imports << klass.source_path
    end

    klass = klass.superclass
  end

  # The reason why we sideload CSS after JS is because the order of CSS is important.
  # Basically, the layout should be loaded before the view so that CSS cascading works i9n the
  # right direction.
  css_imports.reverse_each do |it|
    Importer.sideload_css it, **options
  end
end