2015-10-23 06:50:07 +00:00
|
|
|
require 'net/http'
|
2019-10-26 07:36:04 +00:00
|
|
|
class AlbumImage
|
2014-05-05 06:07:18 +00:00
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
|
|
|
include OrbitTag::Taggable
|
2022-12-03 16:26:47 +00:00
|
|
|
mount_uploader :file, GalleryUploader
|
|
|
|
field :title
|
2021-04-04 05:03:24 +00:00
|
|
|
field :title_translations, type: Hash, default: {}
|
2022-12-03 16:26:47 +00:00
|
|
|
field :description, type: String, localize: true, default: ""
|
|
|
|
field :rss2_id, type: String
|
|
|
|
field :order, type: Integer, default: -1
|
|
|
|
belongs_to :album
|
|
|
|
has_many :album_crops, :autosave => true, :dependent => :destroy
|
|
|
|
accepts_nested_attributes_for :album_crops, :allow_destroy => true
|
|
|
|
# has_and_belongs_to_many :tags, :class_name => "GalleryTag"
|
|
|
|
index({"order" => 1}, { unique: false, background: false })
|
|
|
|
before_create do
|
|
|
|
tmp_album = self.album
|
|
|
|
min_order = nil
|
|
|
|
if tmp_album
|
|
|
|
min_order = tmp_album.min_order
|
|
|
|
min_order = -1 if min_order == 0
|
|
|
|
end
|
|
|
|
if min_order.nil?
|
|
|
|
self.order = -Time.now.to_i
|
|
|
|
else
|
|
|
|
tmp_order = min_order - 1
|
|
|
|
self.order = tmp_order
|
|
|
|
Album.where(:id=>tmp_album.id).update_all(:min_order=>tmp_order) #present call save callback twice due to autosave
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
after_destroy do
|
|
|
|
min_order = Album.where(:id=>self.album_id).pluck(:min_order).first
|
|
|
|
puts "min_order: #{min_order} #{self.order}"
|
|
|
|
if self.order == min_order
|
|
|
|
Album.where(:id=>self.album_id).update_all(:min_order=>min_order + 1)
|
|
|
|
end
|
|
|
|
end
|
2023-05-04 13:06:02 +00:00
|
|
|
before_save do
|
|
|
|
if self.file_changed?
|
|
|
|
cover = self.album['cover']
|
|
|
|
if cover && cover == self.id
|
|
|
|
self.album.update_attributes({:cover_path => self.file.thumb.url, :cover=> self.id})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-04-04 05:03:24 +00:00
|
|
|
def title_translations
|
|
|
|
tmp = super || {}
|
|
|
|
if tmp == {}
|
|
|
|
tmp = I18n.available_locales.collect{|locale| [locale,self[:title]]}.to_h
|
|
|
|
end
|
|
|
|
tmp
|
|
|
|
end
|
|
|
|
def title
|
|
|
|
self.title_translations[I18n.locale]
|
|
|
|
end
|
2014-05-05 06:07:18 +00:00
|
|
|
end
|