Allow only YouTube/Vimeo URLs on 'video_url' attribute

Fixes #1841

On branch aperez-valid-video-url
  Changes to be committed:
    modified:   app/models/proposal.rb
    modified:   spec/factories.rb
    modified:   spec/models/proposal_spec.rb

Allow only YouTube/Vimeo URLs on 'video_url' attribute

Fixes #1841

On branch aperez-valid-video-url
  Changes to be committed:
    modified:   app/helpers/embed_videos_helper.rb
    modified:   app/models/proposal.rb
    modified:   spec/features/management/proposals_spec.rb
    modified:   spec/features/proposals_spec.rb
    modified:   spec/features/tags/proposals_spec.rb
    modified:   spec/models/proposal_spec.rb
This commit is contained in:
Angel Perez
2017-09-12 11:42:51 -04:00
parent e573ab72f7
commit 2181a65bc2
7 changed files with 37 additions and 12 deletions

View File

@@ -1,5 +1,8 @@
module EmbedVideosHelper
VIMEO_REGEX = /vimeo.*(staffpicks\/|channels\/|videos\/|video\/|\/)([^#\&\?]*).*/
YOUTUBE_REGEX = /youtu.*(be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/
def embedded_video_code
link = @proposal.video_url
title = t('proposals.show.embed_video_title', proposal: @proposal.title)
@@ -10,10 +13,10 @@ module EmbedVideosHelper
end
if server == "Vimeo"
reg_exp = /vimeo.*(staffpicks\/|channels\/|videos\/|video\/|\/)([^#\&\?]*).*/
reg_exp = VIMEO_REGEX
src = "https://player.vimeo.com/video/"
elsif server == "YouTube"
reg_exp = /youtu.*(be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/
reg_exp = YOUTUBE_REGEX
src = "https://www.youtube.com/embed/"
end
@@ -28,4 +31,11 @@ module EmbedVideosHelper
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

View File

@@ -15,6 +15,7 @@ class Proposal < ActiveRecord::Base
max_file_size: 3.megabytes,
accepted_content_types: [ "application/pdf" ]
accepts_nested_attributes_for :documents, allow_destroy: true
include EmbedVideosHelper
acts_as_votable
acts_as_paranoid column: :hidden_at
@@ -41,6 +42,8 @@ class Proposal < ActiveRecord::Base
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