Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Engine's assets with Rails 3.1

How should one provide assets in an engine in Rails 3.1? Where should they be located and can they be included automatically?

(originally asked by Tomas Celizna)

like image 979
Marc-André Lafortune Avatar asked May 04 '11 01:05

Marc-André Lafortune


People also ask

What is a Rails engine?

1 What are Engines? Engines can be considered miniature applications that provide functionality to their host applications. A Rails application is actually just a "supercharged" engine, with the Rails::Application class inheriting a lot of its behavior from Rails::Engine .

What are assets in Rails?

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass and ERB. Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets.

What does Rails assets Precompile do?

The Rails asset pipeline provides an assets:precompile rake task to allow assets to be compiled and cached up front rather than compiled every time the app boots. There are two ways you can use the asset pipeline on Heroku. Compiling assets locally.

What is Require_tree?

The require_tree directive tells Sprockets to recursively include all JavaScript files in the specified directory into the output. These paths must be specified relative to the manifest file.


1 Answers

The paths to the all the engines' assets folders are automatically loaded.

The assets themselves are not loaded by default. This is understandable as the loading is done with require_tree ., which loads all css/js from the current folder (i.e. the main application assets' folder) but doesn't say anything about the engines assets.

The easy solution is to ask the user to require the js/css in application.js/css or wherever else it is needed. As the paths are loaded correctly, the user only need to specify the name of your asset (I'd recommend using the name of your engine). Example:

Appended to main_app/app/assets/javascripts/application.js:

//= require your_engine_name 

If you have split your js in different files, your file your_engine_name/app/assets/javascripts/your_engine_name.js could have the following:

//= require_tree . 

This will load all js files in your_engine_name/app/assets/javascripts/, as the "." refers to the local folder (in this case the folder of your engine's javascripts).

Note that ActionView::Helpers::AssetTagHelper.register_javascript_expansion appears not to have any effect when config.use_sprockets is set. I hope they'll at least put a warning in that case.

If you have a rake task to install your engine, then you could do the append to application.js.

Another way for the user to include it is to insert <%= javascript_include_tag "your_engine_name" %> in the erb layout.

I don't think there is a way to have it inserted automatically

like image 64
Marc-André Lafortune Avatar answered Sep 18 '22 09:09

Marc-André Lafortune