From 91b0b6583428edaec1507c8dbfccfb2056bdedb4 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Sun, 14 Nov 2010 16:14:26 +0100 Subject: split the javascript and stylesheet tag helpers into separate files as a precusor before removing the duplication between the two --- .../asset_tag_helpers/javascript_tag_helpers.rb | 180 +++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb (limited to 'actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb new file mode 100644 index 0000000000..ea8ac3f9a1 --- /dev/null +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb @@ -0,0 +1,180 @@ +module ActionView + module Helpers + module AssetTagHelper + module JavascriptTagHelpers + extend ActiveSupport::Concern + + included do + mattr_accessor :javascript_expansions + self.javascript_expansions = { } + end + + module ClassMethods + # 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 register_javascript_expansion(expansions) + self.javascript_expansions.merge!(expansions) + end + end + + # Computes the path to a javascript asset in the public javascripts directory. + # If the +source+ filename has no extension, .js will be appended (except for explicit URIs) + # Full paths from the document root will be passed through. + # Used internally by javascript_include_tag to build the script path. + # + # ==== Examples + # javascript_path "xmlhr" # => /javascripts/xmlhr.js + # javascript_path "dir/xmlhr.js" # => /javascripts/dir/xmlhr.js + # javascript_path "/dir/xmlhr" # => /dir/xmlhr.js + # javascript_path "http://www.railsapplication.com/js/xmlhr" # => http://www.railsapplication.com/js/xmlhr + # javascript_path "http://www.railsapplication.com/js/xmlhr.js" # => http://www.railsapplication.com/js/xmlhr.js + def javascript_path(source) + compute_public_path(source, 'javascripts', 'js') + end + alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route + + # Returns an HTML script tag for each of the +sources+ provided. You + # can pass in the filename (.js extension is optional) of JavaScript files + # that exist in your public/javascripts directory for inclusion into the + # current page or you can pass the full path relative to your document + # root. To include the Prototype and Scriptaculous JavaScript libraries in + # your application, pass :defaults as the source. When using + # :defaults, if an application.js file exists in + # public/javascripts it will be included as well. You can modify the + # HTML attributes of the script tag by passing a hash as the last argument. + # + # ==== Examples + # javascript_include_tag "xmlhr" # => + # + # + # javascript_include_tag "xmlhr.js" # => + # + # + # javascript_include_tag "common.javascript", "/elsewhere/cools" # => + # + # + # + # javascript_include_tag "http://www.railsapplication.com/xmlhr" # => + # + # + # javascript_include_tag "http://www.railsapplication.com/xmlhr.js" # => + # + # + # javascript_include_tag :defaults # => + # + # + # ... + # + # + # * = The application.js file is only referenced if it exists + # + # You can also include all javascripts in the +javascripts+ directory using :all as the source: + # + # javascript_include_tag :all # => + # + # + # ... + # + # + # + # + # Note that the default javascript files will be included first. So Prototype and Scriptaculous are available to + # all subsequently included files. + # + # If you want Rails to search in all the subdirectories under javascripts, you should explicitly set :recursive: + # + # javascript_include_tag :all, :recursive => true + # + # == Caching multiple javascripts into one + # + # You can also cache multiple javascripts into one file, which requires less HTTP connections to download and can better be + # compressed by gzip (leading to faster transfers). Caching will only happen if config.perform_caching + # is set to true (which is the case by default for the Rails production environment, but not for the development + # environment). + # + # ==== Examples + # javascript_include_tag :all, :cache => true # when config.perform_caching is false => + # + # + # ... + # + # + # + # + # javascript_include_tag :all, :cache => true # when config.perform_caching is true => + # + # + # javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when config.perform_caching is false => + # + # + # + # + # javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when config.perform_caching is true => + # + # + # The :recursive option is also available for caching: + # + # javascript_include_tag :all, :cache => true, :recursive => true + def javascript_include_tag(*sources) + options = sources.extract_options!.stringify_keys + concat = options.delete("concat") + cache = concat || options.delete("cache") + recursive = options.delete("recursive") + + if concat || (config.perform_caching && cache) + joined_javascript_name = (cache == true ? "all" : cache) + ".js" + joined_javascript_path = File.join(joined_javascript_name[/^#{File::SEPARATOR}/] ? config.assets_dir : config.javascripts_dir, joined_javascript_name) + + unless config.perform_caching && File.exists?(joined_javascript_path) + write_asset_file_contents(joined_javascript_path, compute_javascript_paths(sources, recursive)) + end + javascript_src_tag(joined_javascript_name, options) + else + sources = expand_javascript_sources(sources, recursive) + ensure_javascript_sources!(sources) if cache + sources.collect { |source| javascript_src_tag(source, options) }.join("\n").html_safe + end + end + + private + + def javascript_src_tag(source, options) + content_tag("script", "", { "type" => Mime::JS, "src" => path_to_javascript(source) }.merge(options)) + end + + def compute_javascript_paths(*args) + expand_javascript_sources(*args).collect { |source| compute_public_path(source, 'javascripts', 'js', false) } + end + + def expand_javascript_sources(sources, recursive = false) + if sources.include?(:all) + all_javascript_files = (collect_asset_files(config.javascripts_dir, ('**' if recursive), '*.js') - ['application']) << 'application' + ((determine_source(:defaults, self.javascript_expansions).dup & all_javascript_files) + all_javascript_files).uniq + else + expanded_sources = sources.collect do |source| + determine_source(source, self.javascript_expansions) + end.flatten + expanded_sources << "application" if sources.include?(:defaults) && File.exist?(File.join(config.javascripts_dir, "application.js")) + expanded_sources + end + end + + def ensure_javascript_sources!(sources) + sources.each do |source| + asset_file_path!(path_to_javascript(source)) + end + return sources + end + end + end + end +end \ No newline at end of file -- cgit v1.2.3 From 1c129f8f8ea5b10605ad77af6064c983584d463d Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Sun, 14 Nov 2010 22:13:17 +0100 Subject: separated the asset id methods to a separate module, removed some dupliation with the various path methods, and moved the base asset tag methods to a base module so the asset id module can play nice with the path generation --- .../helpers/asset_tag_helpers/javascript_tag_helpers.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb index ea8ac3f9a1..668a6136bc 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb @@ -1,8 +1,14 @@ +require 'active_support/concern' +require 'action_view/helpers/asset_tag_helpers/helper_methods' + module ActionView module Helpers module AssetTagHelper + module JavascriptTagHelpers extend ActiveSupport::Concern + extend HelperMethods + include SharedHelpers included do mattr_accessor :javascript_expansions @@ -37,10 +43,7 @@ module ActionView # javascript_path "/dir/xmlhr" # => /dir/xmlhr.js # javascript_path "http://www.railsapplication.com/js/xmlhr" # => http://www.railsapplication.com/js/xmlhr # javascript_path "http://www.railsapplication.com/js/xmlhr.js" # => http://www.railsapplication.com/js/xmlhr.js - def javascript_path(source) - compute_public_path(source, 'javascripts', 'js') - end - alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route + asset_path :javascript, 'js' # Returns an HTML script tag for each of the +sources+ provided. You # can pass in the filename (.js extension is optional) of JavaScript files @@ -174,7 +177,9 @@ module ActionView end return sources end + end + end end end \ No newline at end of file -- cgit v1.2.3 From 6747ae2708d2ad8a482f3dfcb346d007a51af77a Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Mon, 15 Nov 2010 15:44:19 +0100 Subject: reduced duplication between the javascript and stylesheet asset tag methods, also split the asset id caching methods into a separate module for easy inclusion and use by the asset include tag class and base asset tag helpers --- .../asset_tag_helpers/javascript_tag_helpers.rb | 89 +++++++++------------- 1 file changed, 38 insertions(+), 51 deletions(-) (limited to 'actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb index 668a6136bc..de581a2737 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb @@ -1,14 +1,47 @@ require 'active_support/concern' -require 'action_view/helpers/asset_tag_helpers/helper_methods' +require 'active_support/core_ext/file' +require 'action_view/helpers/tag_helper' +require 'action_view/helpers/asset_tag_helpers/common_asset_helpers' +require 'action_view/helpers/asset_tag_helpers/asset_include_tag' module ActionView module Helpers module AssetTagHelper + class JavascriptIncludeTag < AssetIncludeTag + include TagHelper + + self.asset_name = 'javascript' + self.extension = 'js' + + def asset_tag(source, options) + content_tag("script", "", { "type" => Mime::JS, "src" => path_to_asset(source) }.merge(options)) + end + + def custom_dir + config.javascripts_dir + end + + private + + def expand_sources(sources, recursive = false) + if sources.include?(:all) + all_asset_files = (collect_asset_files(custom_dir, ('**' if recursive), "*.#{extension}") - ['application']) << 'application' + ((determine_source(:defaults, expansions).dup & all_asset_files) + all_asset_files).uniq + else + expanded_sources = sources.collect do |source| + determine_source(source, expansions) + end.flatten + expanded_sources << "application" if sources.include?(:defaults) && File.exist?(File.join(custom_dir, "application.#{extension}")) + expanded_sources + end + end + end + module JavascriptTagHelpers extend ActiveSupport::Concern - extend HelperMethods - include SharedHelpers + extend HelperMacros + include CommonAssetHelpers included do mattr_accessor :javascript_expansions @@ -128,56 +161,10 @@ module ActionView # # javascript_include_tag :all, :cache => true, :recursive => true def javascript_include_tag(*sources) - options = sources.extract_options!.stringify_keys - concat = options.delete("concat") - cache = concat || options.delete("cache") - recursive = options.delete("recursive") - - if concat || (config.perform_caching && cache) - joined_javascript_name = (cache == true ? "all" : cache) + ".js" - joined_javascript_path = File.join(joined_javascript_name[/^#{File::SEPARATOR}/] ? config.assets_dir : config.javascripts_dir, joined_javascript_name) - - unless config.perform_caching && File.exists?(joined_javascript_path) - write_asset_file_contents(joined_javascript_path, compute_javascript_paths(sources, recursive)) - end - javascript_src_tag(joined_javascript_name, options) - else - sources = expand_javascript_sources(sources, recursive) - ensure_javascript_sources!(sources) if cache - sources.collect { |source| javascript_src_tag(source, options) }.join("\n").html_safe - end + @javascript_include ||= JavascriptIncludeTag.new(config, controller, self.javascript_expansions) + @javascript_include.include_tag(*sources) end - private - - def javascript_src_tag(source, options) - content_tag("script", "", { "type" => Mime::JS, "src" => path_to_javascript(source) }.merge(options)) - end - - def compute_javascript_paths(*args) - expand_javascript_sources(*args).collect { |source| compute_public_path(source, 'javascripts', 'js', false) } - end - - def expand_javascript_sources(sources, recursive = false) - if sources.include?(:all) - all_javascript_files = (collect_asset_files(config.javascripts_dir, ('**' if recursive), '*.js') - ['application']) << 'application' - ((determine_source(:defaults, self.javascript_expansions).dup & all_javascript_files) + all_javascript_files).uniq - else - expanded_sources = sources.collect do |source| - determine_source(source, self.javascript_expansions) - end.flatten - expanded_sources << "application" if sources.include?(:defaults) && File.exist?(File.join(config.javascripts_dir, "application.js")) - expanded_sources - end - end - - def ensure_javascript_sources!(sources) - sources.each do |source| - asset_file_path!(path_to_javascript(source)) - end - return sources - end - end end -- cgit v1.2.3 From 6a609dbc82d03eb92a85970aa157192657f14882 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Mon, 15 Nov 2010 16:57:20 +0100 Subject: =?UTF-8?q?incorporated=20most=20of=20the=20feedback=20from=20Jos?= =?UTF-8?q?=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../asset_tag_helpers/javascript_tag_helpers.rb | 24 ++++++++++++---------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb index de581a2737..72615220fa 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb @@ -11,8 +11,13 @@ module ActionView class JavascriptIncludeTag < AssetIncludeTag include TagHelper - self.asset_name = 'javascript' - self.extension = 'js' + def asset_name + 'javascript' + end + + def extension + 'js' + end def asset_tag(source, options) content_tag("script", "", { "type" => Mime::JS, "src" => path_to_asset(source) }.merge(options)) @@ -40,14 +45,8 @@ module ActionView module JavascriptTagHelpers extend ActiveSupport::Concern - extend HelperMacros include CommonAssetHelpers - included do - mattr_accessor :javascript_expansions - self.javascript_expansions = { } - end - module ClassMethods # Register one or more javascript files to be included when symbol # is passed to javascript_include_tag. This method is typically intended @@ -61,7 +60,7 @@ module ActionView # # def register_javascript_expansion(expansions) - self.javascript_expansions.merge!(expansions) + JavascriptIncludeTag.expansions.merge!(expansions) end end @@ -76,7 +75,10 @@ module ActionView # javascript_path "/dir/xmlhr" # => /dir/xmlhr.js # javascript_path "http://www.railsapplication.com/js/xmlhr" # => http://www.railsapplication.com/js/xmlhr # javascript_path "http://www.railsapplication.com/js/xmlhr.js" # => http://www.railsapplication.com/js/xmlhr.js - asset_path :javascript, 'js' + def javascript_path(source) + compute_public_path(source, 'javascripts', 'js') + end + alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route # Returns an HTML script tag for each of the +sources+ provided. You # can pass in the filename (.js extension is optional) of JavaScript files @@ -161,7 +163,7 @@ module ActionView # # javascript_include_tag :all, :cache => true, :recursive => true def javascript_include_tag(*sources) - @javascript_include ||= JavascriptIncludeTag.new(config, controller, self.javascript_expansions) + @javascript_include ||= JavascriptIncludeTag.new(config, controller) @javascript_include.include_tag(*sources) end -- cgit v1.2.3 From 0ff1c5935f84f86a8b88802078b115e9964c2249 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Mon, 15 Nov 2010 17:30:15 +0100 Subject: reorganised the the common asset helpers module into a class and have it include the id caching module, this class is now shared from the view instance to the asset include tag helpers (js and css) --- .../helpers/asset_tag_helpers/javascript_tag_helpers.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb index 72615220fa..6581e1d6f2 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb @@ -1,7 +1,6 @@ require 'active_support/concern' require 'active_support/core_ext/file' require 'action_view/helpers/tag_helper' -require 'action_view/helpers/asset_tag_helpers/common_asset_helpers' require 'action_view/helpers/asset_tag_helpers/asset_include_tag' module ActionView @@ -43,9 +42,9 @@ module ActionView end end + module JavascriptTagHelpers extend ActiveSupport::Concern - include CommonAssetHelpers module ClassMethods # Register one or more javascript files to be included when symbol @@ -76,7 +75,7 @@ module ActionView # javascript_path "http://www.railsapplication.com/js/xmlhr" # => http://www.railsapplication.com/js/xmlhr # javascript_path "http://www.railsapplication.com/js/xmlhr.js" # => http://www.railsapplication.com/js/xmlhr.js def javascript_path(source) - compute_public_path(source, 'javascripts', 'js') + asset_paths.compute_public_path(source, 'javascripts', 'js') end alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route @@ -163,7 +162,7 @@ module ActionView # # javascript_include_tag :all, :cache => true, :recursive => true def javascript_include_tag(*sources) - @javascript_include ||= JavascriptIncludeTag.new(config, controller) + @javascript_include ||= JavascriptIncludeTag.new(config, asset_paths) @javascript_include.include_tag(*sources) end -- cgit v1.2.3