Skip to content

Instantly share code, notes, and snippets.

@jackjennings
Last active August 29, 2015 14:07
Show Gist options
  • Save jackjennings/148754e327c316d16cb7 to your computer and use it in GitHub Desktop.
Save jackjennings/148754e327c316d16cb7 to your computer and use it in GitHub Desktop.
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