Module: Adminpanel::Sortable

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/adminpanel/sortable.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#move_to_position(new_position) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/concerns/adminpanel/sortable.rb', line 37

def move_to_position(new_position)
  if new_position < position
    # search for better elements and downgrade them
    self.class.in_better_position(
      self.position,
      new_position
    ).update_all('position = position + 1')
  else
    # search for worster elements and upgrade them
    self.class.in_worst_position(
      self.position,
      new_position
    ).update_all('position = position - 1')
  end
  self.update(position: new_position)
end