box_organizer_controller.rb
1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class BoxOrganizerController < ApplicationController
def move_block
@block = boxes_holder.blocks.find(params[:id].gsub(/^block-/, ''))
@source_box = @block.box
target_position = nil
if (params[:target] =~ /before-block-([0-9]+)/)
block_before = boxes_holder.blocks.find($1)
target_position = block_before.position
@target_box = block_before.box
else
(params[:target] =~ /end-of-box-([0-9])+/)
@target_box = boxes_holder.boxes.find($1)
end
if (@source_box != @target_box)
@block.remove_from_list
@block.box = @target_box
end
if target_position.nil?
# insert in the end of the box
@block.insert_at(@target_box.blocks.size + 1)
@block.move_to_bottom
else
# insert the block in the given position
@block.insert_at(@block.position && @block.position < target_position ? target_position - 1 : target_position)
end
@block.save!
@target_box.reload
end
def move_block_down
@block = boxes_holder.blocks.find(params[:id])
@block.move_lower
redirect_to :back
end
def move_block_up
@block = boxes_holder.blocks.find(params[:id])
@block.move_higher
redirect_to :back
end
end