Class: ActiveadminDraggable::DraggableListItem

Inherits:
Object
  • Object
show all
Defined in:
lib/activeadmin_draggable/draggable_list_item.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dropped_item) ⇒ DraggableListItem

Returns a new instance of DraggableListItem.



22
23
24
# File 'lib/activeadmin_draggable/draggable_list_item.rb', line 22

def initialize(dropped_item)
  @dropped_item = dropped_item
end

Class Method Details

.move(klass, id, left_id) ⇒ Object

method that given any resource class changes possition of an item according to params passed by javascript sorting action



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/activeadmin_draggable/draggable_list_item.rb', line 7

def self.move(klass, id, left_id)
  left_id = left_id.to_i 
  dropped_item = klass.find(id)

  if left_id != 0
    item_to_insert_at = klass.find(left_id)
  else
    item_to_insert_at = nil 
  end 

  item = DraggableListItem.new(dropped_item)
  item.move_to(item_to_insert_at)

end

Instance Method Details

#move_to(item_to_insert_at) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/activeadmin_draggable/draggable_list_item.rb', line 26

def move_to(item_to_insert_at)
  if item_to_insert_at
    @item_to_insert_at = item_to_insert_at
    if moving_up?
      @dropped_item.insert_at(@item_to_insert_at.position)
      @dropped_item.increment_position
    else
      @dropped_item.insert_at(@item_to_insert_at.position)
    end
  else
    @dropped_item.move_to_top
  end
end