Class: Admin::DocumentDistributionsController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/admin/document_distributions_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /admin/document_distributions or /admin/document_distributions.json

Creates a new document distribution. If successful, redirects to the document distributions list with a success notice. Otherwise, renders the new form with errors.



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/admin/document_distributions_controller.rb', line 50

def create
  @document_distribution = DocumentDistribution.new(document_distribution_params)

  respond_to do |format|
    if @document_distribution.save
      format.html { redirect_to admin_document_document_distributions_path(@document), notice: "Document distribution was successfully created." }
      format.json { render :show, status: :created, location: @document_distribution }
    else
      format.html { render :new, status: :unprocessable_entity }
      format.json { render json: @document_distribution.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /admin/document_distributions/1 or /admin/document_distributions/1.json

Deletes a document distribution. Redirects to the document distributions list with a success notice.



83
84
85
86
87
88
89
90
# File 'app/controllers/admin/document_distributions_controller.rb', line 83

def destroy
  @document_distribution.destroy!

  respond_to do |format|
    format.html { redirect_to admin_document_document_distributions_path(@document), status: :see_other, notice: "Document distribution was successfully destroyed." }
    format.json { head :no_content }
  end
end

#destroy_allObject

DELETE /admin/document_distributions/destroy_all

Queues a background job to destroy all document distributions provided in the file parameter. The job will process the CSV file and send a notification when complete.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/admin/document_distributions_controller.rb', line 96

def destroy_all
  return if request.get?

  logger.debug("Queue Destroy Distributions Job")
  unless params.dig(:document_distribution, :distributions, :file)
    raise ArgumentError, "File does not exist or is invalid."
  end

  # Save the uploaded file to a temporary location
  uploaded_file = params.dig(:document_distribution, :distributions, :file)
  temp_file_path = Rails.root.join("tmp", "destroy_distributions_#{Time.current.to_i}_#{SecureRandom.hex(8)}.csv")

  File.binwrite(temp_file_path, uploaded_file.read)

  # Queue the background job
  DestroyDocumentDistributionsJob.perform_later(temp_file_path.to_s, current_user)

  respond_to do |format|
    format.html {
      redirect_to admin_document_distributions_path,
        notice: "Distribution destruction job has been queued. You will receive a notification when it completes."
    }
  end
rescue => e
  respond_to do |format|
    format.html {
      redirect_to admin_document_distributions_path,
        notice: "Failed to queue distribution destruction job: #{e.message}"
    }
  end
end

#editObject

GET /admin/document_distributions/1/edit

Edits an existing document distribution.



43
44
# File 'app/controllers/admin/document_distributions_controller.rb', line 43

def edit
end

#indexObject

GET /admin/document_distributions or /admin/document_distributions.json

Lists all document distributions. If a document_id is provided, it lists distributions associated with that document, ordered by position. Otherwise, it paginates all document distributions.



18
19
20
21
22
23
24
25
# File 'app/controllers/admin/document_distributions_controller.rb', line 18

def index
  @document_distributions = DocumentDistribution.all
  if params[:document_id]
    @document_distributions = DocumentDistribution.where(friendlier_id: @document.friendlier_id).order(position: :asc)
  else
    @pagy, @document_distributions = pagy(DocumentDistribution.all.order(friendlier_id: :asc, updated_at: :desc), items: 20)
  end
end

#newObject

GET /admin/document_references/new

Initializes a new document distribution.



36
37
38
# File 'app/controllers/admin/document_distributions_controller.rb', line 36

def new
  @document_distribution = DocumentDistribution.new
end

#showObject

GET /admin/document_distributions/1 or /admin/document_distributions/1.json

Shows a specific document distribution.



30
31
# File 'app/controllers/admin/document_distributions_controller.rb', line 30

def show
end

#updateObject

PATCH/PUT /admin/document_distributions/1 or /admin/document_distributions/1.json

Updates an existing document distribution. If successful, redirects to the document distribution with a success notice. Otherwise, renders the edit form with errors.



68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/admin/document_distributions_controller.rb', line 68

def update
  respond_to do |format|
    if @document_distribution.update(document_distribution_params)
      format.html { redirect_to admin_document_document_distributions_path(@document), notice: "Document distribution was successfully updated." }
      format.json { render :show, status: :ok, location: @document_distribution }
    else
      format.html { render :edit, status: :unprocessable_entity }
      format.json { render json: @document_distribution.errors, status: :unprocessable_entity }
    end
  end
end