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-06-02 00:08:57 +02:00
attr_protected :path
2009-08-03 16:23:51 +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) ) " , :order = > 'title'
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
2009-06-12 01:04:57 +02:00
self . path = self . title unless self . path
end
def set_title
2009-08-03 16:23:51 +02:00
self . title = File . basename ( 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-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
2009-06-12 01:04:57 +02:00
Dir . mkdir ( APP_CONFIG [ :photos_path ] + self . path ) unless File . exists? ( APP_CONFIG [ :photos_path ] + self . path )
2009-08-01 20:12:34 +02:00
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
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 )
2009-06-12 01:04:57 +02:00
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