We were inconsistent on this one. I consider it particularly useful when a method starts with a `return` statement. In other cases, we probably shouldn't have a guard rule in the middle of a method in any case, but that's a different refactoring.
18 lines
528 B
Ruby
18 lines
528 B
Ruby
class Poll::Question::Answer::Video < ApplicationRecord
|
|
belongs_to :answer, class_name: "Poll::Question::Answer", foreign_key: "answer_id"
|
|
|
|
VIMEO_REGEX = /vimeo.*(staffpicks\/|channels\/|videos\/|video\/|\/)([^#\&\?]*).*/
|
|
YOUTUBE_REGEX = /youtu.*(be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/
|
|
|
|
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
|