INSTALLATION¶ ↑
gem install blix-assets
Manage Asset Files¶ ↑
using the asset pipeline¶ ↑
the assets are served by your asset pipeline ( sprockets) and the link to the asset in your html is the asset name pure and simple.
eg:
<script src="/assets/application.js" type="text/javascript"></script>
using compiled assets¶ ↑
a hash is added to the asset name to ensure that it is unique. This allows the asset to be cached indefinitely on the browser.
eg:
<script src="/assets/application-59D3331242D65ECC.js" type="text/javascript"></script>
template helper¶ ↑
in order to use the relevant version user the asset_path helper in you template. The PRODUCTION flag should indicate if you are in development mode (false) or in production mode/compiled assets mode (true).
eg: <script src="<%= Blix::AssetManager.asset_path('/assets/application.js',PRODUCTION) %>" type="text/javascript"></script> <%= Blix::AssetManager.asset_tag('/assets/application.js',PRODUCTION) %> <%= Blix::AssetManager.asset_tag('/assets/main.css',PRODUCTION, :media=>'screen') %>
if you are using blix_rest then there is a helper defined ..
<script src="<%= asset_path('/assets/application.js') %>" type="text/javascript"></script>
or the complete tag ..
<%= asset_tag('/assets/application.js') %> <%= asset_tag('/static/man.css', :media=>'screen') %>
to enable this helper you have to require 'blix/rest'
first and then require 'blix/assets'
inline assets¶ ↑
assets can also be inlined which results in the contents of the compiled assets to be inlined in production mode only.
In development mode a link to the asset is used.
<%= asset_tag('/assets/application.js', :inline=>true ) %> <%= asset_tag('/static/main.css', :media=>'screen', :inline=>true) %>
Mount points in templates¶ ↑
use the following helpers to expand any filenames to include a mount point in your templates.
Blix::AssetManager.set_path_root
( '/myapp')
Blix::AssetManager.full_path
('/images/icon.png') ==> '/myapp/images/icon.png'
Compiling Assets¶ ↑
ensure that the destination dictory exists and that the config directory 'config/assets' also exists !!
For the destination directory use a path relative to your applications working directory.
This will ensure that the file can be found in your production environment as well as in your development environment.
require 'blix/assets' DEST = File.join("www","assets") # where the asset is to be written data = environment[name].to_s # the compiled asset data Blix::AssetManager.write_asset(DEST,"application.js",data)
it is also possible to force a version number on the asset instead of a generated stamp
Blix::AssetManager.write_asset(DEST,"application.js",data, :version=>"1.2.3")
Generate a static index.html¶ ↑
it is possible to generate a static index.html with the correct assets within your asset compile script.
you will need an index.html.erb file with the <%= Blix::AssetManager.asset_tag
.. %> code
and set your PRODUCTION variable to true in the compile script. In your config.ru set PRODUCTION to false in development mode.
eg …
index.html.erb
.... .... <head> <%= Blix::AssetManager.asset_tag('/assets/application.js',PRODUCTION) %> <%= Blix::AssetManager.asset_tag('/assets/main.css',PRODUCTION, :media=>'screen') %> </head>
compile_assets.rb ..
ROOT = Dir.pwd PRODUCTION = true DEST = File.join("www","assets") .... .... Blix::AssetManager.write_asset(DEST,"application.js",environment["application.js"].to_s) Blix::AssetManager.write_asset(DEST,"main.css",environment["main.css"].to_s) .... .... File.write File.join("www","index.html"),environment['index.html'].to_s