lib/gdshowsdb/models/song.rb in gdshowsdb-0.9.1 vs lib/gdshowsdb/models/song.rb in gdshowsdb-1.0.0

- old
+ new

@@ -2,34 +2,30 @@ include Extensions::UUID belongs_to :show_set, :foreign_key => :show_set_uuid, :primary_key => :uuid belongs_to :song_ref, :foreign_key => :song_ref_uuid, :primary_key => :uuid - attr_accessible :uuid, :song_ref_uuid, :show_set_uuid, :show_set, :position, :segued - - def self.create_from(spec) - Song.create(remove_name(spec)) do |song| - song.show_set = ShowSet.find_by_uuid(spec[:show_set_uuid]) - end + 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 def self.create_song_relationships(spec) song_ref = SongRef.find_by_name(spec[:name]) song_ref.songs << Song.find_by_uuid(spec[:uuid]) - - song_ref.song_occurences.create(uuid: SecureRandom.uuid, position: spec[:position]) do |occurence| - occurence.show = ShowSet.find_by_uuid(spec[:show_set_uuid]).show - end + song_ref.song_occurences << SongOccurence.create_from(uuid: SecureRandom.uuid, position: spec[:position], show_set_uuid: spec[:show_set_uuid]) + song_ref.save end def self.update_from(spec) - read_song = Song.find_by_uuid(spec[:uuid]) - read_song.show_set = ShowSet.find_by_uuid(spec[:show_set_uuid]) - read_song.update(remove_name(spec)) - Song.find_by_uuid(spec[:uuid]) + song = Song.find_by_uuid(spec[:uuid]) + set_spec(song, spec) + song.save + song end def self.update_song_relationships(spec) remove_song_relationships(spec) create_song_relationships(spec) @@ -66,7 +62,18 @@ end private def self.remove_name(spec) spec.reject {|k,v| k == :name } + end + + 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 end \ No newline at end of file