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,5 @@
<div class="small-12 medium-7 small-centered">
<div class="flex-video">
<div id="js-embedded-video" data-video-code="<%= embedded_video_code %>"></div>
</div>
</div>

View File

@@ -1,7 +1,17 @@
module EmbedVideosHelper class Shared::EmbeddedVideoComponent < ApplicationComponent
def embedded_video_code(resource) attr_reader :record
link = resource.video_url
title = t("proposals.show.embed_video_title", proposal: resource.title) 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.*/ if link =~ /vimeo.*/
server = "Vimeo" server = "Vimeo"
elsif link =~ /youtu*.*/ elsif link =~ /youtu*.*/
@@ -9,10 +19,10 @@ module EmbedVideosHelper
end end
if server == "Vimeo" if server == "Vimeo"
reg_exp = resource.class::VIMEO_REGEX reg_exp = record.class::VIMEO_REGEX
src = "https://player.vimeo.com/video/" src = "https://player.vimeo.com/video/"
elsif server == "YouTube" elsif server == "YouTube"
reg_exp = resource.class::YOUTUBE_REGEX reg_exp = record.class::YOUTUBE_REGEX
src = "https://www.youtube.com/embed/" src = "https://www.youtube.com/embed/"
end end

View File

@@ -65,13 +65,7 @@
<blockquote><%= @proposal.summary %></blockquote> <blockquote><%= @proposal.summary %></blockquote>
<% if @proposal.video_url.present? %> <%= render Shared::EmbeddedVideoComponent.new(@proposal) %>
<div class="small-12 medium-7 small-centered">
<div class="flex-video">
<div id="js-embedded-video" data-video-code="<%= embedded_video_code(@proposal) %>"></div>
</div>
</div>
<% end %>
<%= auto_link_already_sanitized_html wysiwyg(@proposal.description) %> <%= auto_link_already_sanitized_html wysiwyg(@proposal.description) %>

View File

@@ -30,13 +30,7 @@
<blockquote><%= @proposal.summary %></blockquote> <blockquote><%= @proposal.summary %></blockquote>
<% if @proposal.video_url.present? %> <%= render Shared::EmbeddedVideoComponent.new(@proposal) %>
<div class="small-12 medium-7 small-centered">
<div class="flex-video">
<div id="js-embedded-video" data-video-code="<%= embedded_video_code(@proposal) %>"></div>
</div>
</div>
<% end %>
<%= auto_link_already_sanitized_html wysiwyg(@proposal.description) %> <%= auto_link_already_sanitized_html wysiwyg(@proposal.description) %>