Module: HtmlHelper

Defined in:
lib/vendor/lux/assets_helper.rb

Overview

export to all templates

asset ‘www/index.scss’

asset ‘www/index.coffee’

Instance Method Summary collapse

Instance Method Details

#asset(file, opts = {}) ⇒ Object

builds full asset path based on resource extension asset(‘js/main’) will render ‘app/assets/js/main/index.coffee’ as aset.path/assets/main-index-md5hash.js



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
53
54
55
56
57
58
59
# File 'lib/vendor/lux/assets_helper.rb', line 24

def asset file, opts={}
  opts = { dev_file: opts } unless opts.class == Hash

  # return joined assets if symbol given
  # = asset :main -> asset("css/main") + asset("js/main")
  return [asset("css/#{file}"), asset("js/#{file}")].join($/) if
    file.is_a?(Symbol)

  # return second link if it is defined and we are in dev mode
  return asset_include opts[:dev_file] if opts[:dev_file] && Lux.config(:compile_assets)

  # return internet links
  return asset_include file if file.starts_with?('/') || file.starts_with?('http')

  # return asset link in production or fail unless able
  unless Lux.config(:compile_assets)
    manifest = Lux.ram_cache('asset-manifest') { JSON.load Lux.root.join('public/manifest.json').read }
    mfile    = manifest['files'][file]

    raise 'Compiled asset link for "%s" not found in manifest.json' % file if mfile.empty?

    return asset_include(Lux.config.assets_root.to_s + mfile, opts)
  end

  # try to create list of incuded files and show every one of them
  data = LuxAssets.files(file).inject([]) do |total, asset|
    if asset.is_a?(Proc)
      tag_name = file.include?('css') ? :style : :script
      total.push({}.tag tag_name, asset.call)
    else
      total.push asset_include '/compiled_asset/' + asset
    end
  end

  data.map{ |it| it.sub(/^\s\s/,'') }.join("\n")
end

#asset_include(path, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/vendor/lux/assets_helper.rb', line 5

def asset_include path, opts={}
  raise ArgumentError.new("Asset path can't be empty") if path.empty?

  ext  = path.split('?').first.split('.').last
  type = ['css', 'sass', 'scss'].include?(ext) ? :style : :script
  type = :style if path.include?('fonts.googleapis.com')

  current.response.early_hints path, type

  if type == :style
    %[<link rel="stylesheet" href="#{path}" />]
  else
    %[<script src="#{path}"></script>]
  end
end