From bd940d98bd417da7cb0a815a1c02aef500c8b2db Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 20 Mar 2008 16:26:04 +0000 Subject: Re-added ActionView::Helpers::register_javascript/stylesheet_expansion to make it easier for plugin developers to inject multiple assets. Closes #10350. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9065 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/action_view/helpers/asset_tag_helper.rb | 73 ++++++++++++++++------ 1 file changed, 55 insertions(+), 18 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb index 2939dfbbef..d57d1e0903 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb @@ -155,7 +155,8 @@ module ActionView alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route JAVASCRIPT_DEFAULT_SOURCES = ['prototype', 'effects', 'dragdrop', 'controls'] unless const_defined?(:JAVASCRIPT_DEFAULT_SOURCES) - @@javascript_default_sources = JAVASCRIPT_DEFAULT_SOURCES.dup + @@javascript_expansions = { :defaults => JAVASCRIPT_DEFAULT_SOURCES.dup } + @@stylesheet_expansions = {} # Returns an html script tag for each of the +sources+ provided. You # can pass in the filename (.js extension is optional) of javascript files @@ -249,16 +250,46 @@ module ActionView end end + # Register one or more javascript files to be included when symbol + # is passed to javascript_include_tag. This method is typically intended + # to be called from plugin initialization to register javascript files + # that the plugin installed in public/javascripts. + # + # ActionView::Helpers::AssetTagHelper.register_javascript_expansion :monkey => ["head", "body", "tail"] + # + # javascript_include_tag :monkey # => + # + # + # + def self.register_javascript_expansion(expansions) + @@javascript_expansions.merge!(expansions) + end + + # Register one or more stylesheet files to be included when symbol + # is passed to stylesheet_link_tag. This method is typically intended + # to be called from plugin initialization to register stylesheet files + # that the plugin installed in public/stylesheets. + # + # ActionView::Helpers::AssetTagHelper.register_stylesheet_expansion :monkey => ["head", "body", "tail"] + # + # stylesheet_link_tag :monkey # => + # + # + # + def self.register_stylesheet_expansion(expansions) + @@stylesheet_expansions.merge!(expansions) + end + # Register one or more additional JavaScript files to be included when # javascript_include_tag :defaults is called. This method is # typically intended to be called from plugin initialization to register additional # .js files that the plugin installed in public/javascripts. def self.register_javascript_include_default(*sources) - @@javascript_default_sources.concat(sources) + @@javascript_expansions[:defaults].concat(sources) end def self.reset_javascript_include_default #:nodoc: - @@javascript_default_sources = JAVASCRIPT_DEFAULT_SOURCES.dup + @@javascript_expansions[:defaults] = JAVASCRIPT_DEFAULT_SOURCES.dup end # Computes the path to a stylesheet asset in the public stylesheets directory. @@ -534,28 +565,34 @@ module ActionView end def expand_javascript_sources(sources) - case - when sources.include?(:all) + if sources.include?(:all) all_javascript_files = Dir[File.join(JAVASCRIPTS_DIR, '*.js')].collect { |file| File.basename(file).split(".", 0).first }.sort - sources = ((@@javascript_default_sources.dup & all_javascript_files) + all_javascript_files).uniq - - when sources.include?(:defaults) - sources = sources[0..(sources.index(:defaults))] + - @@javascript_default_sources.dup + - sources[(sources.index(:defaults) + 1)..sources.length] - - sources.delete(:defaults) - sources << "application" if file_exist?(File.join(JAVASCRIPTS_DIR, "application.js")) + @@all_javascript_sources ||= ((determine_source(:defaults, @@javascript_expansions).dup & all_javascript_files) + all_javascript_files).uniq + else + expanded_sources = sources.collect do |source| + determine_source(source, @@javascript_expansions) + end.flatten + expanded_sources << "application" if sources.include?(:defaults) && file_exist?(File.join(JAVASCRIPTS_DIR, "application.js")) + expanded_sources end - - sources end def expand_stylesheet_sources(sources) if sources.first == :all - @@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).split(".", 1).first }.sort + @@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).split(".", 0).first }.sort + else + sources.collect do |source| + determine_source(source, @@stylesheet_expansions) + end.flatten + end + end + + def determine_source(source, collection) + case source + when Symbol + collection[source] || raise(ArgumentError, "No expansion found for #{source.inspect}") else - sources + source end end -- cgit v1.2.3