generated from dxw/rails-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new files for upload documents
- Loading branch information
1 parent
b36039d
commit 112a61b
Showing
20 changed files
with
369 additions
and
3 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
app/controllers/support/cases/document_uploads_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module Support | ||
class Cases::DocumentUploadsController < Cases::ApplicationController | ||
before_action :set_current_case | ||
before_action { @back_url = support_case_path(@current_case, anchor: "tasklist") } | ||
before_action { @uploaded_files = current_case.upload_documents } | ||
def edit | ||
@document_uploader = current_case.document_uploader | ||
@uploaded_files = current_case.upload_documents | ||
end | ||
|
||
def create | ||
@document_uploader = current_case.document_uploader(document_uploader_params) | ||
if @document_uploader.valid? | ||
@document_uploader.save! | ||
@current_case.update!(case_document_uploader_params) | ||
redirect_to @back_url | ||
else | ||
render :edit | ||
end | ||
end | ||
|
||
def destroy | ||
@uploaded_document = Support::CaseUploadDocument.find(params[:document_id]) | ||
@support_document = Support::Document.find(@uploaded_document.attachable_id) | ||
@back_url = edit_support_case_document_uploads_path | ||
return unless params[:confirm] | ||
|
||
@uploaded_document.destroy! | ||
@support_document.destroy! | ||
|
||
if @uploaded_files.empty? | ||
reset_uploaded_documents | ||
end | ||
redirect_to edit_support_case_document_uploads_path, | ||
notice: I18n.t("support.cases.upload_documents.flash.destroyed", name: @uploaded_document.file_name) | ||
end | ||
|
||
private | ||
|
||
def set_current_case | ||
@current_case = Support::Case.find(params[:case_id]) | ||
end | ||
|
||
def document_uploader_params | ||
params.fetch(:document_uploader, {}).permit(:has_uploaded_documents, files: []) | ||
end | ||
|
||
def case_document_uploader_params | ||
params.require(:document_uploader).permit(:has_uploaded_documents) | ||
end | ||
|
||
def reset_uploaded_documents | ||
@current_case.update!(has_uploaded_documents: nil) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Support::Case::DocumentUploadable | ||
extend ActiveSupport::Concern | ||
|
||
def document_uploader(params = {}) | ||
Support::Case::DocumentUploader.new(support_case: self, **params) | ||
end | ||
|
||
def upload_document_files(files:) | ||
return if files.blank? | ||
|
||
files.each do |file| | ||
upload_documents.create!( | ||
attachable: Support::Document.create!(case: self, file_type: file.content_type, file:), | ||
file_type: file.content_type, | ||
file_name: file.original_filename, | ||
file_size: file.size, | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class Support::Case::DocumentUploader | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
include ActiveModel::Validations | ||
|
||
attribute :files | ||
attribute :upload_document_files | ||
attribute :support_case | ||
attribute :has_uploaded_documents | ||
|
||
validates :files, presence: true, if: -> { support_case.has_uploaded_documents.nil? } | ||
validate :files_safe, if: -> { files.present? } | ||
validates :has_uploaded_documents, presence: true | ||
|
||
def save! | ||
support_case.upload_document_files(files:) | ||
end | ||
|
||
private | ||
|
||
def files_safe | ||
results = files.map { |file| Support::VirusScanner.uploaded_file_safe?(file) } | ||
errors.add(:files, I18n.t("support.case.label.case_files.errors.unsafe")) unless results.all? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
module Support | ||
class CaseUploadDocument < ApplicationRecord | ||
belongs_to :case, class_name: "Support::Case", foreign_key: :support_case_id | ||
belongs_to :attachable, polymorphic: true, optional: true | ||
|
||
delegate :file, :file_type, to: :attachable | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<%= form_with model: @document_uploader, scope: :document_uploader, url: support_case_document_uploads_path, | ||
html: { "data-controller" => "case-files" } do |form| %> | ||
|
||
<h2 class="govuk-heading-m"><%= I18n.t("support.cases.upload_documents.choose_file_title") %></h2> | ||
|
||
<%= form.govuk_error_summary %> | ||
|
||
<span class="govuk-button govuk-button--secondary" role="button" | ||
data-case-files-target="btnDisplayFileDialog"> | ||
<%= I18n.t("support.cases.upload_documents.button.choose_file") %> | ||
</span> | ||
|
||
<%= render "uploaded_documents" %> | ||
|
||
<div class="govuk-form-group govuk-!-margin-top-7"> | ||
<fieldset class="govuk-fieldset"> | ||
<legend class="govuk-fieldset__legend govuk-fieldset__legend--s"><%= I18n.t("support.cases.upload_documents.file_upload_confirmation") %></legend> | ||
<div class="govuk-radios"> | ||
<div class="govuk-radios__item"> | ||
<%= form.radio_button :has_uploaded_documents, true, checked: @current_case.has_uploaded_documents == true, class: "govuk-radios__input" %> | ||
<%= form.label :has_uploaded_documents, "Yes, I have uploaded all documents", value: true, class: "govuk-label govuk-radios__label" %> | ||
</div> | ||
<div class="govuk-radios__item"> | ||
<%= form.radio_button :has_uploaded_documents, false, checked: @current_case.has_uploaded_documents == false, class: "govuk-radios__input" %> | ||
<%= form.label :has_uploaded_documents, "No", value: false, class: "govuk-label govuk-radios__label" %> | ||
</div> | ||
</div> | ||
</fieldset> | ||
</div> | ||
|
||
<div class="govuk-button-group flex-align-center"> | ||
<%= form.submit I18n.t("generic.button.next"), class: "govuk-button", role: "button", "data-action" => "case-files#submit", data: { disable_with: "Uploading..." } %> | ||
<%= link_to I18n.t("generic.button.cancel"), @back_url, class: "govuk-link govuk-link--no-visited-state" %> | ||
</div> | ||
|
||
<%= form.govuk_file_field :files, multiple: true, include_hidden: false, class: "govuk-!-display-none", "data-case-files-target" => "filesField", form_group: { class: "govuk-!-display-none" } %> | ||
|
||
<% end %> |
43 changes: 43 additions & 0 deletions
43
app/views/support/cases/document_uploads/_uploaded_documents.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds"> | ||
<h3 class="govuk-heading-s govuk-!-margin-bottom-2"><%= I18n.t("support.cases.upload_documents.file_uploaded") %></h3> | ||
</div> | ||
</div> | ||
|
||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds"> | ||
<div class="govuk-!-display-none" data-case-files-target="fileList"> | ||
<div data-case-files-target="filePreview"> | ||
<div data-case-files-target="previewTemplate"> | ||
<div class="case-files__file"> | ||
<span class="case-files__file-name" data-dz-name></span> | ||
<div class="case-files__file-actions"> | ||
<span class="case-files__file-remove"> | ||
<a class="govuk-link govuk-link--no-visited-state" href="#" data-dz-remove><%= I18n.t("generic.button.delete") %></a> | ||
</span> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds"> | ||
<% @uploaded_files.each do |uploaded_file| %> | ||
<div class="case-files__file"> | ||
<span class="case-files__file-name"><%= link_to uploaded_file.file_name, support_document_download_path(uploaded_file, type: uploaded_file.class), class: "govuk-link", target: "_blank" %></span> | ||
<div class="case-files__file-actions"> | ||
<span class="case-files__file-remove"> | ||
<%= link_to I18n.t("generic.button.delete"), | ||
support_case_document_uploads_path(case_id: @current_case, document_id: uploaded_file), | ||
method: :delete, | ||
class: "govuk-link govuk-link--no-visited-state" %> | ||
</span> | ||
</div> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<%= render partial: "support/cases/components/case_header", locals: { current_case: @current_case } %> | ||
|
||
<h1 class="govuk-heading-l"> | ||
<%= I18n.t("support.cases.upload_documents.delete_confirmation", name:@uploaded_document.file_name) %> | ||
</h1> | ||
|
||
<div class="govuk-button-group flex-align-center"> | ||
<%= link_to I18n.t("generic.button.delete"), | ||
support_case_document_uploads_path(@uploaded_document, case_id: @current_case, confirm: true, document_id: @uploaded_document.id), | ||
method: :delete, | ||
class: "govuk-button govuk-button--warning" %> | ||
|
||
<%= link_to I18n.t("generic.button.cancel"), | ||
edit_support_case_document_uploads_path(case_id: @current_case), | ||
class: "govuk-link govuk-link--no-visited-state" %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%= render partial: "support/cases/components/case_header", locals: { current_case: @current_case } %> | ||
<h1 class="govuk-heading-l"><%= I18n.t("support.cases.upload_documents.header") %></h1> | ||
<p class="govuk-body-m govuk-!-static-margin-bottom-8"><%= I18n.t("support.cases.upload_documents.hint") %></p> | ||
|
||
<%= render "form" %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%= render "framework_requests/form", | ||
route: "document_uploads", | ||
verb: :post, | ||
path: document_uploads_framework_requests_path, | ||
hide_submit_button: true %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
db/migrate/20241203125734_add_support_case_upload_documents.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class AddSupportCaseUploadDocuments < ActiveRecord::Migration[7.2] | ||
def change | ||
create_table "support_case_upload_documents", id: :uuid do |t| | ||
t.references "support_case", type: :uuid | ||
t.string "file_type" | ||
t.string "file_name" | ||
t.bigint "file_size" | ||
t.uuid "attachable_id" | ||
t.string "attachable_type" | ||
t.timestamps | ||
end | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
db/migrate/20241209104836_add_has_uploaded_documents_to_support_cases.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddHasUploadedDocumentsToSupportCases < ActiveRecord::Migration[7.2] | ||
def change | ||
add_column :support_cases, :has_uploaded_documents, :boolean | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.