Module: Roda::RodaPlugins::Assets::InstanceMethods

Defined in:
lib/roda/plugins/assets.rb

Instance Method Summary collapse

Instance Method Details

#assets(type, attrs = nil) ⇒ Object

Return a string containing html tags for the given asset type. This will use a script tag for the :js type and a link tag for the :css type.

To return the tags for a specific asset group, use an array for the type, such as [:css, :frontend].

When the assets are not compiled, this will result in a separate tag for each asset file. When the assets are compiled, this will result in a single tag to the compiled asset file.



565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/roda/plugins/assets.rb', line 565

def assets(type, attrs = nil)
  o = self.class.assets_opts
  type, *dirs = type if type.is_a?(Array)
  stype = type.to_s

  attrs = if attrs
    ru = Rack::Utils
    attrs.map{|k,v| "#{k}=\"#{ru.escape_html(v.to_s)}\""}.join(SPACE)
  else
    EMPTY_STRING
  end

  if type == :js
    tag_start = "<script type=\"text/javascript\" #{attrs} src=\""
    tag_end = JS_END
  else
    tag_start = "<link rel=\"stylesheet\" #{attrs} href=\""
    tag_end = CSS_END
  end

  url_prefix = request.script_name if self.class.opts[:add_script_name]

  # Create a tag for each individual file
  if compiled = o[:compiled]
    asset_host = o[:compiled_asset_host]
    if dirs && !dirs.empty?
      key = dirs.join(DOT)
      ckey = "#{stype}.#{key}"
      if ukey = compiled[ckey]
        ukey = "#{key}.#{ukey}"
      end
    else
      ukey = compiled[stype]
    end

    if ukey
      "#{tag_start}#{asset_host}#{url_prefix}/#{o[:"compiled_#{stype}_prefix"]}.#{ukey}.#{stype}#{tag_end}"
    end
  else
    asset_dir = o[type]
    if dirs && !dirs.empty?
      dirs.each{|f| asset_dir = asset_dir[f]}
      prefix = "#{dirs.join(SLASH)}/" if o[:group_subdirs]
    end
    Array(asset_dir).map{|f| "#{tag_start}#{url_prefix}/#{o[:"#{stype}_prefix"]}#{prefix}#{f}#{o[:"#{stype}_suffix"]}#{tag_end}"}.join(NEWLINE)
  end
end

#read_asset_file(file, type) ⇒ Object

Return the content of the file if it is already of the correct type. Otherwise, render the file using the render plugin. file should be the relative path to the file from the current directory.



642
643
644
645
646
647
648
# File 'lib/roda/plugins/assets.rb', line 642

def read_asset_file(file, type)
  if file.end_with?(".#{type}")
    ::File.read(file)
  else
    render_asset_file(file, :template_opts=>self.class.assets_opts[:"#{type}_opts"])
  end
end

#render_asset(file, type) ⇒ Object

Render the asset with the given filename. When assets are compiled, or when the file is already of the given type (no rendering necessary), this returns the contents of the compiled file. When assets are not compiled and the file is not already of the correct, this will render the asset using the render plugin. In both cases, if the file has not been modified since the last request, this will return a 304 response.



620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# File 'lib/roda/plugins/assets.rb', line 620

def render_asset(file, type)
  o = self.class.assets_opts
  if o[:compiled]
    file = "#{o[:"compiled_#{type}_path"]}#{file}"

    if o[:gzip] && env[HTTP_ACCEPT_ENCODING] =~ /\bgzip\b/
      @_response[CONTENT_ENCODING] = GZIP
      file += DOTGZ
    end

    check_asset_request(file, type, ::File.stat(file).mtime)
    ::File.read(file)
  else
    file = "#{o[:"#{type}_path"]}#{file}"
    check_asset_request(file, type, asset_last_modified(file))
    read_asset_file(file, type)
  end
end