allow to upload user initials

pull/289/head
Pete Matsyburka 1 year ago
parent f6c061a57b
commit e9a759055b

@ -0,0 +1,43 @@
# frozen_string_literal: true
class UserInitialsController < ApplicationController
before_action :load_user_config
authorize_resource :user_config
def edit; end
def update
file = params[:file]
return redirect_to settings_profile_index_path, notice: 'Unable to save initials' if file.blank?
blob = ActiveStorage::Blob.create_and_upload!(io: file.open,
filename: file.original_filename,
content_type: file.content_type)
attachment = ActiveStorage::Attachment.create!(
blob:,
name: 'initials',
record: current_user
)
if @user_config.update(value: attachment.uuid)
redirect_to settings_profile_index_path, notice: 'Initials has been saved'
else
redirect_to settings_profile_index_path, notice: 'Unable to save initials'
end
end
def destroy
@user_config.destroy
redirect_to settings_profile_index_path, notice: 'Initials has been removed'
end
private
def load_user_config
@user_config =
UserConfig.find_or_initialize_by(user: current_user, key: UserConfig::INITIALS_KEY)
end
end

@ -50,6 +50,7 @@ class User < ApplicationRecord
EMAIL_REGEXP = /[^@;,<>\s]+@[^@;,<>\s]+/
has_one_attached :signature
has_one_attached :initials
belongs_to :account
has_one :access_token, dependent: :destroy

@ -22,6 +22,7 @@
#
class UserConfig < ApplicationRecord
SIGNATURE_KEY = 'signature'
INITIALS_KEY = 'initials'
RECEIVE_COMPLETED_EMAIL = 'receive_completed_email'
belongs_to :user

@ -31,6 +31,15 @@
</div>
<% end %>
<a href="<%= edit_user_signature_path %>" data-turbo-frame="modal" class="base-button w-full">Update Signature</a>
<p class="text-2xl font-bold mt-8 mb-4">Initials</p>
<% initials = UserConfigs.load_initials(current_user) %>
<% if initials %>
<div class="flex justify-center mb-4 relative">
<%= button_to button_title(title: 'Remove', disabled_with: 'Removing'), user_initials_path, method: :delete, class: 'right-0 top-0 absolute link' %>
<img src="<%= initials.url %>" style="max-height: 200px; width: auto" width="<%= initials.metadata['width'] %>" height="<%= initials.metadata['height'] %>">
</div>
<% end %>
<a href="<%= edit_user_initials_path %>" data-turbo-frame="modal" class="base-button w-full">Update Initials</a>
<% if true_user == current_user && !current_account.testing? %>
<p class="text-2xl font-bold mt-8 mb-4">Change Password</p>
<%= form_for current_user, url: update_password_settings_profile_index_path, method: :patch, html: { autocomplete: 'off', class: 'space-y-4' } do |f| %>

@ -0,0 +1,55 @@
<%= render 'shared/turbo_modal', title: 'Update Initials' do %>
<% options = [%w[Draw draw], %w[Upload upload]] %>
<toggle-visible data-element-ids="<%= options.map(&:last).to_json %>" class="relative text-center mt-4 block">
<div class="join">
<% options.each_with_index do |(label, value), index| %>
<span>
<%= radio_button_tag 'option', value, value == 'draw', class: 'peer hidden', data: { action: 'change:toggle-visible#trigger' } %>
<label for="option_<%= value %>" class="<%= '!rounded-s-full' if index.zero? %> btn btn-focus btn-sm join-item md:w-28 peer-checked:btn-active normal-case">
<%= label %>
</label>
</span>
<% end %>
</div>
</toggle-visible>
<div id="draw" class="mt-3">
<%= form_for @user_config, url: user_initials_path, method: :put, data: { turbo_frame: :_top }, html: { autocomplete: :off, enctype: 'multipart/form-data' } do |f| %>
<signature-form class="relative block">
<a class="absolute top-1 right-1 link text-sm" data-target="signature-form.clear" href="#">Clear</a>
<canvas data-target="signature-form.canvas" class="bg-white border border-base-300 rounded"></canvas>
<input name="file" class="hidden" data-target="signature-form.input" type="file" accept="image/png,image/jpeg,image/jpg">
<div class="form-control mt-4">
<%= f.button button_title(title: 'Save', disabled_with: 'Saving'), class: 'base-button', data: { target: 'signature-form.button' } %>
</div>
</signature-form>
<% end %>
</div>
<div id="upload" class="hidden mt-3">
<%= form_for @user_config, url: user_initials_path, method: :put, data: { turbo_frame: :_top }, html: { autocomplete: :off, enctype: 'multipart/form-data' } do |f| %>
<file-dropzone data-submit-on-upload="true" class="w-full">
<label for="file" class="w-full block h-32 relative bg-base-200 hover:bg-base-200/70 rounded-md border border-base-content border-dashed">
<div class="absolute top-0 right-0 left-0 bottom-0 flex items-center justify-center">
<div class="flex flex-col items-center">
<span data-target="file-dropzone.icon">
<%= svg_icon('cloud_upload', class: 'w-10 h-10') %>
</span>
<span data-target="file-dropzone.loading" class="hidden">
<%= svg_icon('loader', class: 'w-10 h-10 animate-spin') %>
</span>
<div class="font-medium mb-1">
Upload Initials
</div>
<div class="text-xs">
<span class="font-medium">Click to upload</span> or drag and drop
</div>
</div>
<input id="file" name="file" class="hidden" data-action="change:file-dropzone#onSelectFiles" data-target="file-dropzone.input" type="file" accept="image/png,image/jpeg,image/jpg">
</div>
</label>
</file-dropzone>
<div class="form-control mt-4">
<%= f.button button_title(title: 'Save', disabled_with: 'Saving'), class: 'base-button' %>
</div>
<% end %>
</div>
<% end %>

@ -50,6 +50,7 @@ Rails.application.routes.draw do
resources :enquiries, only: %i[create]
resources :users, only: %i[new create edit update destroy]
resource :user_signature, only: %i[edit update destroy]
resource :user_initials, only: %i[edit update destroy]
resources :submissions_archived, only: %i[index], path: 'submissions/archived'
resources :submissions, only: %i[index], controller: 'submissions_dashboard'
resources :submissions, only: %i[show destroy]

@ -36,6 +36,14 @@ module Submitters
user.first_name
elsif field_name == 'last name'
user.last_name
elsif field['type'] == 'initials' && (initials = UserConfigs.load_initials(user))
attachment = ActiveStorage::Attachment.find_or_create_by!(
blob_id: initials.blob_id,
name: 'attachments',
record: submitter
)
attachment.uuid
elsif field['type'] == 'signature' && (signature = UserConfigs.load_signature(user))
attachment = ActiveStorage::Attachment.find_or_create_by!(
blob_id: signature.blob_id,

@ -10,4 +10,12 @@ module UserConfigs
ActiveStorage::Attachment.find_by(uuid:, record: user, name: 'signature') if uuid.present?
end
def load_initials(user)
return if user.blank?
uuid = user.user_configs.find_or_initialize_by(key: UserConfig::INITIALS_KEY).value
ActiveStorage::Attachment.find_by(uuid:, record: user, name: 'initials') if uuid.present?
end
end

Loading…
Cancel
Save