Files
nairobi/app/models/concerns/videoable.rb
Javi Martín 236b58ab01 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.
2024-06-06 17:35:27 +02:00

29 lines
587 B
Ruby

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