2009-05-22 14:13:46 +02:00
class Album < ActiveRecord :: Base
2012-07-24 23:26:16 +02:00
extend Ext :: GroupFor
2012-07-26 22:26:23 +02:00
ajaxful_rateable :stars = > 5 , :cache_column = > :rating_average
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
2010-12-20 22:18:27 +01:00
validates :path , :presence = > true , :uniqueness = > true #, :message => "Album already exsists on disc"
validates :title , :presence = > true #, :message => "can't be blank"
2012-07-29 00:33:03 +02:00
2009-06-12 01:04:57 +02:00
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
2012-07-24 23:26:16 +02:00
2012-07-29 00:33:03 +02:00
scope :visible , lambda { where ( :public = > true ) }
scope :popular , lambda { visible . includes ( :photos ) . where ( " photos.id NOT NULL " ) . order ( 'albums.rating_average desc' ) }
scope :untouched , lambda { where ( " 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 , lambda { where ( " albums.id NOT IN (SELECT album_id FROM collection_albums) " ) }
scope :used , lambda { where ( " 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
2012-07-29 00:33:03 +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
2012-07-29 00:33:03 +02:00
2009-06-12 01:04:57 +02:00
def set_title
2010-12-13 05:17:49 +01:00
self . title = File . basename ( self . path ) . titleize unless self . title || ! self . path
2009-06-02 00:08:57 +02:00
end
2012-07-29 00:33:03 +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?
2012-07-29 00:33:03 +02:00
2009-06-02 00:08:57 +02:00
tags = Array . new
2012-07-29 00:33:03 +02:00
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
tags = tags & photo_tags
end
2009-06-03 01:33:39 +02:00
}
2009-06-16 21:43:03 +02:00
return tags
2009-06-02 00:08:57 +02:00
end
2012-07-29 00:33:03 +02:00
2009-06-16 21:43:03 +02:00
def tags = ( tags )
tags = tags . split ( " " ) . sort
2012-07-29 00:33:03 +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
2012-07-29 00:33:03 +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 |
2012-07-29 00:33:03 +02:00
self . photos . each { | photo |
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 |
2012-07-29 00:33:03 +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
2012-07-29 00:33:03 +02:00
self . photos . each { | photo |
2009-06-30 21:24:06 +02:00
tags = tags | photo . tags
}
return tags
2012-07-29 00:33:03 +02:00
end
2009-06-30 21:24:06 +02:00
2009-06-03 01:33:39 +02:00
protected
2009-05-22 22:38:52 +02:00
private
2012-07-29 00:33:03 +02:00
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
2012-07-29 00:33:03 +02:00
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