photomix/app/models/photo.rb

37 lines
1.5 KiB
Ruby
Raw Normal View History

2009-05-22 14:13:46 +02:00
class Photo < ActiveRecord::Base
belongs_to :album
2009-05-22 22:38:52 +02:00
has_many :photo_tags, :dependent => :destroy
has_many :tags, :through => :photo_tags
2009-05-22 21:04:41 +02:00
2009-05-25 21:39:43 +02:00
#accepts_nested_attributes_for :photo_tags, :allow_destroy => true
2009-05-22 21:04:41 +02:00
validates_uniqueness_of :path, :message => "Photo already exsists on disc"
2009-05-25 21:39:43 +02:00
validates_presence_of :title
2009-05-22 22:38:52 +02:00
before_destroy :destroy_file
2009-05-25 21:39:43 +02:00
attr_accessor :tag_list
def tag_list
return self.tags.find(:all, :order => 'title').collect{ |t| t.title }.join(" ")
end
def tag_list=(tags)
ts = Array.new
tags.split(" ").each do |tag|
ts.push( Tag.find_or_create_by_title( :title => tag) )
end
self.tags = ts
end
2009-05-22 22:38:52 +02:00
private
2009-05-25 21:39:43 +02:00
2009-05-22 22:38:52 +02:00
def destroy_file
puts "DELETE FILE " + APP_CONFIG[:photos_path] + self.path
File.delete( APP_CONFIG[:photos_path] + self.path ) if File.exists?( APP_CONFIG[:photos_path] + self.path )
File.delete( APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_small" + File.extname( APP_CONFIG[:photos_path] + self.path ) ) if File.exists?( APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_small" + File.extname( APP_CONFIG[:photos_path] + self.path ) )
File.delete( APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_large" + File.extname( APP_CONFIG[:photos_path] + self.path ) ) if File.exists?( APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_large" + File.extname( APP_CONFIG[:photos_path] + self.path ) )
end
2009-05-22 14:13:46 +02:00
end