Class: Admin::CommitteeFilesController

Inherits:
ApplicationController
  • Object
show all
Includes:
Effective::CrudController
Defined in:
app/controllers/admin/committee_files_controller.rb

Instance Method Summary collapse

Instance Method Details

#bulk_moveObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/admin/committee_files_controller.rb', line 8

def bulk_move
  committee_folder = Effective::CommitteeFolder.find_by_id(params[:committee_folder_id])
  committee_files = Effective::CommitteeFile.sorted.where(id: params[:ids])
  original_folder = Effective::CommitteeFolder.find_by_id(committee_files.first.committee_folder_id)

  # Update the position of the files in the new folder
  position = (committee_folder.committee_files.map { |file| file.position }.compact.max || -1) + 1

  committee_files.each_with_index do |committee_file, index|
    committee_file.update!(committee_folder: committee_folder, position: position + index)
  end

  # Update the position of the files in the original folder
  original_folder.committee_files.each_with_index do |committee_file, index|
    committee_file.update!(position: index)
  end

  render json: { status: 200, message: "Successfully moved #{committee_files.length} files to #{committee_folder.title}" }
rescue => e
  render json: { status: 500, message: "Unable to move files: #{e.message}" }
end