archive/app/models/archive_sort_order.rb

50 lines
1.4 KiB
Ruby
Raw Normal View History

class ArchiveSortOrder
include Mongoid::Document
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
field :max_sort_number, :type => Integer, :default => 0
after_initialize do |record|
if(!record.new_record? && record.sort_order.nil?)
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|
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
end
end
end
def get_default_order
2022-05-22 16:19:51 +00:00
self.sort_order ? self.min_sort_number : self.max_sort_number
end
end