Skip to content

Instantly share code, notes, and snippets.

@dariocravero
Created January 12, 2013 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dariocravero/2816e3c3970ff05851a1 to your computer and use it in GitHub Desktop.
Save dariocravero/2816e3c3970ff05851a1 to your computer and use it in GitHub Desktop.
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
@dariocravero
Copy link
Author

Grunfile.js https://gist.github.com/2e3060fdf63f77937f7e. All the client code lies in app/client/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment