module Roda::RodaPlugins::Assets::InstanceMethods

Public Instance Methods

assets(folder, options = {}) click to toggle source

This will ouput the files with the appropriate tags

# File lib/roda/plugins/assets.rb, line 120
def assets folder, options = {}
  attrs   = options.map{|k,v| "#{k}=\"#{v}\""}
  tags    = []
  folder  = [folder] unless folder.is_a? Array
  type    = folder.first
  attr    = type.to_s == 'js' ? 'src' : 'href'
  path    = "#{assets_opts[:route]}/#{assets_opts[:"#{type}_folder"]}"
  files   = folder.length == 1 \
          ? assets_opts[:"#{folder[0]}"] \
          : assets_opts[:"#{folder[0]}"][:"#{folder[1]}"]

  # Create a tag for each individual file
  if assets_opts[:compiled] || assets_opts[:concat]
    name = assets_opts[:compiled] ? assets_opts[:compiled_name] : assets_opts[:concat_name]
    name = "#{name}/#{folder.join('-')}"
    # Generate unique url so middleware knows to check for # compile/concat
    attrs.unshift("#{attr}=\"/#{path}/#{name}/#{assets_unique_id(type)}.#{type}\"")
    # Return tag string
    send("#{type}_assets_tag", attrs.join(' '))
  else
    files.each do |file|
      # This allows you to do things like:
      # assets_opts[:css] = ['app', './bower/jquery/jquery-min.js']
      file.gsub!(/\./, '$2E')
      # Add tags to the tags array
      tags << send(
        "#{type}_assets_tag",
        attrs.dup.unshift( "#{attr}=\"/#{path}/#{file}.#{type}\"").join(' ')
      )
    end
    # Return tags as string
    tags.join "\n"
  end
end
assets_opts() click to toggle source

Shortcut for class opts

# File lib/roda/plugins/assets.rb, line 221
def assets_opts
  self.class.assets_opts
end
assets_unique_id(*args) click to toggle source

Shortcut for class assets unique id

# File lib/roda/plugins/assets.rb, line 226
def assets_unique_id(*args)
  self.class.assets_unique_id(*args)
end
read_asset_file(file, type) click to toggle source
# File lib/roda/plugins/assets.rb, line 184
def read_asset_file(file, type)
  folder = assets_opts[:"#{type}_folder"]

  # If there is no file it must be a remote file request.
  # Lets set the file to the url
  if file == ''
    route = assets_opts[:route]
    file  = env['SCRIPT_NAME'].gsub(/^\/#{route}\/#{folder}\//, '')
  end

  # If it's not a url or parent direct append the full path
  if !file[/^\.\//] && !file[/^http/]
    file = assets_opts[:path] + '/' + folder + "/#{file}"
  end

  # set the current engine
  engine = assets_opts[:"#{type}_engine"]

  if File.exists? "#{file}.#{engine}"
    # render via tilt
    render path: "#{file}.#{engine}"
  elsif File.exists? "#{file}.#{type}"
    # read file directly
    File.read "#{file}.#{type}"
  elsif file[/^http/]
    # grab remote file content
    Net::HTTP.get(URI.parse(file))
  elsif File.exists?(file) && !file[/\.#{type}$/]
    # Render via tilt if the type isn't css or js
    render path: file
  else
    # if it is css/js read the file directly
    File.read file
  end
end
render_asset(file, type) click to toggle source
# File lib/roda/plugins/assets.rb, line 155
def render_asset(file, type)
  # convert back url safe to period
  file.gsub!(/(\$2E|%242E)/, '.')

  if !assets_opts[:compiled] && !assets_opts[:concat]
    read_asset_file file, type
  elsif assets_opts[:compiled]
    folder = file.split('/')[1].split('-', 2)
    path   = assets_opts[:compiled_path] \
           + "/#{assets_opts[:"#{type}_folder"]}/" \
           + assets_opts[:compiled_name] \
           + (folder.length > 1 ? ".#{folder[1]}" : '') \
           + ".#{type}"

    File.read path
  elsif assets_opts[:concat]
    # "concat.roda.assets/css/123"
    content   = ''
    folder = file.split('/')[1].split('-', 2)
    files  = folder.length == 1 \
            ? assets_opts[:"#{folder[0]}"] \
            : assets_opts[:"#{folder[0]}"][:"#{folder[1]}"]

    files.each { |f| content += read_asset_file f, type }

    content
  end
end

Private Instance Methods

css_assets_tag(attrs) click to toggle source

CSS tag template <link rel=“stylesheet” href=“theme.css”>

# File lib/roda/plugins/assets.rb, line 234
def css_assets_tag(attrs)
  "<link rel=\"stylesheet\" #{attrs} />"
end
js_assets_tag(attrs) click to toggle source

JS tag template <script src=“scriptfile.js”></script>

# File lib/roda/plugins/assets.rb, line 240
def js_assets_tag(attrs)
  "<script type=\"text/javascript\" #{attrs}></script>"
end