Last active
August 29, 2015 14:07
-
-
Save jackjennings/148754e327c316d16cb7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class VideoBlock < ActiveRecord::Base | |
belongs_to :content_block | |
validates_presence_of :url | |
validate :is_a_vimeo_link | |
validates_length_of :title, maximum: 255 | |
before_save :set_video_id | |
def vimeo_url? | |
self.url.include?('vimeo.com') | |
end | |
private | |
def is_a_vimeo_link | |
errors.add(:url, 'must be from Vimeo') unless vimeo_url? | |
end | |
def set_video_id | |
self.video_id = self.parse_vimeo_id if vimeo_url? | |
end | |
def parse_vimeo_id | |
self.url.split("/").last | |
end | |
def as_json(options = {}) | |
options.reverse_merge! only: [:title, :caption, :url] | |
super(options) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment