Save track files in new Sound model:

* Consolidate migrations
* Add Sound model. Each sound can belong to a track and contains
  informations about one sound file.
This commit is contained in:
Thibault Jouan
2011-09-15 21:08:24 +00:00
parent f49a5a3f67
commit 27550fd14e
26 changed files with 265 additions and 216 deletions

18
app/models/sound.rb Normal file
View File

@@ -0,0 +1,18 @@
require 'fileutils'
class Sound < ActiveRecord::Base
belongs_to :track
validates_presence_of :sha256
validates_presence_of :mime_type
def path
"#{Rails.root}/data/sounds/#{sha256}"
end
def file=(file)
self.sha256 = Digest::SHA256.file(file.path).hexdigest
FileUtils.cp file.path, path
self.mime_type = file.content_type
end
end

View File

@@ -1,19 +1,20 @@
class Track < ActiveRecord::Base
validates_presence_of :name
validates_presence_of :mime_type
validates_presence_of :sha256
has_many :sounds
def filepath
"#{Rails.root}/data/tracks/#{sha256}"
attr_accessible :name, :file
validates_presence_of :name
def file=(file)
sounds.build({:file => file})
end
def save_with_file(file, mime_type)
self.sha256 = Digest::SHA256.file(file.path).hexdigest
self.mime_type = mime_type
File.open(filepath, 'w') do |f|
f.write file.read
end
save!
def sound
sounds.first
end
def sound?
sounds.any?
end
def self.latest