mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-19 13:19:39 +02:00
* Introduce ActiveStorage * Add active storage related service gems * Update storage.yml * Install image processing gem - sudo apt-get install libvips (required dependency) * Set default active storage service * Add profile image to user model * Amend form to allow profile images to be saved, introduce stimulus controller. * Purge image when form is blank * Update markup/stimulus controller * Add test for profile image uplaods * Add profile image validation * Use rails guide gem versions * Use correct ERB syntax and make all storage options configurable * Ensure form submits when user clears profile image * Add profile image thumbnail method * Extract profile image to a partial * Updates env.example and storage.yml * Fix bug with double form save * Add profile image to the sidenav * Update production config * Fix ERB formatting * normalize en.yml * Handle non-square images * Use pre-processing on thumbnail variant * Resovle gemfile.lock issues * Rubocop style changes --------- Signed-off-by: Christian <47796704+crobbo@users.noreply.github.com> Co-authored-by: Christian Robinson <christian@robbo.dev>
89 lines
2.1 KiB
Ruby
89 lines
2.1 KiB
Ruby
class User < ApplicationRecord
|
|
has_secure_password
|
|
|
|
belongs_to :family
|
|
accepts_nested_attributes_for :family
|
|
|
|
validates :email, presence: true, uniqueness: true
|
|
normalizes :email, with: ->(email) { email.strip.downcase }
|
|
|
|
normalizes :first_name, :last_name, with: ->(value) { value.strip.presence }
|
|
|
|
enum :role, { member: "member", admin: "admin" }, validate: true
|
|
|
|
has_one_attached :profile_image do |attachable|
|
|
attachable.variant :thumbnail, resize_to_limit: [ 150, 150 ], preprocessed: true
|
|
end
|
|
|
|
validate :profile_image_size
|
|
|
|
generates_token_for :password_reset, expires_in: 15.minutes do
|
|
password_salt&.last(10)
|
|
end
|
|
|
|
def display_name
|
|
[ first_name, last_name ].compact.join(" ").presence || email
|
|
end
|
|
|
|
def initial
|
|
(display_name&.first || email.first).upcase
|
|
end
|
|
|
|
def acknowledge_upgrade_prompt(commit_sha)
|
|
update!(last_prompted_upgrade_commit_sha: commit_sha)
|
|
end
|
|
|
|
def acknowledge_upgrade_alert(commit_sha)
|
|
update!(last_alerted_upgrade_commit_sha: commit_sha)
|
|
end
|
|
|
|
def has_seen_upgrade_prompt?(upgrade)
|
|
last_prompted_upgrade_commit_sha == upgrade.commit_sha
|
|
end
|
|
|
|
def has_seen_upgrade_alert?(upgrade)
|
|
last_alerted_upgrade_commit_sha == upgrade.commit_sha
|
|
end
|
|
|
|
# Deactivation
|
|
validate :can_deactivate, if: -> { active_changed? && !active }
|
|
after_update_commit :purge_later, if: -> { saved_change_to_active?(from: true, to: false) }
|
|
|
|
def deactivate
|
|
update active: false, email: deactivated_email
|
|
end
|
|
|
|
def can_deactivate
|
|
if admin? && family.users.count > 1
|
|
errors.add(:base, I18n.t("activerecord.errors.user.cannot_deactivate_admin_with_other_users"))
|
|
end
|
|
end
|
|
|
|
def purge_later
|
|
UserPurgeJob.perform_later(self)
|
|
end
|
|
|
|
def purge
|
|
if last_user_in_family?
|
|
family.destroy
|
|
else
|
|
destroy
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def last_user_in_family?
|
|
family.users.count == 1
|
|
end
|
|
|
|
def deactivated_email
|
|
email.gsub(/@/, "-deactivated-#{SecureRandom.uuid}@")
|
|
end
|
|
|
|
def profile_image_size
|
|
if profile_image.attached? && profile_image.byte_size > 5.megabytes
|
|
errors.add(:profile_image, "is too large. Maximum size is 5 MB.")
|
|
end
|
|
end
|
|
end
|