video.rb
1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- encoding : utf-8 -*-
# Author - Igor Portela - igorportela.com | Copyright(c) 2013. All rights reserved.
# == Schema Information
#
# Table name: videos
#
# id :integer(4) not null, primary key
# url :string(255)
# legend :string(255)
# window_size :string(255)
# window_position :string(255)
# created_at :datetime not null
# updated_at :datetime not null
# closed_caption :integer(4)
# transparency :integer(4)
# user_id :integer(4)
#
class Video < ActiveRecord::Base
# Relationships
belongs_to :user
# Scope
scope :by_user, select("id, user_id").group("user_id").order('user_id')
scope :only_user, lambda{ |user_id| select("id, user_id").where(user_id: user_id).group("user_id").order('user_id') }
scope :by_month, select("id").where("month(created_at) = ? and year(created_at) = ?",Time.now.month, Time.now.year).order('id')
scope :by_one_user, lambda{ |user_id| where(user_id: user_id)}
# Protection
attr_accessible :id, :url, :legend, :window_size, :window_position, :remove_legend, :legend_cache, :remove_url, :url_cache, :closed_caption, :transparency, :user_id
# Foi adicionado :user_id para um video só poder ser criado com um usuário;
# Validates
validates_presence_of :url, :user_id
# Scope
default_scope order('created_at desc')
# Uploader
mount_uploader :url, VideoUploader
mount_uploader :legend, SubtitleUploader
# Rules
before_save :default
def default
if self.transparency == nil
self.transparency = 0
end
end
def name
id
end
end