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 "
2009-06-12 01:04:57 +02:00
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
2009-06-04 22:28:28 +02:00
after_destroy :destroy_folders
2009-06-02 00:08:57 +02:00
2009-06-16 21:43:03 +02:00
attr_accessor :tags
2009-08-11 01:23:30 +02:00
#attr_protected :path
2009-06-02 00:08:57 +02:00
2010-09-17 17:57:33 +02:00
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) ) " , :order = > 'title'
scope :unused , :conditions = > " albums.id NOT IN (SELECT album_id FROM collection_albums) "
scope :used , :conditions = > " albums.id IN (SELECT album_id FROM collection_albums) "
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 } "
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
2009-08-10 14:56:56 +02:00
self . path = self . title . parameterize unless self . path
2009-06-12 01:04:57 +02:00
end
def set_title
2009-08-11 12:12:00 +02:00
self . title = File . basename ( self . path ) . titleize unless self . title || ! self . path
2009-06-02 00:08:57 +02:00
end
2009-05-22 21:04:41 +02:00
2009-06-16 21:43:03 +02:00
def tags
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
2009-06-30 21:24:06 +02:00
#tags = tags & photo_tags
2009-06-03 01:33:39 +02:00
tags = tags & photo_tags
end
}
2009-06-16 21:43:03 +02:00
return tags
2009-06-02 00:08:57 +02:00
end
2009-06-03 01:33:39 +02:00
2009-06-16 21:43:03 +02:00
def tags = ( tags )
tags = tags . split ( " " ) . sort
2009-06-30 21:24:06 +02:00
current_tags = ( self . tags . nil? ? [ ] : self . tags . map { | tag | tag . title } )
2009-06-18 15:38:37 +02:00
return if tags == self . tags
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 |
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
2009-06-30 21:24:06 +02:00
( tags - current_tags ) . each do | tag |
2009-06-03 01:33:39 +02:00
self . photos . each { | photo |
2009-06-30 21:24:06 +02:00
photo . tag ( 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
2009-06-30 21:24:06 +02:00
def photo_tags
tags = Array . new
self . photos . each { | photo |
tags = tags | photo . tags
}
return tags
end
2009-06-03 01:33:39 +02:00
protected
2009-05-22 22:38:52 +02:00
private
2009-06-02 00:08:57 +02:00
def create_folders
2010-04-14 05:56:13 +02:00
#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
2009-06-04 22:28:28 +02:00
def destroy_folders
2009-05-22 22:38:52 +02:00
#puts "DELETE DIRECTORY " + APP_CONFIG[:photos_path] + self.path
2010-04-14 05:56:13 +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