Permalink
Browse files

Add ability to set a callback to a class to manually add asset right …

…before the assets are rendered
  • Loading branch information...
1 parent cb452ea commit 42b7c9310e5ce5c9b542de86c605b51e34aacda6 @iampersistent iampersistent committed Nov 29, 2009
Showing with 124 additions and 148 deletions.
  1. +32 −3 README
  2. +0 −102 README~
  3. +57 −41 lib/asset_auto_include.rb
  4. +35 −2 test/asset_auto_include_test.rb
View
35 README
@@ -135,10 +135,39 @@ The following will register a file for both js and css
In a controller you can use
- ActionView::Helpers::AssetTagHelper::register_asset_auto_include 'manual' #registers both
+ AssetAutoInclude::register 'manual' #registers both javascript and stylesheet
-This would put <script src="/javascripts/manual.js"> in the header, after the javascript that is autoloaded from
-the views directory.
+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.
View
102 README~
@@ -1,102 +0,0 @@
-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
-
-== 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/
-
View
@@ -79,7 +79,7 @@ def asset_auto_include_tags(asset_type = :javascript, options = {})
end
#add the manually added files and clear them out
- @to_include |= ManualAsset::assets asset_type
+ @to_include |= AssetAutoInclude::assets asset_type
"<!-- auto #{asset_type.to_s} -->\n#{
@to_include.uniq.collect { |source|
@@ -89,57 +89,73 @@ def asset_auto_include_tags(asset_type = :javascript, options = {})
end
def register_asset_auto_include(asset_name = "", asset_type = :both)
- ManualAsset::register(asset_name, asset_type)
+ AssetAutoInclude::register(asset_name, asset_type)
end
- def self.register_asset_auto_include(asset_name = "", asset_type = :both)
- ManualAsset::register(asset_name, asset_type)
- end
-
- private
- def register_manual(asset_name = "", asset_type = :both)
- if asset_name.empty?
- return nil
- end
- if asset_type == :both
- @@aai_manual[:javascript] << "#{asset_name}.#{@@aai_extension[:javascript]}"
- @@aai_manual[:stylesheet] << "#{asset_name}.#{@@aai_extension[:stylesheet]}"
- else
- begin
- @@aai_manual[asset_type] << "#{asset_name}.#{@@aai_extension[asset_type]}"
- rescue
- raise "#{asset_type} is not a valid asset type"
- end
- end
- return nil
- end
-
+# def self.register_asset_auto_include(asset_name = "", asset_type = :both)
+# AssetAutoInclude::register(asset_name, asset_type)
+# end
end
end
end
-class ManualAsset
- @@aai_manual = {:javascript => [], :stylesheet => []}
- @@aai_extension = {:javascript => 'js', :stylesheet => 'css'}
+
+class AssetAutoInclude
+ @@aai_manual = {:javascript => [], :stylesheet => []}
+ @@aai_extension = {:javascript => 'js', :stylesheet => 'css'}
+ @@callbacks = {:javascript => [], :stylesheet => []}
def self.assets(asset_type, reset=true)
+ @@callbacks[asset_type].each { |klass| @@aai_manual[asset_type] << klass.auto_include_assets(asset_type, reset) }
+ @@callbacks[asset_type] = []
assets = @@aai_manual[asset_type]
@@aai_manual[asset_type]=[] if reset
return assets
end
def self.register(asset_name = "", asset_type = :both)
- if asset_name.empty?
- return nil
- end
- if asset_type == :both
- @@aai_manual[:javascript] << "#{asset_name}.#{@@aai_extension[:javascript]}"
- @@aai_manual[:stylesheet] << "#{asset_name}.#{@@aai_extension[:stylesheet]}"
- else
- begin
- @@aai_manual[asset_type] << "#{asset_name}.#{@@aai_extension[asset_type]}"
- rescue
- raise "#{asset_type} is not a valid asset type"
- end
- end
- return nil
+ if asset_name.empty?
+ return nil
+ end
+ if asset_type == :both
+ @@aai_manual[:javascript] << "#{asset_name}.#{@@aai_extension[:javascript]}"
+ @@aai_manual[:stylesheet] << "#{asset_name}.#{@@aai_extension[:stylesheet]}"
+ else
+ begin
+ @@aai_manual[asset_type] << "#{asset_name}.#{@@aai_extension[asset_type]}"
+ rescue
+ raise "#{asset_type} is not a valid asset type"
+ end
+ end
+ return nil
+ end
+ def self.register_callback(klass, asset_type = :both)
+ if !klass
+ return nil
+ end
+ if asset_type == :both
+ @@callbacks[:javascript] << klass
+ @@callbacks[:stylesheet] << klass
+ else
+ begin
+ @@callbacks[asset_type] << klass
+ rescue
+ raise "#{asset_type} is not a valid asset type"
+ end
+ end
+ return nil
end
end
+
+module AssetAutoIncludeModule
+ ##
+ # Declares a callback for AssetAutoInclude
+ #
+ # optional param
+ # @param :javascript callback for javascript only
+ # @param :stylesheet callback for stylesheet only
+ #
+ # There should be a auto_include_assets function in the class where this is included
+ #
+ def asset_auto_include_callback(asset_type = :both)
+ AssetAutoInclude::register_callback(self, asset_type)
+ end
+end
@@ -106,7 +106,7 @@ def action_name
results = asset_auto_include_tags :javascript
assert_equal(expected, results)
end
- def test_javascript_return_with_manual_from_controller_and_no_controller_or_action
+ def test_javascript_return_with_manual_not_from_view_and_no_controller_or_action
@controller = Class.new do
def controller_path
'not_included'
@@ -115,7 +115,7 @@ def action_name
'not_here'
end
end.new
- ActionView::Helpers::AssetTagHelper::register_asset_auto_include 'manual', :javascript
+ AssetAutoInclude::register 'manual', :javascript
expected = "<!-- auto javascript -->\n"
expected += "<script src=\"/javascripts/manual.js\" type=\"text/javascript\"></script>\n"
expected += "<!-- /auto javascript -->"
@@ -214,4 +214,37 @@ def action_name
results = asset_auto_include_tags :javascript
assert_equal(expected, results)
end
+ def test_callback_to_class
+ @klass = Class.new do
+ def initialize
+ AssetAutoInclude::register_callback(self)
+ end
+ def auto_include_assets(asset_type, reset)
+ case asset_type
+ when :javascript
+ return 'klass'
+ when :stylesheet
+ return ['css_klass', 'no_klass']
+ end
+ end
+ end.new
+ @controller = Class.new do
+ def controller_path
+ 'not_included'
+ end
+ def action_name
+ 'not_here'
+ end
+ end.new
+ expected = "<!-- auto javascript -->\n"
+ expected += "<script src=\"/javascripts/klass.js\" type=\"text/javascript\"></script>\n"
+ expected += "<!-- /auto javascript -->"
+ expected += "<!-- auto stylesheet -->\n"
+ expected += "<link href=\"/stylesheets/css_klass.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />\n"
+ expected += "<link href=\"/stylesheets/no_klass.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />\n"
+ expected += "<!-- /auto stylesheet -->"
+ results = asset_auto_include_tags :javascript
+ results += asset_auto_include_tags :stylesheet
+ assert_equal(expected, results)
+ end
end

0 comments on commit 42b7c93

Please sign in to comment.