Skip to content

Commit

Permalink
130 - add without_emails and import ujs instead
Browse files Browse the repository at this point in the history
  • Loading branch information
gamesover committed Dec 18, 2024
1 parent 96670de commit 8360520
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
24 changes: 14 additions & 10 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class User < ApplicationRecord
)
}
scope :committee, -> { where(committee: true) }
scope :without_emails, -> {
left_outer_joins(:emails)
.where(emails: { id: nil })
.where.not(email: nil)
}

attr_accessor :skip_subscriptions

Expand All @@ -44,17 +49,16 @@ def save_as_confirmed!

def update_emails
# fetch the email attribute directly from the table
existing_email = self.read_attribute_before_type_cast('email')
if email == nil && existing_email != ''
email = Email.new(email: existing_email, user: self, primary: true)
email.skip_confirmation!
email.save
if email.errors.present?
return errors
end
logger.info 'Email Updated!'
existing_email = read_attribute_before_type_cast('email')
return if existing_email.blank? || email.present?

new_email = Email.new(email: existing_email, user: self, primary: true)
new_email.skip_confirmation!

if new_email.save
logger.info "Email updated for user #{id}: #{existing_email}"
else
logger.info 'Email already exists or no email saved on record'
logger.error "Failed to update email for user #{id}: #{new_email.errors.full_messages.join(', ')}"
end
end

Expand Down
3 changes: 2 additions & 1 deletion app/packs/entrypoints/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import 'core-js/stable'
import 'regenerator-runtime/runtime'

require("@rails/ujs").start()
import Rails from "@rails/ujs"
Rails.start()

const SignaturePad = require("signature_pad").default;

Expand Down
44 changes: 44 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,48 @@
expect(user.deactivated?).to be_truthy
end
end

describe '.without_emails' do
let(:user_with_email_field) do
create(:user).tap do |user|
user[:email] = user.email
user.save!
user.emails.destroy_all
end
end
let(:user_with_emails_association) { create(:user) }

it 'does not return users with email and associated Email record' do
user_with_email_field
user_with_emails_association
expect(User.without_emails).to eq([user_with_email_field])
end
end

describe '#update_emails' do
let(:user_with_email_field) do
create(:user, email: '[email protected]').tap do |user|
user[:email] = user.email
user.save!
user.emails.destroy_all
end
end
let(:user_with_emails_association) { create(:user) }

it 'creates a new Email record for users with email but no associated Email record' do
user_with_email_field

expect { user_with_email_field.update_emails }.to change(Email, :count).by(1)
expect(user_with_email_field.reload.read_attribute_before_type_cast('email')).to eq('[email protected]')
expect(user_with_email_field.emails.first).to have_attributes(
email: '[email protected]',
primary: true,
)
end

it 'does not create a new Email record for users with email and associated Email record' do
user_with_emails_association
expect { user_with_emails_association.update_emails }.not_to change(Email, :count)
end
end
end

0 comments on commit 8360520

Please sign in to comment.