lib/anoubis.rb
# Recalculate position of elements after set current position
Recalculate position of elements after set current position
Recalculate position of elements after set current position
def manual_order_after_save_element
        if manual_order_changed_orders
          position = self.manual_order_options[:field].to_sym  
          order_where = manual_order_get_where
          order_where[position] = self[position]
          count = 0
          self.class.where(order_where).where.not(:id => self.id).find_each do |element|
            element[position] = self[position]+1
            begin
              element.current_user = self.current_user
            rescue
              puts 'Cant set current_user'
            end
            element.save
            count += 1
          end
          if manual_order_changed_group
            order_where = manual_order_get_where_was
            pos = eval('self.'+position.to_s+'_was').to_i
            order_where[position] = [pos..Float::INFINITY]
            self.class.where(order_where).order(position).all.each  do |element|
              element[position] = pos
              begin
                element.current_user = self.current_user
              rescue
                puts 'Cant set current_user'
              end
              element.save
              pos += 1
            end
          end
        end
      end
def manual_order_after_save_element if manual_order_changed_orders position = self . manual_order_options [ :field ] . to_sym order_where = manual_order_get_where order_where [ position ] = self [ position ] count = 0 self . class . where ( order_where ) . where . not ( :id => self . id ) . find_each do | element | element [ position ] = self [ position ] + 1 begin element . current_user = self . current_user rescue puts 'Cant set current_user' end element . save count += 1 end if manual_order_changed_group order_where = manual_order_get_where_was pos = eval ( 'self.' + position . to_s + '_was' ) . to_i order_where [ position ] = [ pos .. Float :: INFINITY ] self . class . where ( order_where ) . order ( position ) . all . each do | element | element [ position ] = pos begin element . current_user = self . current_user rescue puts 'Cant set current_user' end element . save pos += 1 end end end end
Recalculate position of elements after set current position
Recalculate position of elements after set current position
#          puts self.id.to_s+'> manual_order_after_save_element' # set order field for simplicity #            puts self.id.to_s+'> Increment element position: '+element.id.to_s #if count == 0 #            order_where[position] = [self[position]+2..Float::INFINITY] #            data = self.class.where(order_where).where.not(:id => self.id).order(position).first #            if data #              data[position] = self[position]+1 #              data.save #            end #          end