Sha256: 6fc9b73c0486bd749310b9f4b7fc626752e3ca892bc82d1a41eef27ef0d1bcb4

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

require 'oj'
require "sinatra/asset_helpers/version"

module Sinatra
  module AssetHelpers
    module Helpers
      def asset_url_for(filename)
        return filename if filename =~ /^http|https|\//
        halt 500, "Invalid path (#{settings.manifest_path}) to manifest.json" unless File.exists? settings.manifest_path

        manifest = Oj.load(File.open(settings.manifest_path, 'r'))
        file = manifest[filename]

        "#{settings.assets_host}/#{settings.assets_path}/#{file}"
      end

      def stylesheet_tag(filename, media = 'screen')
        %Q(<link href="#{asset_url_for(filename)}" media="#{media}" rel="stylesheet">)
      end

      def javascript_tag(filename)
        %Q(<script src="#{asset_url_for(filename)}"></script>)
      end

      def image_tag(filename, alt = '', title = '')
        %Q(<img src="#{asset_url_for(filename)}" alt="#{alt}" title="#{title}">)
      end
    end

    def self.registered(app)
      app.helpers AssetHelpers::Helpers
      app.set :assets_path, 'assets'
      app.set :assets_host, ENV['ASSETS_HOST']
      app.set :manifest_path, File.join(app.settings.root, 'public/assets/manifest.json')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sinatra-asset_helpers-0.0.1 lib/sinatra/asset_helpers.rb