2019-09-05 07:15:45 +00:00
|
|
|
class ArchiveSortOrder
|
|
|
|
include Mongoid::Document
|
2022-05-22 10:32:22 +00:00
|
|
|
field :sort_order, :type => Boolean # true => asc , false => desc
|
|
|
|
field :need_update_sort, :type => Boolean, :default => true
|
2022-05-22 16:19:51 +00:00
|
|
|
field :min_sort_number, :type => Integer, :default => 0
|
2022-05-22 10:32:22 +00:00
|
|
|
field :max_sort_number, :type => Integer, :default => 0
|
2020-02-11 15:38:10 +00:00
|
|
|
after_initialize do |record|
|
2022-05-22 10:32:22 +00:00
|
|
|
if(!record.new_record? && record.sort_order.nil?)
|
2020-02-11 15:38:10 +00:00
|
|
|
record.sort_order = true
|
|
|
|
record.save
|
|
|
|
elsif(!record.new_record?)
|
|
|
|
@module_app = ModuleApp.where(:key=>'archive').first
|
|
|
|
if !@module_app.nil?
|
|
|
|
@asc = @module_app.asc rescue nil
|
|
|
|
if !@asc.nil?
|
|
|
|
record.sort_order = @asc
|
|
|
|
record.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
after_save do |record|
|
2022-05-22 10:32:22 +00:00
|
|
|
unless record.instance_variable_get(:@skip_callback)
|
|
|
|
if record.sort_order_changed?
|
|
|
|
@module_app = ModuleApp.where(:key=>'archive').first
|
|
|
|
if !@module_app.nil?
|
|
|
|
@sort_order = record.sort_order rescue nil
|
|
|
|
if !@asc.nil?
|
|
|
|
if !@module_app.attributes[:asc].nil?
|
|
|
|
@module_app.asc = @sort_order
|
|
|
|
@module_app.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
record.instance_variable_set(:@skip_callback, true)
|
|
|
|
record.update(:need_update_sort=>true)
|
|
|
|
if record.need_update_sort
|
|
|
|
Thread.new do
|
|
|
|
sort_order = record.sort_order ? 1 : -1
|
|
|
|
ArchiveFile.recalc_sort_number(sort_order)
|
|
|
|
record.instance_variable_set(:@skip_callback, false)
|
|
|
|
end
|
|
|
|
end
|
2020-02-11 15:38:10 +00:00
|
|
|
end
|
|
|
|
end
|
2022-05-22 10:32:22 +00:00
|
|
|
end
|
|
|
|
def get_default_order
|
2022-05-22 16:19:51 +00:00
|
|
|
self.sort_order ? self.min_sort_number : self.max_sort_number
|
2020-02-11 15:38:10 +00:00
|
|
|
end
|
2019-09-05 07:15:45 +00:00
|
|
|
end
|