-
-
Save dariocravero/00129ea297a435742d50 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Padrino | |
module Assets | |
def self.registered(app) | |
app.set :assets_config, YAML.load_file(File.join(Padrino.root, 'config', 'assets.yml')) | |
app.set :assets_path, File.join('app', 'client') | |
# You must set this: | |
# app.set :assets_base_url, '' | |
['require.config.js', 'app/', 'components/', 'dist/'].each do |path| | |
route = path =~ /\/$/ ? "#{path}/*file" : path | |
app.get "/#{route}" do | |
if params.include? :file | |
file = File.join(Padrino.root, self.settings.assets_path, path, params[:file]) | |
else | |
file = File.join(Padrino.root, self.settings.assets_path, path) | |
end | |
expires 0, :no_cache | |
send_file file, :type => File.extname(file) | |
end | |
end | |
app.helpers Padrino::Assets::Helpers | |
end | |
module Helpers | |
## | |
# Returns an html script tag for each of the sources provided. | |
# You can pass in the filename without extension or a symbol and we search it in your +appname.public_folder+ | |
# like app/public/javascript for inclusion. You can provide also a full path. | |
# | |
# @overload javascript_include_tag(*sources, options={}) | |
# @param [Array<String>] sources Splat of js source paths | |
# @param [Hash] options The html options for the script tag | |
# | |
# @return [String] Script tag for +sources+ with specified +options+. | |
# | |
# @example | |
# javascript_include_tag 'application', :extjs | |
# | |
# @api public | |
def fingerprinted_javascript_include_tag(*sources) | |
options = sources.extract_options!.symbolize_keys | |
options.reverse_merge!(:type => 'text/javascript') | |
sources.flatten.map { |source| | |
content_tag(:script, nil, options.reverse_merge(:src => "#{self.settings.assets_base_url}#{fingerprinted_asset_path(source)}")) | |
}.join("\n") | |
end | |
## | |
# Returns an html script tag for each of the sources provided. | |
# You can pass in the filename without extension or a symbol and we search it in your +appname.public_folder+ | |
# like app/public/stylesheets for inclusion. You can provide also a full path. | |
# | |
# @overload stylesheet_link_tag(*sources, options={}) | |
# @param [Array<String>] sources Splat of css source paths | |
# @param [Hash] options The html options for the link tag | |
# | |
# @return [String] Stylesheet link html tag for +sources+ with specified +options+. | |
# | |
# @example | |
# stylesheet_link_tag 'style', 'application', 'layout' | |
# | |
# @api public | |
def fingerprinted_stylesheet_link_tag(*sources) | |
options = sources.extract_options!.symbolize_keys | |
options.reverse_merge!(:media => 'screen', :rel => 'stylesheet', :type => 'text/css') | |
sources.flatten.map { |source| | |
tag(:link, options.reverse_merge(:href => "#{self.settings.assets_base_url}#{fingerprinted_asset_path(source)}")) | |
}.join("\n") | |
end | |
private | |
def fingerprinted_asset_path(source) | |
fingerprinted = JSON.parse(File.open(File.join(Padrino.root, self.settings.assets_path, 'fingerprinted.json')).read)['files'] | |
fingerprinted[source] | |
end | |
end | |
end # BBB | |
end # Padrino |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment