photomix/app/models/album.rb
2009-06-04 22:28:28 +02:00

89 lines
2.6 KiB
Ruby

class Album < ActiveRecord::Base
has_many :photos, :dependent => :destroy
has_many :collection_albums
has_many :collections, :through => :collection_albums
validates_uniqueness_of :path, :message => "Album already exsists on disc"
before_validation :ensure_path
after_create :create_folders
after_destroy :destroy_folders
attr_accessor :tag_list
attr_protected :path
def self.untouched
self.find(:all, :conditions => "Albums.Id IN ( SELECT DISTINCT Photos.Album_Id FROM Photos WHERE Photos.description IS NULL AND Photos.Id NOT IN ( SELECT Photo_ID FROM Photo_Tags) )" )
end
def ensure_path
self.path = self.title if !self.path
end
def tag_list
# should maybe cache this to database?
# should maybe return array instead?
tags = Array.new
self.photos.map{ |photo|
if photo.tags.empty?
# photo has no tags => no unversial tags for this album
return
else
photo.tags
end
}.each_with_index{ |photo_tags,i|
# returns tag collection for each photo
if i == 0
tags = photo_tags
else
# combine arrays if they have identical tags.
# Will remove tags that are only tagged to one photo
tags = tags & photo_tags
end
}
return tags.collect{|tag| tag.title }.sort.join(" ")
end
def tag_list=(tags)
return if tags.split(" ").sort.join(" ") == self.tag_list
current_tags = ( self.tag_list.nil? ? [] : self.tag_list.split(" ") )
tags = tags.split(" ")
# find tags that should be removed from this album - thus remove from all photos in album
# i.e. tags listed in self.tag_list but no in parameter tags
#current_tags.map {|tag|tag if !tags.include?(tag) }.compact
(current_tags - tags).each { |tag|
#puts "remove #{tag}"
self.photos.each {|photo|
photo.untag( tag )
}
}
# add universial tags to all photos in album
tags.each do |tag|
#puts "tag photos with #{tag}" if !current_tags.include?( tag )
self.photos.each { |photo|
photo.tag( tag ) if !current_tags.include?( tag ) # no need to re-tag
}
end
end
protected
private
def create_folders
Dir.mkdir( APP_CONFIG[:photos_path] + self.path )
Dir.mkdir( APP_CONFIG[:thumbs_path] + self.path )
end
def destroy_folders
#puts "DELETE DIRECTORY " + APP_CONFIG[:photos_path] + self.path
Dir.delete( APP_CONFIG[:thumbs_path] + self.path ) if File.exists?( APP_CONFIG[:thumbs_path] + self.path )
Dir.delete( APP_CONFIG[:photos_path] + self.path ) if File.exists?( APP_CONFIG[:photos_path] + self.path )
end
end