2009-05-22 14:13:46 +02:00
|
|
|
class Photo < ActiveRecord::Base
|
2012-07-24 23:26:16 +02:00
|
|
|
extend Ext::GroupFor
|
2010-12-22 11:23:53 +01:00
|
|
|
|
2012-07-26 22:26:23 +02:00
|
|
|
ajaxful_rateable :stars => 5, :cache_column => :rating_average
|
|
|
|
|
2009-05-22 14:13:46 +02:00
|
|
|
belongs_to :album
|
2009-05-22 22:38:52 +02:00
|
|
|
has_many :photo_tags, :dependent => :destroy
|
|
|
|
has_many :tags, :through => :photo_tags
|
2009-05-22 21:04:41 +02:00
|
|
|
|
2012-07-24 23:26:16 +02:00
|
|
|
mount_uploader :attachment, FileUploader
|
|
|
|
|
2011-04-11 06:38:53 +02:00
|
|
|
before_create :exif_read
|
2012-07-24 23:26:16 +02:00
|
|
|
before_update :exif_write
|
2012-07-30 21:56:01 +02:00
|
|
|
after_create :set_title, :set_visible
|
2009-06-02 00:08:57 +02:00
|
|
|
|
2009-05-25 21:39:43 +02:00
|
|
|
attr_accessor :tag_list
|
2012-07-26 22:26:23 +02:00
|
|
|
|
|
|
|
scope :visible, where(:public => true)
|
2012-07-29 00:33:03 +02:00
|
|
|
scope :popular, visible.order('rating_average desc')
|
2010-09-17 17:57:33 +02:00
|
|
|
scope :untouched, :conditions => "photos.description IS NULL AND photos.id NOT IN ( SELECT photo_id FROM photo_tags)", :include => :album
|
|
|
|
scope :previous, lambda { |p,a| { :conditions => ["id < :id AND album_Id = :album ", { :id => p, :album => a } ], :limit => 1, :order => "id DESC"} }
|
|
|
|
scope :next, lambda { |p,a| { :conditions => ["id > :id AND album_Id = :album ", { :id => p, :album => a } ], :limit => 1, :order => "id ASC"} }
|
2009-06-11 13:05:09 +02:00
|
|
|
|
|
|
|
def to_param
|
2009-08-11 01:23:30 +02:00
|
|
|
"#{id}-#{title.parameterize}"
|
2009-06-12 01:04:57 +02:00
|
|
|
end
|
|
|
|
|
2009-06-03 02:15:23 +02:00
|
|
|
def tag(title)
|
|
|
|
return if self.tags.collect{|tag|tag.title}.include?( title )
|
|
|
|
self.photo_tags.create(:tag => Tag.find_or_create_by_title( :title => title) )
|
|
|
|
self.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
def untag(title)
|
|
|
|
return if !self.tags.collect{|tag|tag.title}.include?( title )
|
|
|
|
# perhaps not the best way but it finds the correct PhotoTag and deletes it
|
|
|
|
self.photo_tags.select{|photo_tag|
|
|
|
|
photo_tag.tag.title == title
|
|
|
|
}.each {|photo_tag|photo_tag.destroy}
|
|
|
|
self.reload
|
|
|
|
end
|
2009-06-02 00:08:57 +02:00
|
|
|
|
2009-05-25 21:39:43 +02:00
|
|
|
def tag_list
|
2012-07-24 23:26:16 +02:00
|
|
|
return self.tags.order('title').map{ |t| t.title }.sort.join(" ")
|
2009-05-25 21:39:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def tag_list=(tags)
|
|
|
|
ts = Array.new
|
|
|
|
tags.split(" ").each do |tag|
|
2009-06-03 21:30:09 +02:00
|
|
|
ts.push( Tag.find_or_create_by_title( :title => tag.downcase) )
|
2009-05-25 21:39:43 +02:00
|
|
|
end
|
|
|
|
self.tags = ts
|
|
|
|
end
|
2010-11-17 17:50:25 +01:00
|
|
|
|
|
|
|
def _delete
|
|
|
|
0
|
|
|
|
end
|
2009-05-25 21:39:43 +02:00
|
|
|
|
2009-06-02 00:08:57 +02:00
|
|
|
|
2009-06-12 01:04:57 +02:00
|
|
|
private
|
2009-06-03 01:33:39 +02:00
|
|
|
|
2012-07-30 21:56:01 +02:00
|
|
|
def set_visible
|
|
|
|
update_attribute(:public, true) unless self.public
|
|
|
|
end
|
|
|
|
|
2009-06-16 21:43:03 +02:00
|
|
|
def set_title
|
2012-07-30 21:56:01 +02:00
|
|
|
update_attribute(:title, self.attachment.file.basename.titleize) unless self.title
|
2009-06-16 21:43:03 +02:00
|
|
|
end
|
2011-04-11 11:17:08 +02:00
|
|
|
|
2009-06-03 01:33:39 +02:00
|
|
|
def exif_read
|
2012-07-24 23:26:16 +02:00
|
|
|
photo = MiniExiftool.new(self.attachment.file.file)
|
2009-06-03 01:33:39 +02:00
|
|
|
self.longitude = photo.GPSLongitude if self.longitude.nil?
|
|
|
|
self.latitude = photo.GPSLatitude if self.latitude.nil?
|
|
|
|
self.title = photo.DocumentName if self.title.nil?
|
2012-01-10 21:55:32 +01:00
|
|
|
self.description = photo.ImageDescription if self.description.nil? && photo.ImageDescription != 'Exif_JPEG_PICTURE'
|
2009-06-15 23:36:47 +02:00
|
|
|
self.tag_list = (self.tags.empty? ? "" : self.album.tag_list) + " " + (photo.Keywords.nil? ? "" : photo.Keywords.to_a.map { |tag| tag.gsub(" ", "_") }.join(" "))
|
2009-06-02 00:08:57 +02:00
|
|
|
end
|
|
|
|
|
2009-06-03 01:33:39 +02:00
|
|
|
def exif_write
|
2011-04-11 06:38:53 +02:00
|
|
|
# should only write if tags are changed as images can be large and thus ExifTool will take a while to write to the file
|
2012-07-24 23:26:16 +02:00
|
|
|
photo = MiniExiftool.new(self.attachment.file.file)
|
2009-06-02 00:08:57 +02:00
|
|
|
photo.GPSLongitude = self.longitude
|
|
|
|
photo.GPSLatitude = self.latitude
|
|
|
|
photo.DocumentName = self.title
|
|
|
|
photo.ImageDescription = self.description
|
2009-06-03 01:33:39 +02:00
|
|
|
photo.Keywords = self.tags
|
2009-06-02 00:08:57 +02:00
|
|
|
photo.save
|
|
|
|
end
|
2010-04-13 21:10:40 +02:00
|
|
|
|
2009-05-22 14:13:46 +02:00
|
|
|
end
|