119 lines
3.8 KiB
Ruby
119 lines
3.8 KiB
Ruby
class CustomAlbum
|
|
include Mongoid::Document
|
|
include Mongoid::Timestamps
|
|
|
|
include OrbitCategory::Categorizable
|
|
include OrbitTag::Taggable
|
|
include Slug
|
|
|
|
field :custom_module
|
|
field :bind_uid
|
|
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
|
|
field :rss2_id, type: String
|
|
field :order, type: Integer, default: -1
|
|
field :resize_gravity
|
|
# has_and_belongs_to_many :tags, :class_name => "CustomGalleryTag"
|
|
has_many :custom_album_images, :autosave => true, :dependent => :destroy
|
|
has_many :custom_album_colors, :autosave => true, :dependent => :destroy
|
|
accepts_nested_attributes_for :custom_album_images, :allow_destroy => true
|
|
accepts_nested_attributes_for :custom_album_colors, :allow_destroy => true
|
|
before_save do |record|
|
|
if record.order.nil? || record.order == -1
|
|
topest_order = CustomAlbum.all.pluck(:order).sort{|a,b| a.to_i<=>b.to_i}.first
|
|
if topest_order.nil? || topest_order == 0
|
|
record.order = -2
|
|
else
|
|
record.order = topest_order - 1
|
|
end
|
|
end
|
|
end
|
|
after_create do
|
|
custom_record_callback(1)
|
|
end
|
|
after_destroy do
|
|
custom_record_callback(-1)
|
|
end
|
|
def custom_record_callback(num)
|
|
if self.custom_module && self.bind_uid.present?
|
|
custom_album_config = CustomAlbumConfig.where(:module=>self.custom_module).first
|
|
if custom_album_config && custom_album_config.custom_record_callback.present? && custom_album_config.bind_model.present?
|
|
target_model = custom_album_config.bind_model.constantize rescue nil
|
|
if target_model
|
|
target_record = target_model.where(custom_album_config.uid_field=>self.bind_uid).first
|
|
target_record.send(custom_album_config.custom_record_callback,num) if target_record
|
|
end
|
|
end
|
|
end
|
|
end
|
|
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]
|
|
(tmp.blank? ? (CustomAlbumSetting.first.resize_gravity.blank? ? "Center" : CustomAlbumSetting.first.resize_gravity) : tmp) rescue 'Center'
|
|
end
|
|
def self.find_by_param(input)
|
|
self.find_by(uid: input)
|
|
end
|
|
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('custom_gallery.show_desc') || value.name==I18n.t('custom_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
|
|
def cover
|
|
tmp = self['cover']
|
|
if tmp=='default'
|
|
tmp = (self.custom_album_images.first.id.to_s rescue 'default')
|
|
end
|
|
tmp
|
|
end
|
|
def cover_path
|
|
tmp = self['cover_path']
|
|
if tmp.nil?
|
|
tmp = (self.custom_album_images.first.file.thumb.url rescue nil)
|
|
end
|
|
tmp
|
|
end
|
|
end |