Support file uploads
This allows submissions to store file uploads
This commit is contained in:
8
app/controllers/file_uploads_controller.rb
Normal file
8
app/controllers/file_uploads_controller.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
class FileUploadsController < ApplicationController
|
||||
def show
|
||||
@form = Form.find_by!(token: params[:form_id])
|
||||
@submission = @form.submissions.find(params[:submission_id])
|
||||
@file_upload = @submission.files_attachments.find(params[:id])
|
||||
redirect_to url_for(@file_upload)
|
||||
end
|
||||
end
|
||||
@@ -5,14 +5,18 @@ class SubmissionsController < ApplicationController
|
||||
|
||||
def create
|
||||
@form = Form.find_by!(token: params[:form_id])
|
||||
@submission = @form.submissions.build(data: data_params)
|
||||
# create a new submission object. we need a persisted submission to be able to process
|
||||
# potential the data - to be able to create URLs to uploads which is added as link to the table
|
||||
@submission = @form.submissions.create(remote_ip: request.remote_ip, referrer: request.referer)
|
||||
# processes the submitted data and saves the submission
|
||||
@submission.process_data(data_params)
|
||||
|
||||
respond_to do |format|
|
||||
if @submission.save
|
||||
format.html { redirect_to(@form.thank_you_url) if @form.thank_you_url.present? }
|
||||
format.json { render(json: { success: true, data: @submission.data }) }
|
||||
else
|
||||
format.html
|
||||
format.html { redirect_to(@form.thank_you_url) if @form.thank_you_url.present? }
|
||||
format.json { render(json: { error: @submission.errors }, status: 422) }
|
||||
end
|
||||
end
|
||||
|
||||
8
app/jobs/submission_append_job.rb
Normal file
8
app/jobs/submission_append_job.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
class SubmissionAppendJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
def perform(submission_id)
|
||||
submission = Submission.find(submission_id)
|
||||
submission.append_to_spreadsheet
|
||||
end
|
||||
end
|
||||
@@ -1,17 +1,20 @@
|
||||
class Submission < ApplicationRecord
|
||||
belongs_to :form
|
||||
after_create :append_to_spreadsheet
|
||||
validates_presence_of :data
|
||||
has_many :file_uploads, dependent: :destroy
|
||||
has_many_attached :files
|
||||
|
||||
def data=(value)
|
||||
sanitized_data = {}
|
||||
value.each do |key, value|
|
||||
sanitized_data[key] = submission_value(value)
|
||||
validates_presence_of :data, if: :appended_at?
|
||||
|
||||
def process_data(submitted_data)
|
||||
processed_data = {}
|
||||
submitted_data.each do |key, value|
|
||||
processed_data[key] = submission_value_for(value)
|
||||
end
|
||||
write_attribute(:data, sanitized_data)
|
||||
update_attribute(:data, processed_data)
|
||||
SubmissionAppendJob.perform_later(self.id)
|
||||
end
|
||||
|
||||
def submission_value(value)
|
||||
def submission_value_for(value)
|
||||
case value
|
||||
when Array
|
||||
value.join(', ')
|
||||
@@ -20,7 +23,16 @@ class Submission < ApplicationRecord
|
||||
when 'tinyforms_now'
|
||||
Time.now.utc.to_formatted_s(:rfc822)
|
||||
when ActionDispatch::Http::UploadedFile
|
||||
''
|
||||
# manually create the ActiveStorage attachment because we need the ID of the Attachment to create the URL
|
||||
# first the file needs to be uplaoded then we can create an Attachment
|
||||
# The CreateOne mainly handles the uplaod and the creation of the blob for us
|
||||
# `files` is the name from `has_many_attached :files`
|
||||
create_one = ActiveStorage::Attached::Changes::CreateOne.new('files', self, value)
|
||||
create_one.upload
|
||||
attachment = ActiveStorage::Attachment.new(record: self, name: 'files', blob: create_one.blob)
|
||||
attachment.save
|
||||
# return the URL that we use to show in the Spreadsheet
|
||||
Rails.application.routes.url_helpers.file_upload_url(form_id: form, submission_id: self, id: attachment.id, host: DEFAULT_HOST)
|
||||
else
|
||||
value.to_s
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user