2014-05-05 06:07:18 +00:00
|
|
|
class Album
|
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
|
|
|
|
|
|
|
include OrbitCategory::Categorizable
|
|
|
|
include OrbitTag::Taggable
|
2020-03-05 15:28:13 +00:00
|
|
|
include Slug
|
2014-05-05 06:07:18 +00:00
|
|
|
field :name, as: :slug_title, localize: true
|
|
|
|
field :description, localize: true
|
|
|
|
field :cover, default: "default"
|
|
|
|
field :cover_path #can refact
|
|
|
|
field :tag_names
|
|
|
|
field :uid, type: String
|
2014-06-13 08:15:13 +00:00
|
|
|
field :rss2_id, type: String
|
2016-06-28 08:40:23 +00:00
|
|
|
field :order, type: Integer, default: -1
|
2022-12-03 16:26:47 +00:00
|
|
|
field :min_order, type: Integer, default: -1
|
2021-04-04 05:03:24 +00:00
|
|
|
field :resize_gravity
|
2014-05-05 06:07:18 +00:00
|
|
|
# has_and_belongs_to_many :tags, :class_name => "GalleryTag"
|
|
|
|
has_many :album_images, :autosave => true, :dependent => :destroy
|
2019-09-25 06:53:46 +00:00
|
|
|
has_many :album_colors, :autosave => true, :dependent => :destroy
|
2014-05-05 06:07:18 +00:00
|
|
|
accepts_nested_attributes_for :album_images, :allow_destroy => true
|
2019-09-25 06:53:46 +00:00
|
|
|
accepts_nested_attributes_for :album_colors, :allow_destroy => true
|
2022-12-03 16:26:47 +00:00
|
|
|
index({"order" => 1}, { unique: false, background: false })
|
|
|
|
before_create do |record|
|
|
|
|
topest_order = AlbumSetting.pluck(:min_order).first.to_i
|
|
|
|
if topest_order.nil? || topest_order == 0
|
|
|
|
tmp_order = -2
|
|
|
|
else
|
|
|
|
tmp_order = topest_order - 1
|
|
|
|
end
|
|
|
|
record.order = tmp_order
|
|
|
|
AlbumSetting.update_all(:min_order=>tmp_order)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
after_destroy do
|
|
|
|
min_order = AlbumSetting.pluck(:min_order).first
|
|
|
|
if self.order == min_order
|
|
|
|
AlbumSetting.update_all(:min_order=>min_order + 1)
|
2020-10-31 02:17:24 +00:00
|
|
|
end
|
|
|
|
end
|
2021-04-04 05:03:24 +00:00
|
|
|
def resize_gravity
|
|
|
|
"""
|
|
|
|
NorthWestGravity
|
|
|
|
Position object at top-left of region
|
|
|
|
NorthGravity
|
|
|
|
Position object at top-center of region
|
|
|
|
NorthEastGravity
|
|
|
|
Position object at top-right of region
|
|
|
|
WestGravity
|
|
|
|
Position object at left-center of region
|
|
|
|
CenterGravity
|
|
|
|
Position object at center of region
|
|
|
|
EastGravity
|
|
|
|
Position object at right-center of region
|
|
|
|
SouthWestGravity
|
|
|
|
Position object at left-bottom of region
|
|
|
|
SouthGravity
|
|
|
|
Position object at bottom-center of region
|
|
|
|
SouthEastGravity
|
|
|
|
Position object at bottom-right of region
|
|
|
|
"""
|
|
|
|
tmp = self[:resize_gravity]
|
2021-05-24 09:44:52 +00:00
|
|
|
(tmp.blank? ? (AlbumSetting.first.resize_gravity.blank? ? "Center" : AlbumSetting.first.resize_gravity) : tmp) rescue 'Center'
|
2021-04-04 05:03:24 +00:00
|
|
|
end
|
2014-05-05 06:07:18 +00:00
|
|
|
def self.find_by_param(input)
|
2022-10-04 10:43:24 +00:00
|
|
|
self.find_by(uid: input.to_s)
|
2014-05-05 06:07:18 +00:00
|
|
|
end
|
2019-09-30 14:24:14 +00:00
|
|
|
def self.filter_by_tags(tags=[])
|
|
|
|
tags = OrbitHelper.page_tags if tags.blank?
|
|
|
|
tags = [tags].flatten.uniq
|
|
|
|
if !(tags.include?("all"))
|
|
|
|
tag_temp = Tag.all.select{|value| tags.include? value.id.to_s}
|
|
|
|
tag_temp_length = 0
|
|
|
|
tag_temp.each do |value|
|
|
|
|
if value.name==I18n.t('gallery.show_desc') || value.name==I18n.t('gallery.not_show_desc')
|
|
|
|
tag_temp_length+=1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if tag_temp_length!=0
|
|
|
|
if (tags.length - tag_temp_length) == 0
|
|
|
|
tags = ['all']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if tags.blank? || (tags.include?("all") rescue false)
|
|
|
|
self.all
|
|
|
|
else
|
|
|
|
tags
|
|
|
|
taggings = Tagging.where(:tag_id.in=>tags).map{|item| item.taggable_id}
|
|
|
|
self.where(:id.in=>taggings)
|
|
|
|
end
|
|
|
|
end
|
2020-03-05 15:28:13 +00:00
|
|
|
def cover
|
|
|
|
tmp = self['cover']
|
|
|
|
if tmp=='default'
|
|
|
|
tmp = (self.album_images.first.id.to_s rescue 'default')
|
|
|
|
end
|
|
|
|
tmp
|
|
|
|
end
|
|
|
|
def cover_path
|
|
|
|
tmp = self['cover_path']
|
|
|
|
if tmp.nil?
|
|
|
|
tmp = (self.album_images.first.file.thumb.url rescue nil)
|
|
|
|
end
|
|
|
|
tmp
|
|
|
|
end
|
2014-05-05 06:07:18 +00:00
|
|
|
end
|