From 236b58ab0137f04381e92ba3bbb4ba357aab5338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sun, 19 May 2024 04:25:26 +0200 Subject: [PATCH] Remove duplication in code to validate video URL We were using the same code, and the same regular expressions, in two places. To do so, we were including a helper inside a model, which is something we don't usually do. --- app/helpers/embed_videos_helper.rb | 15 ++----------- app/models/concerns/videoable.rb | 28 ++++++++++++++++++++++++ app/models/poll/question/answer/video.rb | 13 +---------- app/models/proposal.rb | 4 +--- 4 files changed, 32 insertions(+), 28 deletions(-) create mode 100644 app/models/concerns/videoable.rb diff --git a/app/helpers/embed_videos_helper.rb b/app/helpers/embed_videos_helper.rb index 08a3179f8..4d050149b 100644 --- a/app/helpers/embed_videos_helper.rb +++ b/app/helpers/embed_videos_helper.rb @@ -1,7 +1,4 @@ module EmbedVideosHelper - VIMEO_REGEX = /vimeo.*(staffpicks\/|channels\/|videos\/|video\/|\/)([^#\&\?]*).*/ - YOUTUBE_REGEX = /youtu.*(be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/ - def embedded_video_code(resource) link = resource.video_url title = t("proposals.show.embed_video_title", proposal: resource.title) @@ -12,10 +9,10 @@ module EmbedVideosHelper end if server == "Vimeo" - reg_exp = VIMEO_REGEX + reg_exp = resource.class::VIMEO_REGEX src = "https://player.vimeo.com/video/" elsif server == "YouTube" - reg_exp = YOUTUBE_REGEX + reg_exp = resource.class::YOUTUBE_REGEX src = "https://www.youtube.com/embed/" end @@ -29,12 +26,4 @@ module EmbedVideosHelper "" end end - - def valid_video_url? - return if video_url.blank? - return if video_url.match(VIMEO_REGEX) - return if video_url.match(YOUTUBE_REGEX) - - errors.add(:video_url, :invalid) - end end diff --git a/app/models/concerns/videoable.rb b/app/models/concerns/videoable.rb new file mode 100644 index 000000000..1febafcce --- /dev/null +++ b/app/models/concerns/videoable.rb @@ -0,0 +1,28 @@ +module Videoable + extend ActiveSupport::Concern + + included do + validate :valid_video_url? + end + + VIMEO_REGEX = /vimeo.*(staffpicks\/|channels\/|videos\/|video\/|\/)([^#\&\?]*).*/ + YOUTUBE_REGEX = /youtu.*(be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/ + + def valid_video_url? + url = send(video_url_field) + + return if url.blank? + return if url.match(VIMEO_REGEX) + return if url.match(YOUTUBE_REGEX) + + errors.add(video_url_field, :invalid) + end + + def video_url_field + if has_attribute?(:video_url) + :video_url + else + :url + end + end +end diff --git a/app/models/poll/question/answer/video.rb b/app/models/poll/question/answer/video.rb index 6cf9e7cc6..e353dd365 100644 --- a/app/models/poll/question/answer/video.rb +++ b/app/models/poll/question/answer/video.rb @@ -1,17 +1,6 @@ class Poll::Question::Answer::Video < ApplicationRecord belongs_to :answer, class_name: "Poll::Question::Answer" - - VIMEO_REGEX = /vimeo.*(staffpicks\/|channels\/|videos\/|video\/|\/)([^#\&\?]*).*/ - YOUTUBE_REGEX = /youtu.*(be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/ + include Videoable validates :title, presence: true - validate :valid_url? - - def valid_url? - return if url.blank? - return if url.match(VIMEO_REGEX) - return if url.match(YOUTUBE_REGEX) - - errors.add(:url, :invalid) - end end diff --git a/app/models/proposal.rb b/app/models/proposal.rb index f1e4f7198..ca4518b07 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -14,7 +14,7 @@ class Proposal < ApplicationRecord include Mappable include Notifiable include Documentable - include EmbedVideosHelper + include Videoable include Relationable include Milestoneable include Randomizable @@ -59,8 +59,6 @@ class Proposal < ApplicationRecord validates :terms_of_service, acceptance: { allow_nil: false }, on: :create - validate :valid_video_url? - before_validation :set_responsible_name before_save :calculate_hot_score, :calculate_confidence_score