aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/asset_tag_helper.rb
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib/action_view/helpers/asset_tag_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/asset_tag_helper.rb73
1 files changed, 70 insertions, 3 deletions
diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
index 14cdc7a025..6d2c28f969 100644
--- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
@@ -454,6 +454,21 @@ module ActionView
end
alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
+ # 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") # => /videos/hd.avi
+ # video_path("http://www.railsapplication.com/vid/hd.avi") # => http://www.railsapplication.com/vid/hd.avi
+ def video_path(source)
+ compute_public_path(source, 'videos')
+ end
+ alias_method :path_to_video, :video_path # aliased to avoid conflicts with an video_path named route
+
# 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.
#
@@ -490,8 +505,8 @@ module ActionView
def image_tag(source, options = {})
options.symbolize_keys!
- options[:src] = path_to_image(source)
- options[:alt] ||= File.basename(options[:src], '.*').split('.').first.to_s.capitalize
+ src = options[:src] = path_to_image(source)
+ options[:alt] ||= File.basename(src, '.*').split('.').first.to_s.capitalize
if size = options.delete(:size)
options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
@@ -499,12 +514,64 @@ module ActionView
if mouseover = options.delete(:mouseover)
options[:onmouseover] = "this.src='#{image_path(mouseover)}'"
- options[:onmouseout] = "this.src='#{image_path(options[:src])}'"
+ 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:
+ #
+ # * <tt>:poster</tt> - Set an image (like a screenshot) to be shown
+ # before the video loads. The path is calculated like the +src+ of +image_tag+.
+ # * <tt>:size</tt> - Supplied as "{Width}x{Height}", so "30x45" becomes
+ # width="30" and height="45". <tt>:size</tt> will be ignored if the
+ # value is not in the correct format.
+ #
+ # ==== Examples
+ # video_tag("trailer") # =>
+ # <video src="/videos/trailer" />
+ # video_tag("trailer.ogg") # =>
+ # <video src="/videos/trailer.ogg" />
+ # video_tag("trailer.ogg", :controls => true, :autobuffer => true) # =>
+ # <video autobuffer="autobuffer" controls="controls" src="/videos/trailer.ogg" />
+ # video_tag("trailer.m4v", :size => "16x10", :poster => "screenshot.png") # =>
+ # <video src="/videos/trailer.m4v" width="16" height="10" poster="/images/screenshot.png" />
+ # video_tag("/trailers/hd.avi", :size => "16x16") # =>
+ # <video src="/trailers/hd.avi" width="16" height="16" />
+ # video_tag("/trailers/hd.avi", :height => '32', :width => '32') # =>
+ # <video height="32" src="/trailers/hd.avi" width="32" />
+ # video_tag(["trailer.ogg", "trailer.flv"]) # =>
+ # <video><source src="trailer.ogg" /><source src="trailer.ogg" /><source src="trailer.flv" /></video>
+ # video_tag(["trailer.ogg", "trailer.flv"] :size => "160x120") # =>
+ # <video height="120" width="160"><source src="trailer.ogg" /><source src="trailer.flv" /></video>
+ def video_tag(sources, options = {})
+ options.symbolize_keys!
+
+ options[:poster] = path_to_image(options[:poster]) if options[:poster]
+
+ if size = options.delete(:size)
+ options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
+ end
+
+ if sources.is_a?(Array)
+ content_tag("video", options) do
+ sources.map { |source| tag("source", :src => source) }.join
+ end
+ else
+ options[:src] = path_to_video(sources)
+ tag("video", options)
+ end
+ end
+
def self.cache_asset_timestamps
@@cache_asset_timestamps
end