class ArchiveSortOrder include Mongoid::Document field :sort_order, :type => Boolean # true => asc , false => desc field :need_update_sort, :type => Boolean, :default => true 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 self.sort_order ? self.min_sort_number : self.max_sort_number end end