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.
This commit is contained in:
@@ -1,7 +1,4 @@
|
|||||||
module EmbedVideosHelper
|
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)
|
def embedded_video_code(resource)
|
||||||
link = resource.video_url
|
link = resource.video_url
|
||||||
title = t("proposals.show.embed_video_title", proposal: resource.title)
|
title = t("proposals.show.embed_video_title", proposal: resource.title)
|
||||||
@@ -12,10 +9,10 @@ module EmbedVideosHelper
|
|||||||
end
|
end
|
||||||
|
|
||||||
if server == "Vimeo"
|
if server == "Vimeo"
|
||||||
reg_exp = VIMEO_REGEX
|
reg_exp = resource.class::VIMEO_REGEX
|
||||||
src = "https://player.vimeo.com/video/"
|
src = "https://player.vimeo.com/video/"
|
||||||
elsif server == "YouTube"
|
elsif server == "YouTube"
|
||||||
reg_exp = YOUTUBE_REGEX
|
reg_exp = resource.class::YOUTUBE_REGEX
|
||||||
src = "https://www.youtube.com/embed/"
|
src = "https://www.youtube.com/embed/"
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -29,12 +26,4 @@ module EmbedVideosHelper
|
|||||||
""
|
""
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
28
app/models/concerns/videoable.rb
Normal file
28
app/models/concerns/videoable.rb
Normal file
@@ -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
|
||||||
@@ -1,17 +1,6 @@
|
|||||||
class Poll::Question::Answer::Video < ApplicationRecord
|
class Poll::Question::Answer::Video < ApplicationRecord
|
||||||
belongs_to :answer, class_name: "Poll::Question::Answer"
|
belongs_to :answer, class_name: "Poll::Question::Answer"
|
||||||
|
include Videoable
|
||||||
VIMEO_REGEX = /vimeo.*(staffpicks\/|channels\/|videos\/|video\/|\/)([^#\&\?]*).*/
|
|
||||||
YOUTUBE_REGEX = /youtu.*(be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/
|
|
||||||
|
|
||||||
validates :title, presence: true
|
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
|
end
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ class Proposal < ApplicationRecord
|
|||||||
include Mappable
|
include Mappable
|
||||||
include Notifiable
|
include Notifiable
|
||||||
include Documentable
|
include Documentable
|
||||||
include EmbedVideosHelper
|
include Videoable
|
||||||
include Relationable
|
include Relationable
|
||||||
include Milestoneable
|
include Milestoneable
|
||||||
include Randomizable
|
include Randomizable
|
||||||
@@ -59,8 +59,6 @@ class Proposal < ApplicationRecord
|
|||||||
|
|
||||||
validates :terms_of_service, acceptance: { allow_nil: false }, on: :create
|
validates :terms_of_service, acceptance: { allow_nil: false }, on: :create
|
||||||
|
|
||||||
validate :valid_video_url?
|
|
||||||
|
|
||||||
before_validation :set_responsible_name
|
before_validation :set_responsible_name
|
||||||
|
|
||||||
before_save :calculate_hot_score, :calculate_confidence_score
|
before_save :calculate_hot_score, :calculate_confidence_score
|
||||||
|
|||||||
Reference in New Issue
Block a user