Skip to content

Commit 858f464

Browse files
committed
reuse common video/audio tags code and do not modify options
1 parent fc54787 commit 858f464

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

actionpack/lib/action_view/helpers/asset_tag_helper.rb

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -416,22 +416,12 @@ def image_alt(src)
416416
# video_tag(["trailer.ogg", "trailer.flv"] :size => "160x120") # =>
417417
# <video height="120" width="160"><source src="/videos/trailer.ogg" /><source src="/videos/trailer.flv" /></video>
418418
def video_tag(*sources)
419-
options = sources.extract_options!.symbolize_keys!
420-
sources.flatten!
419+
multiple_sources_tag('video', sources) do |options|
420+
options[:poster] = path_to_image(options[:poster]) if options[:poster]
421421

422-
options[:poster] = path_to_image(options[:poster]) if options[:poster]
423-
424-
if size = options.delete(:size)
425-
options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
426-
end
427-
428-
if sources.size > 1
429-
content_tag("video", options) do
430-
safe_join sources.map { |source| tag("source", :src => path_to_video(source)) }
422+
if size = options.delete(:size)
423+
options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
431424
end
432-
else
433-
options[:src] = path_to_video(sources.first)
434-
tag("video", options)
435425
end
436426
end
437427

@@ -449,24 +439,30 @@ def video_tag(*sources)
449439
# audio_tag("sound.wav", "sound.mid") # =>
450440
# <audio><source src="/audios/sound.wav" /><source src="/audios/sound.mid" /></audio>
451441
def audio_tag(*sources)
452-
options = sources.extract_options!.symbolize_keys!
453-
sources.flatten!
454-
455-
if sources.size > 1
456-
content_tag("audio", options) do
457-
safe_join sources.collect { |source| tag("source", :src => path_to_audio(source)) }
458-
end
459-
else
460-
options[:src] = path_to_audio(sources.first)
461-
tag("audio", options)
462-
end
442+
multiple_sources_tag('audio', sources)
463443
end
464444

465445
private
466446

467447
def asset_paths
468448
@asset_paths ||= AssetTagHelper::AssetPaths.new(config, controller)
469449
end
450+
451+
def multiple_sources_tag(type, sources)
452+
options = sources.extract_options!.dup.symbolize_keys!
453+
sources.flatten!
454+
455+
yield options if block_given?
456+
457+
if sources.size > 1
458+
content_tag(type, options) do
459+
safe_join sources.map { |source| tag("source", :src => send("path_to_#{type}", source)) }
460+
end
461+
else
462+
options[:src] = send("path_to_#{type}", sources.first)
463+
tag(type, options)
464+
end
465+
end
470466
end
471467
end
472468
end

actionpack/test/template/asset_tag_helper_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,14 @@ def test_audio_tag
494494
AudioLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
495495
end
496496

497+
def test_video_audio_tag_does_not_modify_options
498+
options = {:autoplay => true}
499+
video_tag('video', options)
500+
assert_equal({:autoplay => true}, options)
501+
audio_tag('audio', options)
502+
assert_equal({:autoplay => true}, options)
503+
end
504+
497505
def test_timebased_asset_id
498506
expected_time = File.mtime(File.expand_path(File.dirname(__FILE__) + "/../fixtures/public/images/rails.png")).to_i.to_s
499507
assert_equal %(<img alt="Rails" src="/images/rails.png?#{expected_time}" />), image_tag("rails.png")

0 commit comments

Comments
 (0)