Class: Song

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Extensions::UUID
Defined in:
lib/gdshowsdb/models/song.rb

Class Method Summary collapse

Class Method Details

.create_from(spec) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/gdshowsdb/models/song.rb', line 7

def self.create_from(spec)
  song = Song.new
  set_spec(song, spec)
  song.save
  create_song_relationships(spec)
  Song.find_by_uuid(spec[:uuid])
end

.create_song_relationships(spec) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/gdshowsdb/models/song.rb', line 15

def self.create_song_relationships(spec)
  song_ref = SongRef.find_by_name(spec[:name])
  raise "Unable to find a song ref named #{spec[:name]} #{spec.inspect}" unless song_ref
  song_ref.songs << Song.find_by_uuid(spec[:uuid])
  song_ref.song_occurences << SongOccurence.create_from(uuid: SecureRandom.uuid, position: spec[:position], show_set_uuid: spec[:show_set_uuid])
  song_ref.save
end

.find_all_by_year(year) ⇒ Object



59
60
61
# File 'lib/gdshowsdb/models/song.rb', line 59

def self.find_all_by_year(year)
  Song.joins(:show_set => [:show]).where('shows.year = ?', year)
end

.remove_from(spec) ⇒ Object



35
36
37
38
39
# File 'lib/gdshowsdb/models/song.rb', line 35

def self.remove_from(spec)
  remove_song_relationships(spec)
  song = Song.find_by_uuid(spec[:uuid])
  song.delete if song
end

.remove_name(spec) ⇒ Object



64
65
66
# File 'lib/gdshowsdb/models/song.rb', line 64

def self.remove_name(spec)
  spec.reject {|k,v| k == :name }
end

.remove_song_relationships(spec) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gdshowsdb/models/song.rb', line 41

def self.remove_song_relationships(spec)
  song_ref = SongRef.find_by_name(spec[:name])
  return unless song_ref
  
  song = Song.find_by_uuid(spec[:uuid])
  return unless song

  song_ref.songs.delete(song) if song_ref.songs
  show_set = ShowSet.find_by_uuid(spec[:show_set_uuid])
  
  show = ShowSet.find_by_uuid(spec[:show_set_uuid]).show

  song_ref.song_occurences.where('show_uuid = ? and song_ref_uuid = ?', show.uuid, song_ref.uuid).each do | occurence|
    song_ref.song_occurences.delete(occurence)
    occurence.delete
  end
end

.set_spec(song, spec) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/gdshowsdb/models/song.rb', line 68

def self.set_spec(song, spec)
  song.uuid = spec[:uuid]
  song.song_ref_uuid = spec[:song_ref_uuid]
  song.show_set_uuid = spec[:show_set_uuid]
  song.show_set = spec[:show_set]
  song.position = spec[:position]
  song.segued = spec[:segued]
  song.show_set = ShowSet.find_by_uuid(spec[:show_set_uuid])
  song
end

.update_from(spec) ⇒ Object



23
24
25
26
27
28
# File 'lib/gdshowsdb/models/song.rb', line 23

def self.update_from(spec)
  song = Song.find_by_uuid(spec[:uuid])
  set_spec(song, spec)
  song.save
  song
end

.update_song_relationships(spec) ⇒ Object



30
31
32
33
# File 'lib/gdshowsdb/models/song.rb', line 30

def self.update_song_relationships(spec)
  remove_song_relationships(spec)
  create_song_relationships(spec)
end