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



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
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/vendor/lux/assets_helper.rb', line 33

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

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

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

    # try to create list of incuded files and show every one of them
    files = LuxAssets.files(file) || []
    data = files.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")
  else
    # return asset link in production or fail unless able
    manifest = Lux.ram_cache('asset-manifest') { JSON.load Lux.root.join('public/manifest.json').read }
    mfile    = manifest['files'][file]

    if mfile.empty?
      unless opts[:raise].is_a?(FalseClass)
        raise 'Compiled asset link for "%s" not found in manifest.json' % file
      end

      nil
    else
      opts[:integrity] = manifest.dig('integrity', file)
      return asset_include(Lux.config.assets_root.to_s + mfile, opts)
    end
  end
end

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

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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

  data = {}

  data[:crossorigin] = opts[:crossorigin] || :anonymous
  data[:integrity]   = opts[:integrity] if opts[:integrity]

  if type == :style
    data[:media] = opts[:media] || :all
    data[:rel]   = :stylesheet
    data[:href]  = path
    data.tag :link
  else
    data[:src] = path
    data.tag :script
  end
end

#assets(*args) ⇒ Object

assets :vendor, :main



74
75
76
77
78
79
80
81
82
83
# File 'lib/vendor/lux/assets_helper.rb', line 74

def assets *args
  total = []
  [:css, :js].each do |ext|
    args.map do |group|
      data = asset("#{ext}/#{group}", raise: false)
      total.push data if data.present?
    end
  end
  total.join($/)
end