photomix/app/models/album.rb

96 lines
3 KiB
Ruby
Raw Normal View History

2009-05-22 14:13:46 +02:00
class Album < ActiveRecord::Base
2009-05-22 22:38:52 +02:00
has_many :photos, :dependent => :destroy
2009-06-03 01:33:39 +02:00
has_many :collection_albums
has_many :collections, :through => :collection_albums
2009-06-02 00:08:57 +02:00
2009-05-22 21:04:41 +02:00
validates_uniqueness_of :path, :message => "Album already exsists on disc"
validates_presence_of :title, :message => "can't be blank"
before_validation :ensure_path, :set_title
2009-06-02 00:08:57 +02:00
after_create :create_folders
after_destroy :destroy_folders
2009-06-02 00:08:57 +02:00
attr_accessor :tag_list
attr_protected :path
2009-06-09 00:30:22 +02:00
named_scope :untouched, :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) )"
2009-06-03 01:33:39 +02:00
2009-06-11 13:05:09 +02:00
def to_param
2009-06-16 01:18:42 +02:00
"#{id}-#{title.parameterize}"
#self.title.gsub(/[^a-z0-9]+/i, '-')
2009-06-11 13:05:09 +02:00
end
2009-06-09 00:30:22 +02:00
2009-06-02 00:08:57 +02:00
def ensure_path
self.path = self.title unless self.path
end
def set_title
self.title = File.basename( File.dirname(self.path) ) unless self.title || !self.path
2009-06-02 00:08:57 +02:00
end
2009-05-22 21:04:41 +02:00
2009-06-02 00:08:57 +02:00
def tag_list
2009-06-03 21:30:09 +02:00
# should maybe cache this to database?
# should maybe return array instead?
2009-06-03 01:33:39 +02:00
2009-06-02 00:08:57 +02:00
tags = Array.new
self.photos.map{ |photo|
if photo.tags.empty?
2009-06-03 01:33:39 +02:00
# photo has no tags => no unversial tags for this album
2009-06-02 00:08:57 +02:00
return
else
photo.tags
2009-06-03 01:33:39 +02:00
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
}
2009-06-03 02:15:23 +02:00
return tags.collect{|tag| tag.title }.sort.join(" ")
2009-06-02 00:08:57 +02:00
end
2009-06-03 01:33:39 +02:00
2009-06-02 00:08:57 +02:00
def tag_list=(tags)
2009-06-03 02:15:23 +02:00
return if tags.split(" ").sort.join(" ") == self.tag_list
2009-06-03 21:30:09 +02:00
current_tags = ( self.tag_list.nil? ? [] : self.tag_list.split(" ") )
2009-06-03 01:33:39 +02:00
tags = tags.split(" ")
2009-06-02 00:08:57 +02:00
2009-06-03 01:33:39 +02:00
# 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|
2009-06-03 02:15:23 +02:00
photo.untag( tag )
2009-06-03 01:33:39 +02:00
}
}
# 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|
2009-06-03 02:15:23 +02:00
photo.tag( tag ) if !current_tags.include?( tag ) # no need to re-tag
2009-06-03 01:33:39 +02:00
}
2009-06-02 00:08:57 +02:00
end
2009-06-03 01:33:39 +02:00
end
protected
2009-05-22 22:38:52 +02:00
private
2009-06-02 00:08:57 +02:00
def create_folders
Dir.mkdir( APP_CONFIG[:photos_path] + self.path ) unless File.exists?( APP_CONFIG[:photos_path] + self.path )
Dir.mkdir( APP_CONFIG[:thumbs_path] + self.path ) unless File.exists?( APP_CONFIG[:thumbs_path] + self.path )
2009-06-02 00:08:57 +02:00
end
def destroy_folders
2009-05-22 22:38:52 +02:00
#puts "DELETE DIRECTORY " + APP_CONFIG[:photos_path] + self.path
2009-06-11 18:45:25 +02:00
Dir.delete( APP_CONFIG[:photos_path] + self.path ) if File.exists?( APP_CONFIG[:photos_path] + self.path )
Dir.delete( APP_CONFIG[:thumbs_path] + self.path ) if File.exists?( APP_CONFIG[:thumbs_path] + self.path )
2009-05-22 22:38:52 +02:00
end
2009-05-22 14:13:46 +02:00
end