diff --git a/app/controllers/user_initials_controller.rb b/app/controllers/user_initials_controller.rb new file mode 100644 index 00000000..b8ba23e5 --- /dev/null +++ b/app/controllers/user_initials_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index ff066cd9..1948bfe0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/models/user_config.rb b/app/models/user_config.rb index 789ebc98..c3ead052 100644 --- a/app/models/user_config.rb +++ b/app/models/user_config.rb @@ -22,6 +22,7 @@ # class UserConfig < ApplicationRecord SIGNATURE_KEY = 'signature' + INITIALS_KEY = 'initials' RECEIVE_COMPLETED_EMAIL = 'receive_completed_email' belongs_to :user diff --git a/app/views/profile/index.html.erb b/app/views/profile/index.html.erb index 4cb89d76..76e7c710 100644 --- a/app/views/profile/index.html.erb +++ b/app/views/profile/index.html.erb @@ -31,6 +31,15 @@ <% end %> Update Signature +

Initials

+ <% initials = UserConfigs.load_initials(current_user) %> + <% if initials %> +
+ <%= button_to button_title(title: 'Remove', disabled_with: 'Removing'), user_initials_path, method: :delete, class: 'right-0 top-0 absolute link' %> + +
+ <% end %> + Update Initials <% if true_user == current_user && !current_account.testing? %>

Change Password

<%= form_for current_user, url: update_password_settings_profile_index_path, method: :patch, html: { autocomplete: 'off', class: 'space-y-4' } do |f| %> diff --git a/app/views/user_initials/edit.html.erb b/app/views/user_initials/edit.html.erb new file mode 100644 index 00000000..08a67d31 --- /dev/null +++ b/app/views/user_initials/edit.html.erb @@ -0,0 +1,55 @@ +<%= render 'shared/turbo_modal', title: 'Update Initials' do %> + <% options = [%w[Draw draw], %w[Upload upload]] %> + +
+ <% options.each_with_index do |(label, value), index| %> + + <%= radio_button_tag 'option', value, value == 'draw', class: 'peer hidden', data: { action: 'change:toggle-visible#trigger' } %> + + + <% end %> +
+
+
+ <%= form_for @user_config, url: user_initials_path, method: :put, data: { turbo_frame: :_top }, html: { autocomplete: :off, enctype: 'multipart/form-data' } do |f| %> + + Clear + + +
+ <%= f.button button_title(title: 'Save', disabled_with: 'Saving'), class: 'base-button', data: { target: 'signature-form.button' } %> +
+
+ <% end %> +
+ +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 00305fdf..171868be 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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] diff --git a/lib/submitters/maybe_update_default_values.rb b/lib/submitters/maybe_update_default_values.rb index 2a146425..c9d62ebe 100644 --- a/lib/submitters/maybe_update_default_values.rb +++ b/lib/submitters/maybe_update_default_values.rb @@ -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, diff --git a/lib/user_configs.rb b/lib/user_configs.rb index 574b88d6..7dc61711 100644 --- a/lib/user_configs.rb +++ b/lib/user_configs.rb @@ -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