Skip to content

Commit

Permalink
convert to jbuilder, restrict API routes to JSON only
Browse files Browse the repository at this point in the history
  • Loading branch information
starswan committed Jan 28, 2025
1 parent 1c5be14 commit 68da99e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
43 changes: 14 additions & 29 deletions app/controllers/publishers/vacancies/documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,26 @@ def destroy
end

def upload_file
document = params.require(:documents)

if vacancy.supporting_documents.attach(document)
upload_success document
else
upload_failure document
@document = params.require(:documents)

respond_to do |format|
if vacancy.supporting_documents.attach(@document)
send_dfe_analytics_event(:supporting_document_created, @document.original_filename, @document.size, @document.content_type)
format.json { render "upload_success" }
else
format.json { render "upload_error" }
end
end
end

def delete_uploaded_file
vacancy.supporting_documents.select { |d| d.filename == params.require(:delete) }.each do |document|
filename = params.require(:delete)
vacancy.supporting_documents.select { |d| d.filename == filename }.each do |document|
document.purge
send_dfe_analytics_event(:supporting_document_deleted, document.filename, document.byte_size, document.content_type)
end

render json: {
success: true,
}
respond_to(&:json)
end

def confirm
Expand All @@ -64,25 +66,8 @@ def confirm

private

def upload_success(document)
send_dfe_analytics_event(:supporting_document_created, document.original_filename, document.size, document.content_type)
render json: {
success: {
messageHtml: t("jobs.file_upload_success_message", filename: document.original_filename),
messageText: t("jobs.file_upload_success_message", filename: document.original_filename),
},
file: {
filename: document.original_filename,
originalname: document.original_filename,
},
}
end

def upload_failure(document)
render json: {
error: {
message: "#{document.original_filename} #{vacancy.errors[:supporting_documents].first}",
},
def file_json(document)
{
file: {
filename: document.original_filename,
originalname: document.original_filename,
Expand Down
4 changes: 4 additions & 0 deletions app/views/publishers/vacancies/documents/_file.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.file do
json.filename document.original_filename
json.orginalname document.original_filename
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.success true
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.error do
json.message "#{@document.original_filename} #{vacancy.errors[:supporting_documents].first}"
end
json.partial! "file", document: @document
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
json.success do
json.messageHtml t("jobs.file_upload_success_message", filename: @document.original_filename)
json.messageText t("jobs.file_upload_success_message", filename: @document.original_filename)
end
json.partial! "file", document: @document
5 changes: 3 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,9 @@
resources :build, only: %i[show update], controller: "publishers/vacancies/build"
resources :documents, only: %i[index new create destroy], controller: "publishers/vacancies/documents" do
post :confirm, on: :collection
post "/upload", on: :collection, to: "publishers/vacancies/documents#upload_file"
post "/remove", on: :collection, to: "publishers/vacancies/documents#delete_uploaded_file"
# API for MoJ multi-select component
post :upload, on: :collection, to: "publishers/vacancies/documents#upload_file", format: 'json'
post :remove, on: :collection, to: "publishers/vacancies/documents#delete_uploaded_file", format: 'json'
end
resource :application_forms, only: %i[create destroy], controller: "publishers/vacancies/application_forms"

Expand Down

0 comments on commit 68da99e

Please sign in to comment.