Create 'poll_question_answer_videos' table and model

This commit is contained in:
Angel Perez
2017-10-04 17:25:51 -04:00
parent f4774894e7
commit 32f3b643b0
4 changed files with 38 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
class Poll::Question::Answer < ActiveRecord::Base
belongs_to :question, class_name: 'Poll::Question', foreign_key: 'question_id'
has_many :videos, class_name: 'Poll::Question::Answer::Video'
validates :title, presence: true

View File

@@ -0,0 +1,16 @@
class Poll::Question::Answer::Video < ActiveRecord::Base
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

View File

@@ -0,0 +1,11 @@
class CreatePollQuestionAnswerVideos < ActiveRecord::Migration
def change
create_table :poll_question_answer_videos do |t|
t.string :title
t.string :url
t.integer :answer_id, index: true
end
add_foreign_key :poll_question_answer_videos, :poll_question_answers, column: :answer_id
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20171004151553) do
ActiveRecord::Schema.define(version: 20171004210108) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -668,6 +668,14 @@ ActiveRecord::Schema.define(version: 20171004151553) do
add_index "poll_partial_results", ["origin"], name: "index_poll_partial_results_on_origin", using: :btree
add_index "poll_partial_results", ["question_id"], name: "index_poll_partial_results_on_question_id", using: :btree
create_table "poll_question_answer_videos", force: :cascade do |t|
t.string "title"
t.string "url"
t.integer "answer_id"
end
add_index "poll_question_answer_videos", ["answer_id"], name: "index_poll_question_answer_videos_on_answer_id", using: :btree
create_table "poll_question_answers", force: :cascade do |t|
t.string "title"
t.text "description"
@@ -1152,6 +1160,7 @@ ActiveRecord::Schema.define(version: 20171004151553) do
add_foreign_key "poll_partial_results", "poll_officer_assignments", column: "officer_assignment_id"
add_foreign_key "poll_partial_results", "poll_questions", column: "question_id"
add_foreign_key "poll_partial_results", "users", column: "author_id"
add_foreign_key "poll_question_answer_videos", "poll_question_answers", column: "answer_id"
add_foreign_key "poll_question_answers", "poll_questions", column: "question_id"
add_foreign_key "poll_questions", "polls"
add_foreign_key "poll_questions", "proposals"