Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Based on the javascript_auto_include_plugin but allows to include all sorts of things, not just js
Ruby JavaScript
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
tasks
test
README
Rakefile
init.rb
install.rb
uninstall.rb

README

AssetAutoInclude
=====================

== Resources

  script/plugin install git://github.com/evilchelu/asset_auto_include.git
 
== Usage

Add the following to the head of your template file

  <%= asset_auto_include_tags :javascript, {options} %>
  <%= asset_auto_include_tags :stylesheet, {options} %>

Options:
 :auto_dir this can be set to change the location of the autoloaded files, it is 'auto' by default, it can be set to 'views' for compatibility with javascript_auto_include

Javascript specific options:
 :method the ActionView::Helper::AssetTag to use, the default is 'javascript_include_tag'
 :extension the file extension for the javascript file, 'js' is the default
 :directory the directory for the javascript files, 'javascripts' is the default

Stylesheet specific options:
 :method the ActionView::Helper::AssetTag to use, the default is 'stylesheet_link_tag'
 :extension the file extension for the stylesheet file, 'css' is the default
 :directory the directory for the stylesheet files, 'stylesheets' is the default
 :template_extension used for an alternate templating system, 'sass' is the default
 :template_directory used for an alternate templating sytem, 'stylesheets/sass' is the default
 
Now each time the template is loaded asset files in the 
public/(javascripts|stylesheets)/ folder that correspond to the name of the
current controller or view will be auto loaded. For example:

  /public
    /javascripts
      /auto
        /accounts
          show.js

  /public
    /stylesheets
      /auto
        users.css
        /users
          edit.css

Assuming the above file structure loading each of the following urls would load:
 
 mydomain.com/accounts        # no js files loaded
 mydomain.com/accounts/show/1 # loads auto/accounts/show.js
 mydomain.com/users           # loads auto/users/users.css
 mydomain.com/users/1/edit    # loads auto/users/users.css
                                loads auto/users/edit.css

There's also support for sass files. When using :stylesheet, the sass directory 
is also checked, along with the stylesheets directory.

  /public
    /stylesheets
      /sass
        /auto
          /posts
            view.sass

Assuming the above file structure loading each of the following urls would load:

 mydomain.com/users/1           # loads auto/posts/view.css

Namespace is supported so for the following

  /public
    /javascripts
      /auto
        admin.js
        /admin
          /dashboard
            index.js

Assuming the above file structure loading each of the following urls would load:
 
 mydomain.com/admin/dashboard        # loads auto/admin/dashboard/index.js and auto/admin.js


You can also load a default .js or .css file.  They should be located at

 /public
    /javascripts
      auto.js

  /public
    /stylesheets
      auto.css
      
 Now each time the template is loaded javascript files in the public/javascripts/views
 folder that correspond to the name of the current controller or view will be auto
 loaded. It's also possible for the same javascript file to be loaded by multiple views
 by adding the name of each view to the filename separated by the "-" character, e.g. to load
 a javascript file in the new and edit views create a file named new-edit.js. Any number
 of views can be strung together using this naming convention. For example:
 
 /public
   /javascripts
     /auto
       users.js
       /users
         edit.js
       roles.js
       /accounts
         show-new-edit-create.js
show.js
 
 Assuming the above file structure loading each of the following urls would include:
 
 mydomain.com/users # includes users.js
 mydomain.com/users/edit/1 # includes users.js and edit.js
 mydomain.com/users/show/1 # includes users.js
 mydomain.com/roles # includes roles.js
 mydomain.com/accounts # no files included
 mydomain.com/accounts/show/1 # includes show.js and show-new-edit-create.js
 mydomain.com/accounts/new # includes show-new-edit-create.js
 mydomain.com/accounts/edit/1 # includes show-new-edit-create.js
 mydomain.com/accounts/create # includes show-new-edit-create.js
 
 Sometimes, it may be useful to dynamically add javascript files, based on logic or a particular view or partial.
 With JavascriptAutoInclude you can also manually include files to be loaded and they will be displayed with
 <%= javascript_auto_include_tag %>. These files should be in the javascript directory, not the autoloading
 views directory. To use the manually loading feature in a view, you can add
 
  <%= register_asset_auto_include 'manual', :javascript %>
  <%= register_asset_auto_include 'manual', :stylesheet %>

The following will register a file for both js and css
  <%= register_asset_auto_include 'manual', :both %>
  <%= register_asset_auto_include 'manual' %>
  
In a controller you can use
 
  AssetAutoInclude::register 'manual' #registers both javascript and stylesheet
  
This would put <script src="/javascripts/manual.js"> in the <head>, after the javascript that is autoloaded from
the views directory.  It would also put <link href="/stylesheets/manual.css rel="stylesheet" type="text/css" /> in the <head>

Registering from other plugins

You can register a callback from AssetAutoInclude that will give another class the opportunity to manually add assets before the
html is inserted.

Include the following to the class

  def initialize
    AssetAutoInclude::register_callback(self)
  end

You can optionally add a second param of either :javascript or :stylesheet to include just one of those asset in the callback

You must also include a function in the class to handle the callback

  def auto_include_assets(asset_type, reset)
       def self.auto_include_assets(asset_type, reset)
        case asset_type
        when :javascript
          return 'klass'
        when :stylesheet
          return ['css_klass', 'no_klass']
        end
      end
    
  end


== Aknowledgements

This plugin was originally based on the javascript_auto_include by Jamie Dyer.

If you want the original plugin:

  http://github.com/kernow/javascript_auto_include/tree/master
  http://hosting.media72.co.uk/blog/2008/05/13/asset-auto-include-rails-plugin/

Something went wrong with that request. Please try again.