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/asset_id_caching.rb | 67 +++++ .../asset_tag_helpers/base_asset_helpers.rb | 330 +++++++++++++++++++++ .../helpers/asset_tag_helpers/helper_methods.rb | 67 +++++ .../asset_tag_helpers/javascript_tag_helpers.rb | 13 +- .../asset_tag_helpers/stylesheet_tag_helpers.rb | 13 +- 5 files changed, 482 insertions(+), 8 deletions(-) create mode 100644 actionpack/lib/action_view/helpers/asset_tag_helpers/asset_id_caching.rb create mode 100644 actionpack/lib/action_view/helpers/asset_tag_helpers/base_asset_helpers.rb create mode 100644 actionpack/lib/action_view/helpers/asset_tag_helpers/helper_methods.rb (limited to 'actionpack/lib/action_view/helpers/asset_tag_helpers') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/asset_id_caching.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/asset_id_caching.rb new file mode 100644 index 0000000000..36fe961a72 --- /dev/null +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/asset_id_caching.rb @@ -0,0 +1,67 @@ +require 'active_support/concern' + +module ActionView + module Helpers + module AssetTagHelper + + module AssetIdCaching + extend ActiveSupport::Concern + + included do + # You can enable or disable the asset tag timestamps cache. + # With the cache enabled, the asset tag helper methods will make fewer + # expensive file system calls. However this prevents you from modifying + # any asset files while the server is running. + # + # ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false + mattr_accessor :cache_asset_timestamps + + private + mattr_accessor :asset_timestamps_cache + self.asset_timestamps_cache = {} + + mattr_accessor :asset_timestamps_cache_guard + self.asset_timestamps_cache_guard = Mutex.new + end + + private + + # Use the RAILS_ASSET_ID environment variable or the source's + # modification time as its cache-busting asset id. + def rails_asset_id(source) + if asset_id = ENV["RAILS_ASSET_ID"] + asset_id + else + if self.cache_asset_timestamps && (asset_id = self.asset_timestamps_cache[source]) + asset_id + else + path = File.join(config.assets_dir, source) + asset_id = File.exist?(path) ? File.mtime(path).to_i.to_s : '' + + if self.cache_asset_timestamps + self.asset_timestamps_cache_guard.synchronize do + self.asset_timestamps_cache[source] = asset_id + end + end + + asset_id + end + end + end + + # Break out the asset path rewrite in case plugins wish to put the asset id + # someplace other than the query string. + # This is the default implementation + def handle_asset_id(source) + asset_id = rails_asset_id(source) + if asset_id.empty? + source + else + "#{source}?#{asset_id}" + end + end + end + + end + end +end \ No newline at end of file diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/base_asset_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/base_asset_helpers.rb new file mode 100644 index 0000000000..7dbb0fa70f --- /dev/null +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/base_asset_helpers.rb @@ -0,0 +1,330 @@ +require 'active_support/concern' +require 'action_view/helpers/asset_tag_helpers/helper_methods' + +module ActionView + module Helpers + module AssetTagHelper + + module BaseAssetHelpers + extend HelperMethods + # Returns a link tag that browsers and news readers can use to auto-detect + # an RSS or ATOM feed. The +type+ can either be :rss (default) or + # :atom. Control the link options in url_for format using the + # +url_options+. You can modify the LINK tag itself in +tag_options+. + # + # ==== Options + # * :rel - Specify the relation of this link, defaults to "alternate" + # * :type - Override the auto-generated mime type + # * :title - Specify the title of the link, defaults to the +type+ + # + # ==== Examples + # auto_discovery_link_tag # => + # + # auto_discovery_link_tag(:atom) # => + # + # auto_discovery_link_tag(:rss, {:action => "feed"}) # => + # + # auto_discovery_link_tag(:rss, {:action => "feed"}, {:title => "My RSS"}) # => + # + # auto_discovery_link_tag(:rss, {:controller => "news", :action => "feed"}) # => + # + # auto_discovery_link_tag(:rss, "http://www.example.com/feed.rss", {:title => "Example RSS"}) # => + # + def auto_discovery_link_tag(type = :rss, url_options = {}, tag_options = {}) + tag( + "link", + "rel" => tag_options[:rel] || "alternate", + "type" => tag_options[:type] || Mime::Type.lookup_by_extension(type.to_s).to_s, + "title" => tag_options[:title] || type.to_s.upcase, + "href" => url_options.is_a?(Hash) ? url_for(url_options.merge(:only_path => false)) : url_options + ) + end + + # Web browsers cache favicons. If you just throw a favicon.ico into the document + # root of your application and it changes later, clients that have it in their cache + # won't see the update. Using this helper prevents that because it appends an asset ID: + # + # <%= favicon_link_tag %> + # + # generates + # + # + # + # You may specify a different file in the first argument: + # + # <%= favicon_link_tag 'favicon.ico' %> + # + # That's passed to +path_to_image+ as is, so it gives + # + # + # + # The helper accepts an additional options hash where you can override "rel" and "type". + # + # For example, Mobile Safari looks for a different LINK tag, pointing to an image that + # will be used if you add the page to the home screen of an iPod Touch, iPhone, or iPad. + # The following call would generate such a tag: + # + # <%= favicon_link_tag 'mb-icon.png', :rel => 'apple-touch-icon', :type => 'image/png' %> + # + def favicon_link_tag(source='/favicon.ico', options={}) + tag('link', { + :rel => 'shortcut icon', + :type => 'image/vnd.microsoft.icon', + :href => path_to_image(source) + }.merge(options.symbolize_keys)) + end + + # Computes the path to an image asset in the public images directory. + # Full paths from the document root will be passed through. + # Used internally by +image_tag+ to build the image path: + # + # image_path("edit") # => "/images/edit" + # image_path("edit.png") # => "/images/edit.png" + # image_path("icons/edit.png") # => "/images/icons/edit.png" + # image_path("/icons/edit.png") # => "/icons/edit.png" + # image_path("http://www.railsapplication.com/img/edit.png") # => "http://www.railsapplication.com/img/edit.png" + # + # If you have images as application resources this method may conflict with their named routes. + # The alias +path_to_image+ is provided to avoid that. Rails uses the alias internally, and + # plugin authors are encouraged to do so. + asset_path :image + + # Computes the path to a video asset in the public videos directory. + # Full paths from the document root will be passed through. + # Used internally by +video_tag+ to build the video path. + # + # ==== Examples + # video_path("hd") # => /videos/hd + # video_path("hd.avi") # => /videos/hd.avi + # video_path("trailers/hd.avi") # => /videos/trailers/hd.avi + # video_path("/trailers/hd.avi") # => /trailers/hd.avi + # video_path("http://www.railsapplication.com/vid/hd.avi") # => http://www.railsapplication.com/vid/hd.avi + asset_path :video + + # Computes the path to an audio asset in the public audios directory. + # Full paths from the document root will be passed through. + # Used internally by +audio_tag+ to build the audio path. + # + # ==== Examples + # audio_path("horse") # => /audios/horse + # audio_path("horse.wav") # => /audios/horse.wav + # audio_path("sounds/horse.wav") # => /audios/sounds/horse.wav + # audio_path("/sounds/horse.wav") # => /sounds/horse.wav + # audio_path("http://www.railsapplication.com/sounds/horse.wav") # => http://www.railsapplication.com/sounds/horse.wav + asset_path :audio + + # Returns an html image tag for the +source+. The +source+ can be a full + # path or a file that exists in your public images directory. + # + # ==== Options + # You can add HTML attributes using the +options+. The +options+ supports + # three additional keys for convenience and conformance: + # + # * :alt - If no alt text is given, the file name part of the + # +source+ is used (capitalized and without the extension) + # * :size - Supplied as "{Width}x{Height}", so "30x45" becomes + # width="30" and height="45". :size will be ignored if the + # value is not in the correct format. + # * :mouseover - Set an alternate image to be used when the onmouseover + # event is fired, and sets the original image to be replaced onmouseout. + # This can be used to implement an easy image toggle that fires on onmouseover. + # + # ==== Examples + # image_tag("icon") # => + # Icon + # image_tag("icon.png") # => + # Icon + # image_tag("icon.png", :size => "16x10", :alt => "Edit Entry") # => + # Edit Entry + # image_tag("/icons/icon.gif", :size => "16x16") # => + # Icon + # image_tag("/icons/icon.gif", :height => '32', :width => '32') # => + # Icon + # image_tag("/icons/icon.gif", :class => "menu_icon") # => + # Icon + # image_tag("mouse.png", :mouseover => "/images/mouse_over.png") # => + # Mouse + # image_tag("mouse.png", :mouseover => image_path("mouse_over.png")) # => + # Mouse + def image_tag(source, options = {}) + options.symbolize_keys! + + src = options[:src] = path_to_image(source) + + unless src =~ /^cid:/ + options[:alt] = options.fetch(:alt){ File.basename(src, '.*').capitalize } + end + + if size = options.delete(:size) + options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$} + end + + if mouseover = options.delete(:mouseover) + options[:onmouseover] = "this.src='#{path_to_image(mouseover)}'" + options[:onmouseout] = "this.src='#{src}'" + end + + tag("img", options) + end + + # Returns an html video tag for the +sources+. If +sources+ is a string, + # a single video tag will be returned. If +sources+ is an array, a video + # tag with nested source tags for each source will be returned. The + # +sources+ can be full paths or files that exists in your public videos + # directory. + # + # ==== Options + # You can add HTML attributes using the +options+. The +options+ supports + # two additional keys for convenience and conformance: + # + # * :poster - Set an image (like a screenshot) to be shown + # before the video loads. The path is calculated like the +src+ of +image_tag+. + # * :size - Supplied as "{Width}x{Height}", so "30x45" becomes + # width="30" and height="45". :size will be ignored if the + # value is not in the correct format. + # + # ==== Examples + # video_tag("trailer") # => + #