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/stylesheet_tag_helpers.rb | 164 +++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb (limited to 'actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb new file mode 100644 index 0000000000..6ae4792b70 --- /dev/null +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb @@ -0,0 +1,164 @@ +module ActionView + module Helpers + module AssetTagHelper + module StylesheetTagHelpers + extend ActiveSupport::Concern + + included do + mattr_accessor :stylesheet_expansions + self.stylesheet_expansions = { } + end + + module ClassMethods + # 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 register_stylesheet_expansion(expansions) + self.stylesheet_expansions.merge!(expansions) + end + end + + # Computes the path to a stylesheet asset in the public stylesheets directory. + # If the +source+ filename has no extension, .css will be appended (except for explicit URIs). + # Full paths from the document root will be passed through. + # Used internally by +stylesheet_link_tag+ to build the stylesheet path. + # + # ==== Examples + # stylesheet_path "style" # => /stylesheets/style.css + # stylesheet_path "dir/style.css" # => /stylesheets/dir/style.css + # stylesheet_path "/dir/style.css" # => /dir/style.css + # stylesheet_path "http://www.railsapplication.com/css/style" # => http://www.railsapplication.com/css/style + # stylesheet_path "http://www.railsapplication.com/css/style.css" # => http://www.railsapplication.com/css/style.css + def stylesheet_path(source) + compute_public_path(source, 'stylesheets', 'css') + end + alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route + + # Returns a stylesheet link tag for the sources specified as arguments. If + # you don't specify an extension, .css will be appended automatically. + # You can modify the link attributes by passing a hash as the last argument. + # + # ==== Examples + # stylesheet_link_tag "style" # => + # + # + # stylesheet_link_tag "style.css" # => + # + # + # stylesheet_link_tag "http://www.railsapplication.com/style.css" # => + # + # + # stylesheet_link_tag "style", :media => "all" # => + # + # + # stylesheet_link_tag "style", :media => "print" # => + # + # + # stylesheet_link_tag "random.styles", "/css/stylish" # => + # + # + # + # You can also include all styles in the stylesheets directory using :all as the source: + # + # stylesheet_link_tag :all # => + # + # + # + # + # If you want Rails to search in all the subdirectories under stylesheets, you should explicitly set :recursive: + # + # stylesheet_link_tag :all, :recursive => true + # + # == Caching multiple stylesheets into one + # + # You can also cache multiple stylesheets into one file, which requires less HTTP connections 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: + # + # ==== Examples + # stylesheet_link_tag :all, :cache => true # when config.perform_caching is false => + # + # + # + # + # stylesheet_link_tag :all, :cache => true # when config.perform_caching is true => + # + # + # stylesheet_link_tag "shop", "cart", "checkout", :cache => "payment" # when config.perform_caching is false => + # + # + # + # + # stylesheet_link_tag "shop", "cart", "checkout", :cache => "payment" # when config.perform_caching is true => + # + # + # The :recursive option is also available for caching: + # + # stylesheet_link_tag :all, :cache => true, :recursive => true + # + # To force concatenation (even in development mode) set :concat to true. This is useful if + # you have too many stylesheets for IE to load. + # + # stylesheet_link_tag :all, :concat => true + # + def stylesheet_link_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_stylesheet_name = (cache == true ? "all" : cache) + ".css" + joined_stylesheet_path = File.join(joined_stylesheet_name[/^#{File::SEPARATOR}/] ? config.assets_dir : config.stylesheets_dir, joined_stylesheet_name) + + unless config.perform_caching && File.exists?(joined_stylesheet_path) + write_asset_file_contents(joined_stylesheet_path, compute_stylesheet_paths(sources, recursive)) + end + stylesheet_tag(joined_stylesheet_name, options) + else + sources = expand_stylesheet_sources(sources, recursive) + ensure_stylesheet_sources!(sources) if cache + sources.collect { |source| stylesheet_tag(source, options) }.join("\n").html_safe + end + end + + private + + def stylesheet_tag(source, options) + tag("link", { "rel" => "stylesheet", "type" => Mime::CSS, "media" => "screen", "href" => ERB::Util.html_escape(path_to_stylesheet(source)) }.merge(options), false, false) + end + + def compute_stylesheet_paths(*args) + expand_stylesheet_sources(*args).collect { |source| compute_public_path(source, 'stylesheets', 'css', false) } + end + + def expand_stylesheet_sources(sources, recursive) + if sources.first == :all + collect_asset_files(config.stylesheets_dir, ('**' if recursive), '*.css') + else + sources.collect do |source| + determine_source(source, self.stylesheet_expansions) + end.flatten + end + end + + def ensure_stylesheet_sources!(sources) + sources.each do |source| + asset_file_path!(path_to_stylesheet(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/stylesheet_tag_helpers.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb index 6ae4792b70..1f18e9dfce 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_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 StylesheetTagHelpers extend ActiveSupport::Concern + extend HelperMethods + include SharedHelpers included do mattr_accessor :stylesheet_expansions @@ -37,10 +43,8 @@ module ActionView # stylesheet_path "/dir/style.css" # => /dir/style.css # stylesheet_path "http://www.railsapplication.com/css/style" # => http://www.railsapplication.com/css/style # stylesheet_path "http://www.railsapplication.com/css/style.css" # => http://www.railsapplication.com/css/style.css - def stylesheet_path(source) - compute_public_path(source, 'stylesheets', 'css') - end - alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route + asset_path :stylesheet, 'css' + # Returns a stylesheet link tag for the sources specified as arguments. If # you don't specify an extension, .css will be appended automatically. @@ -159,6 +163,7 @@ module ActionView 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/stylesheet_tag_helpers.rb | 71 +++++++--------------- 1 file changed, 23 insertions(+), 48 deletions(-) (limited to 'actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb index 1f18e9dfce..e6a2863558 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb @@ -1,14 +1,32 @@ 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 StylesheetIncludeTag < AssetIncludeTag + include TagHelper + + self.asset_name = 'stylesheet' + self.extension = 'css' + + def asset_tag(source, options) + tag("link", { "rel" => "stylesheet", "type" => Mime::CSS, "media" => "screen", "href" => ERB::Util.html_escape(path_to_asset(source)) }.merge(options), false, false) + end + + def custom_dir + config.stylesheets_dir + end + end + module StylesheetTagHelpers extend ActiveSupport::Concern - extend HelperMethods - include SharedHelpers + extend HelperMacros + include CommonAssetHelpers included do mattr_accessor :stylesheet_expansions @@ -115,53 +133,10 @@ module ActionView # stylesheet_link_tag :all, :concat => true # def stylesheet_link_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_stylesheet_name = (cache == true ? "all" : cache) + ".css" - joined_stylesheet_path = File.join(joined_stylesheet_name[/^#{File::SEPARATOR}/] ? config.assets_dir : config.stylesheets_dir, joined_stylesheet_name) - - unless config.perform_caching && File.exists?(joined_stylesheet_path) - write_asset_file_contents(joined_stylesheet_path, compute_stylesheet_paths(sources, recursive)) - end - stylesheet_tag(joined_stylesheet_name, options) - else - sources = expand_stylesheet_sources(sources, recursive) - ensure_stylesheet_sources!(sources) if cache - sources.collect { |source| stylesheet_tag(source, options) }.join("\n").html_safe - end + @stylesheet_include ||= StylesheetIncludeTag.new(config, controller, self.stylesheet_expansions) + @stylesheet_include.include_tag(*sources) end - private - - def stylesheet_tag(source, options) - tag("link", { "rel" => "stylesheet", "type" => Mime::CSS, "media" => "screen", "href" => ERB::Util.html_escape(path_to_stylesheet(source)) }.merge(options), false, false) - end - - def compute_stylesheet_paths(*args) - expand_stylesheet_sources(*args).collect { |source| compute_public_path(source, 'stylesheets', 'css', false) } - end - - def expand_stylesheet_sources(sources, recursive) - if sources.first == :all - collect_asset_files(config.stylesheets_dir, ('**' if recursive), '*.css') - else - sources.collect do |source| - determine_source(source, self.stylesheet_expansions) - end.flatten - end - end - - def ensure_stylesheet_sources!(sources) - sources.each do |source| - asset_file_path!(path_to_stylesheet(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/stylesheet_tag_helpers.rb | 25 +++++++++++----------- 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb index e6a2863558..1289ca383a 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb @@ -11,8 +11,13 @@ module ActionView class StylesheetIncludeTag < AssetIncludeTag include TagHelper - self.asset_name = 'stylesheet' - self.extension = 'css' + def asset_name + 'stylesheet' + end + + def extension + 'css' + end def asset_tag(source, options) tag("link", { "rel" => "stylesheet", "type" => Mime::CSS, "media" => "screen", "href" => ERB::Util.html_escape(path_to_asset(source)) }.merge(options), false, false) @@ -25,14 +30,8 @@ module ActionView module StylesheetTagHelpers extend ActiveSupport::Concern - extend HelperMacros include CommonAssetHelpers - included do - mattr_accessor :stylesheet_expansions - self.stylesheet_expansions = { } - end - module ClassMethods # Register one or more stylesheet files to be included when symbol # is passed to stylesheet_link_tag. This method is typically intended @@ -46,7 +45,7 @@ module ActionView # # def register_stylesheet_expansion(expansions) - self.stylesheet_expansions.merge!(expansions) + StylesheetIncludeTag.expansions.merge!(expansions) end end @@ -61,8 +60,10 @@ module ActionView # stylesheet_path "/dir/style.css" # => /dir/style.css # stylesheet_path "http://www.railsapplication.com/css/style" # => http://www.railsapplication.com/css/style # stylesheet_path "http://www.railsapplication.com/css/style.css" # => http://www.railsapplication.com/css/style.css - asset_path :stylesheet, 'css' - + def stylesheet_path(source) + compute_public_path(source, 'stylesheets', 'css') + end + alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route # Returns a stylesheet link tag for the sources specified as arguments. If # you don't specify an extension, .css will be appended automatically. @@ -133,7 +134,7 @@ module ActionView # stylesheet_link_tag :all, :concat => true # def stylesheet_link_tag(*sources) - @stylesheet_include ||= StylesheetIncludeTag.new(config, controller, self.stylesheet_expansions) + @stylesheet_include ||= StylesheetIncludeTag.new(config, controller) @stylesheet_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/stylesheet_tag_helpers.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb index 1289ca383a..d02b28d7f6 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_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 @@ -28,9 +27,9 @@ module ActionView end end + module StylesheetTagHelpers extend ActiveSupport::Concern - include CommonAssetHelpers module ClassMethods # Register one or more stylesheet files to be included when symbol @@ -61,7 +60,7 @@ module ActionView # stylesheet_path "http://www.railsapplication.com/css/style" # => http://www.railsapplication.com/css/style # stylesheet_path "http://www.railsapplication.com/css/style.css" # => http://www.railsapplication.com/css/style.css def stylesheet_path(source) - compute_public_path(source, 'stylesheets', 'css') + asset_paths.compute_public_path(source, 'stylesheets', 'css') end alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route @@ -134,7 +133,7 @@ module ActionView # stylesheet_link_tag :all, :concat => true # def stylesheet_link_tag(*sources) - @stylesheet_include ||= StylesheetIncludeTag.new(config, controller) + @stylesheet_include ||= StylesheetIncludeTag.new(config, asset_paths) @stylesheet_include.include_tag(*sources) end -- cgit v1.2.3