Skip to content

Instantly share code, notes, and snippets.

@frankyston
Forked from lazaronixon/medical_record.rb
Created June 22, 2024 17:01
Show Gist options
  • Save frankyston/6899412732e99b07487075d5dfc3338d to your computer and use it in GitHub Desktop.
Save frankyston/6899412732e99b07487075d5dfc3338d to your computer and use it in GitHub Desktop.
Api upload
class MedicalRecord < ApplicationRecord
enum kind: %i[ imaging results notes ]
belongs_to :patient, class_name: "User", inverse_of: :medical_records
has_one_attached :document, dependent: :detach
scope :with_kind, -> (kind) { where kind: kind }
after_create_commit :deliver_updation
private
def deliver_updation
Deliveries::MedicalRecordUpdationDelivery.deliver(self, patient)
end
end
class Users::MedicalRecordsController < ApplicationProfessionalController
include PatientScoped
rescue_from ActiveSupport::MessageVerifier::InvalidSignature, with: :not_found
def create
if has_kind? && has_document?
@medical_record = @patient.medical_records.create!(medical_record_params); render("medical_records/show", status: :created)
else
head :bad_request
end
end
private
def medical_record_params
params.permit :kind, :document
end
def has_kind?
MedicalRecord.kinds.keys.include?(params[:kind])
end
def has_document?
params[:document].present?
end
end
class UploadsController < ApplicationController
def create
if params[:name]
@blob = upload(upload_params); render(json: response_attributes, status: :created)
else
head :bad_request
end
end
private
def upload(attributes)
ActiveStorage::Blob.create_and_upload!(**attributes)
end
def upload_params
{ io: request.body, filename: params[:name] }
end
def response_attributes
{ attachable_sgid: @blob.signed_id }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment