Sha256: 1b2963d6a0c2b3ddecc3c4d92597f182d108530368a6f5b0ff0f01c0c7127884

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 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

        filepath = filename.split('/')
        filename = filepath.pop
        manifest = Oj.load(File.open(settings.manifest_path, 'r'))
        asset_url = [(settings.assets_host || ''), settings.assets_path, filepath, manifest[filename]].flatten.join('/')
        puts asset_url
        asset_url
      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_host, ENV['ASSETS_HOST']
      app.set :assets_path, 'assets'
      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.2 lib/sinatra/asset_helpers.rb