Extract component to render an embedded video

This commit is contained in:
Javi Martín
2024-05-19 04:29:02 +02:00
parent 236b58ab01
commit 579e332cf8
4 changed files with 23 additions and 20 deletions

View File

@@ -0,0 +1,39 @@
class Shared::EmbeddedVideoComponent < ApplicationComponent
attr_reader :record
def initialize(record)
@record = record
end
def render?
record.video_url.present?
end
def embedded_video_code
link = record.video_url
title = t("proposals.show.embed_video_title", proposal: record.title)
if link =~ /vimeo.*/
server = "Vimeo"
elsif link =~ /youtu*.*/
server = "YouTube"
end
if server == "Vimeo"
reg_exp = record.class::VIMEO_REGEX
src = "https://player.vimeo.com/video/"
elsif server == "YouTube"
reg_exp = record.class::YOUTUBE_REGEX
src = "https://www.youtube.com/embed/"
end
if reg_exp
match = link.match(reg_exp)
end
if match && match[2]
'<iframe src="' + src + match[2] + '" style="border:0;" allowfullscreen title="' + title + '"></iframe>'
else
""
end
end
end