base_photo_app/app/models/image_file.rb

88 lines
1.9 KiB
Ruby

# -*- encoding : utf-8 -*-
class ImageFile < ActiveRecord::Base
include PgSearch
mount_uploader :file, ImageUploader
#validates :icloud_id #, :uniqueness => true
belongs_to :album
has_many :image_file_share_images
has_many :share_images, :through => :image_file_share_images
attr_accessor :origin_n, :skip_tag_caches
before_save do
end
has_many :tag_taggables, :as => :taggable
has_many :tags, :through => :tag_taggables
pg_search_scope :search_by_text, :against => [:name, :description, :photograph, :origin_name, :tags_cache], :using => {
:tsearch => {:dictionary => 'french', :prefix => true, tsvector_column: "tsv_tsearch"},
:trigram => {
:threshold => 0.1,
}
},:ignoring => :accents
after_save do
if false
if self.changes[:album_id]
self.changes[:album_id].each do |album_id|
Album.find(album_id.to_i).update_image_count if album_id.to_i > 0
end
end
Album.find(self.album_id.to_i).update_image_count if self.album_id.to_i > 0
end
if !self.skip_tag_caches
self.tags_cache = self.tags.map{ |element| element.name}.join(" , ")
self.skip_tag_caches = true
self.save
end
end
def rotate(degrees=90)
versions = [self.file.path, self.file.large.path, self.file.large.thumb.path]
versions.each do |v|
image = Magick::ImageList.new(v)
image = image.rotate(degrees)
image.write(v)
end
self.file.recreate_versions!(:secure, :secure_thumb)
self.updated_at = Time.now
self.save
end
protected
def generate_token
if !self.token_s
begin
self.token_s = SecureRandom.hex(5)
end while ImageFile.exists?(:token_s => self.token_s)
end
end
end