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
-
#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.
- #asset_include(path, opts = {}) ⇒ Object
-
#assets(*args) ⇒ Object
assets :vendor, :main.
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 60 61 |
# File 'lib/vendor/lux/assets_helper.rb', line 24 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 return asset_include(Lux.config.assets_root.to_s + mfile, opts) end end end |
#asset_include(path, opts = {}) ⇒ Object
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 |
#assets(*args) ⇒ Object
assets :vendor, :main
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/vendor/lux/assets_helper.rb', line 64 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 |