Permalink
Browse files

implemented 'extras' functionality for additional path lookups to pot…

…entially include, updated README to reflect the new features
  • Loading branch information...
1 parent c7da2c2 commit b752070adfca828cd1fdeef7caff96d1618d32ee Brian Lopez committed Sep 11, 2008
Showing with 148 additions and 51 deletions.
  1. +0 −25 README
  2. +49 −0 README.md
  3. +3 −3 about.yml
  4. +96 −23 lib/dynamic_asset_helper.rb
View
25 README
@@ -1,25 +0,0 @@
-Dynamic Asset Helper
-
-This plugin allows for dynamic inclusion of javascript and css assets based on basic routes.
-
-For example, the path {:controller => 'products', :action => 'list'} might have custom
-JS or CSS files specifically for use by that view. This plugin aims to make that easier.
-
-By placing <%= dynamic_stylesheet_link_tag %> and/or <%= dynamic_javascript_include_tag %>
-in your view, this plugin will look for files at the following paths
-(given the example path above)
-'/stylesheets/products.css', '/stylesheets/products/list.css'
-'/javascripts/products.js', '/javascripts/products/list.js'
-A <script> and/or <link> tag will be generated for each file that exists.
-
-This can be especially helpful since no tags will be output if no css or js file exists
-to be included. Think of it as a way to override or add on to the global look and feel of your
-site, for a specific path.
-
-I realize this plugin may not have performance in mind, since it's doing File.exists? checks
-upon every page load. So please keep in mind this is intended for a small audience who needs it
-and should probably only be used to speed up development.
-
-Although, I hear word that rails 2.0 may include functionality to automagically combine a cached
-version of all assets of the same time from a path tree like this plugin is doing.
-I guess time will tell...
View
49 README.md
@@ -0,0 +1,49 @@
+Dynamic Asset Helper - A set of ActionView helper methods for easier dynamic JS and CSS includes
+====================
+
+This plugin allows for dynamic inclusion of javascript and css assets based on routes.
+A given page might have custom JS or CSS files specifically for it's own use, and no others.
+This plugin aims to make that easier.
+
+Example Usage
+-------------
+
+Consider the following:
+:controller => 'products', :action => 'list'
+
+By placing <%= dynamic_stylesheet_link_tag %> and/or <%= dynamic_javascript_include_tag %>
+in your view, this plugin will look for files at the following paths:
+
+'/stylesheets/products.css'
+'/stylesheets/products/list.css'
+'/javascripts/products.js'
+'/javascripts/products/list.js'
+
+A "script" and/or "link" tag will be generated for each file that exists.
+
+This can be especially helpful since no tags will be output if no css or js file exists
+to be included. Think of it as a way to override or add on to the global look and feel of your
+site, for a specific path.
+
+I realize this plugin may not have performance in mind, since it's doing File.exists? checks
+upon every page load. So please keep in mind this is intended for a small audience who needs it
+and should probably only be used to speed up development.
+
+
+Options
+-------
+
+Both dynamic_stylesheet_link_tag and dynamic_javascript_include_tag accept an :extras parameter.
+:extras is expected to be an array of symbols for "extra" query parameters to use when building paths
+to look up and possibly include as part of final list of JS and CSS files.
+
+Consider the following:
+:controller => 'products', :action => 'list', :tab => 'sale'
+
+dynamic_stylesheet_link_tag :extras => [:tab]
+
+Will attempt to find and generate script include tags following paths:
+'/javascripts/products.js'
+'/javascripts/products/list.js'
+'/javascripts/products/list/tabs.js'
+'/javascripts/products/list/tabs/sale.js'
View
6 about.yml
@@ -1,6 +1,6 @@
author: brianmario
summary: Dynamically includes JS and/or CSS files based on controller/action name, if such files exist at either of those paths
homepage: http://sharpercoding.com
-plugin: https://svn.sharpercoding.com/repos/rails/plugins/dynamic_asset_helper/
-version: 0.1
-rails_version: 1.2+
+plugin: http://github.com/brianmario/dynamic_asset_helper
+version: 1.1
+rails_version: 2.1.1+
View
119 lib/dynamic_asset_helper.rb
@@ -1,43 +1,116 @@
+# DynamicAssetHelper
+#
+# @version 1.0
+#
+# @license MIT-style license
+# @author Brian Lopez <brianmario [at] me.com>
+# @copyright Author
module DynamicAssetHelper
- def dynamic_javascript_include_tag
+ def dynamic_javascript_include_tag(options={})
output = []
- output << controller_javascript_include_tag
- output << action_javascript_include_tag
+ if options[:extras]
+ extras = options[:extras]
+ options.reject! {|k, v| k == :extras}
+ end
+ output << controller_javascript_include_tag(options) unless controller_javascript_include_tag(options).nil?
+ output << action_javascript_include_tag(options) unless action_javascript_include_tag(options).nil?
+ output << other_javascript_include_tags(options, extras) unless extras.nil?
output.join("\n")
end
- def controller_javascript_include_tag
- if defined?(RAILS_ROOT) && File.exists?("#{RAILS_ROOT}/#{@js_path}/"+params[:controller]+'.js')
- javascript_include_tag(params[:controller]+'.js')
+ def dynamic_stylesheet_link_tag(options={})
+ output = []
+ if options[:extras]
+ extras = options[:extras]
+ options.reject! {|k, v| k == :extras}
end
+ output << controller_stylesheet_link_tag(options) unless controller_stylesheet_link_tag(options).nil?
+ output << action_stylesheet_link_tag(options) unless action_stylesheet_link_tag(options).nil?
+ output << other_stylesheet_link_tags(options, extras) unless extras.nil?
+ output.join("\n")
end
- def action_javascript_include_tag
- if defined?(RAILS_ROOT) && File.exists?("#{RAILS_ROOT}/#{@js_path}/"+params[:controller]+'/'+params[:action]+'.js')
- javascript_include_tag(params[:controller]+'/'+params[:action]+'.js')
+ protected
+
+ def controller_javascript_include_tag(options)
+ if File.exists?(File.join(Rails.root, 'public', 'javascripts', params[:controller]+'.js'))
+ path = "#{params[:controller]}"
+ path << "_#{options[:media]}" unless options[:media].nil?
+ javascript_include_tag("#{path}.js", options)
end
end
- def dynamic_stylesheet_link_tag
- output = []
- output << controller_stylesheet_link_tag
- output << action_stylesheet_link_tag
- output.join("\n")
+ def action_javascript_include_tag(options)
+ if File.exists?(File.join(Rails.root, 'public', 'javascripts', params[:controller], params[:action]+'.js'))
+ path = "#{params[:controller]}/#{params[:action]}"
+ path << "_#{options[:media]}" unless options[:media].nil?
+ javascript_include_tag("#{path}.js", options)
+ end
+ end
+
+ def other_javascript_include_tags(options, extras)
+ unless extras.nil?
+ output = []
+ extras.each do |extra|
+
+ fs_path = File.join(Rails.root, 'public', 'javascripts', params[:controller], params[:action], extra.to_s.pluralize+'.js')
+ if File.exists?(fs_path)
+ path = "#{params[:controller]}/#{params[:action]}/#{extra.to_s.pluralize}"
+ path << "_#{options[:media]}" unless options[:media].nil?
+ output << javascript_include_tag("#{path}.js", options)
+ end
+
+ if params[extra]
+ fs_path = File.join(Rails.root, 'public', 'javascripts', params[:controller], params[:action], extra.to_s.pluralize, params[extra]+'.js')
+ if File.exists?(fs_path)
+ path = "#{params[:controller]}/#{params[:action]}/#{extra.to_s.pluralize}/#{params[extra]}"
+ path << "_#{options[:media]}" unless options[:media].nil?
+ output << javascript_include_tag("#{path}.js", options)
+ end
+ end
+ end
+ output.join("\n")
+ end
end
- def controller_stylesheet_link_tag
- if defined?(RAILS_ROOT) && File.exists?("#{RAILS_ROOT}/#{@cs_path}/"+params[:controller]+'.css')
- stylesheet_link_tag(params[:controller]+'.css')
+ def controller_stylesheet_link_tag(options)
+ if File.exists?(File.join(Rails.root, 'public', 'stylesheets', params[:controller]+'.css'))
+ path = "#{params[:controller]}"
+ path << "_#{options[:media]}" unless options[:media].nil?
+ stylesheet_link_tag("#{path}.css", options)
end
end
- def action_stylesheet_link_tag
- if defined?(RAILS_ROOT) && File.exists?("#{RAILS_ROOT}/#{@cs_path}/"+params[:controller]+'/'+params[:action]+'.css')
- stylesheet_link_tag(params[:controller]+'/'+params[:action]+'.css')
+ def action_stylesheet_link_tag(options)
+ if File.exists?(File.join(Rails.root, 'public', 'stylesheets', params[:controller], params[:action]+'.css'))
+ path = "#{params[:controller]}/#{params[:action]}"
+ path << "_#{options[:media]}" unless options[:media].nil?
+ stylesheet_link_tag("#{path}.css", options)
end
end
- private
- @js_path = 'public/javascripts'
- @cs_path = 'public/stylesheets'
+ def other_stylesheet_link_tags(options, extras)
+ unless extras.nil?
+ output = []
+ extras.each do |extra|
+
+ fs_path = File.join(Rails.root, 'public', 'stylesheets', params[:controller], params[:action], extra.to_s.pluralize+'.css')
+ if File.exists?(fs_path)
+ path = "#{params[:controller]}/#{params[:action]}/#{extra.to_s.pluralize}"
+ path << "_#{options[:media]}" unless options[:media].nil?
+ output << stylesheet_link_tag("#{path}.css", options)
+ end
+
+ if params[extra]
+ fs_path = File.join(Rails.root, 'public', 'stylesheets', params[:controller], params[:action], extra.to_s.pluralize, params[extra]+'.css')
+ if File.exists?(fs_path)
+ path = "#{params[:controller]}/#{params[:action]}/#{extra.to_s.pluralize}/#{params[extra]}"
+ path << "_#{options[:media]}" unless options[:media].nil?
+ output << stylesheet_link_tag("#{path}.css", options)
+ end
+ end
+ end
+ output.join("\n")
+ end
+ end
end

0 comments on commit b752070

Please sign in to comment.