-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbroadcast.rb
73 lines (54 loc) · 1.5 KB
/
broadcast.rb
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# frozen_string_literal: true
# == Schema Information
#
# Table name: broadcasts
#
# id :integer not null, primary key
# show_id :integer not null
# label :string not null
# started_at :datetime not null
# finished_at :datetime not null
# people :string
# details :text
# created_at :datetime
# updated_at :datetime
# updater_id :integer
#
class Broadcast < ApplicationRecord
include NonOverlappable
belongs_to :show
belongs_to :updater, optional: true, class_name: 'User'
has_many :audio_files, dependent: :restrict_with_error
has_many :tracks, dependent: :nullify
validates :label, :started_at, :finished_at, presence: true
validates :started_at, :finished_at, uniqueness: true
before_validation :set_show_label_if_empty
before_save :set_user_stamps
after_create :assign_tracks
scope :list, -> { order('broadcasts.started_at') }
class << self
def at(timestamp)
where('broadcasts.started_at <= ? AND broadcasts.finished_at > ?', timestamp, timestamp)
end
end
def to_s
I18n.l(started_at)
end
# duration in seconds
def duration
finished_at - started_at
end
private
def set_show_label_if_empty
self.label ||= show.name if show
end
def assign_tracks
Track
.where(tracks: { started_at: started_at..finished_at })
.update_all(broadcast_id: id)
end
def set_user_stamps
return unless User.current
self.updater = User.current
end
end