forked from beautyjoy/BJC-Teacher-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
187384622/new email model - PR1 - Change Email and Make Sure original tests didn't break #49
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
afe77cc
Backup model updates
perryzjc 47f2799
Finish model updates
perryzjc 8f7d9ec
Update seed data
perryzjc b0b20cd
Update controllers
perryzjc a2bf8dc
Update frontend
perryzjc e7e88b4
Update fixtures
perryzjc 9e2d47e
Update cucumber tests
perryzjc 96ba6e5
Update rspec tests
perryzjc 9de1f36
Fix code for email usage in the session
perryzjc a486c25
Change email regex to the rail default email regex
perryzjc d62937c
Remove email validation due to original email format problem
perryzjc 6d4a956
Revert back the email validation
perryzjc 81b15db
Adjust personal email handling for this PR
perryzjc 358140a
Kept original email column in teacher for this PR
perryzjc a1fc144
Change order to improve readability
perryzjc a825a5f
Clean up naming & data handling
perryzjc 63dd1ab
Shorten code finding teacher
perryzjc 793733b
Clarify and shorten primary email update logic
perryzjc 74b8b83
Fix typo
perryzjc ee559cc
Refract param handling
perryzjc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,22 +42,23 @@ def new | |
@school = @teacher.school # maybe delegate this | ||
@readonly = false | ||
if omniauth_data.present? | ||
email_data = { email: omniauth_data.delete("email"), primary: true } | ||
@teacher.assign_attributes(omniauth_data) | ||
@teacher.email_addresses.build(email_data) | ||
end | ||
end | ||
|
||
# TODO: This needs to be re-written. | ||
# If you are logged in and not an admin, this should fail. | ||
def create | ||
# Find by email, but allow updating other info. | ||
@teacher = Teacher.find_by(email: teacher_params[:email]) | ||
@teacher = EmailAddress.find_by(email: params.dig(:email, :primary))&.teacher | ||
if @teacher && defined?(current_user.id) && (current_user.id == @teacher.id) | ||
params[:id] = current_user.id | ||
update | ||
return | ||
elsif @teacher | ||
redirect_to login_path, | ||
notice: "You already have signed up with '#{@teacher.email}'. Please log in." | ||
notice: "You already have signed up with '#{@teacher.primary_email}'. Please log in." | ||
return | ||
end | ||
|
||
|
@@ -71,12 +72,14 @@ def create | |
end | ||
|
||
@teacher = Teacher.new(teacher_params) | ||
@teacher.email_addresses.build(email: params[:email][:primary], primary: true) | ||
|
||
@teacher.try_append_ip(request.remote_ip) | ||
@teacher.session_count += 1 | ||
@teacher.school = @school | ||
if @teacher.save | ||
@teacher.not_reviewed! | ||
flash[:success] = "Thanks for signing up for BJC, #{@teacher.first_name}! You'll hear from us shortly. Your email address is: #{@teacher.email}." | ||
flash[:success] = "Thanks for signing up for BJC, #{@teacher.first_name}! You'll hear from us shortly. Your email address is: #{@teacher.primary_email}." | ||
TeacherMailer.form_submission(@teacher).deliver_now | ||
TeacherMailer.teacher_form_submission(@teacher).deliver_now | ||
redirect_to root_path | ||
|
@@ -95,7 +98,15 @@ def edit | |
def update | ||
load_school | ||
ordered_schools | ||
|
||
primary_email = params.dig(:email, :primary) | ||
personal_emails = params[:email]&.select { |key, value| key.start_with?("personal_") }&.values | ||
|
||
# Now, `params[:teacher]` does not contain primary_email or any personal_emailX fields | ||
@teacher.assign_attributes(teacher_params) | ||
|
||
update_primary_email(primary_email) | ||
update_personal_emails(personal_emails) | ||
if teacher_params[:school_id].present? | ||
@teacher.school = @school | ||
else | ||
|
@@ -111,26 +122,27 @@ def update | |
redirect_to root_path, alert: "Failed to update your information. You have already been denied. If you have questions, please email [email protected]." | ||
return | ||
end | ||
if (@teacher.email_changed? || @teacher.snap_changed?) && !is_admin? | ||
redirect_to edit_teacher_path(current_user.id), alert: "Failed to update your information. If you want to change your email or Snap! username, please email [email protected]." | ||
if (@teacher.email_changed_flag || @teacher.snap_changed?) && !is_admin? | ||
@teacher.email_changed_flag = false | ||
redirect_to edit_teacher_path(params[:id]), alert: "Failed to update your information. If you want to change your email or Snap! username, please email [email protected]." | ||
return | ||
end | ||
if !@teacher.save | ||
redirect_to edit_teacher_path(current_user.id), | ||
alert: "An error occurred: #{@teacher.errors.full_messages.join(', ')}" | ||
unless @teacher.save | ||
redirect_to edit_teacher_path(params[:id]), | ||
alert: "An error occurred: #{@teacher.errors.full_messages.join(', ')}" | ||
return | ||
end | ||
if [email protected]? && !current_user.admin? | ||
TeacherMailer.form_submission(@teacher).deliver_now | ||
TeacherMailer.teacher_form_submission(@teacher).deliver_now | ||
end | ||
if is_admin? | ||
redirect_to edit_teacher_path(current_user.id), notice: "Saved #{@teacher.full_name}" | ||
redirect_to edit_teacher_path(params[:id]), notice: "Saved #{@teacher.full_name}" | ||
return | ||
else | ||
@teacher.try_append_ip(request.remote_ip) | ||
end | ||
redirect_to edit_teacher_path(current_user.id), notice: "Successfully updated your information" | ||
redirect_to edit_teacher_path(params[:id]), notice: "Successfully updated your information" | ||
end | ||
|
||
def send_email_if_application_status_changed_and_email_resend_enabled | ||
|
@@ -209,13 +221,12 @@ def load_school | |
end | ||
|
||
def teacher_params | ||
teacher_attributes = [:first_name, :last_name, :school, :email, :status, :snap, | ||
:more_info, :personal_website, :education_level, :school_id] | ||
if is_admin? | ||
teacher_attributes << [:personal_email, :application_status, | ||
:request_reason, :skip_email] | ||
end | ||
params.require(:teacher).permit(*teacher_attributes, languages: []) | ||
teacher_attributes = [:first_name, :last_name, :school, :status, :snap, | ||
:more_info, :personal_website, :education_level, :school_id, languages: []] | ||
admin_attributes = [:application_status, :request_reason, :skip_email] | ||
teacher_attributes.push(*admin_attributes) if is_admin? | ||
|
||
params.require(:teacher).permit(*teacher_attributes) | ||
end | ||
|
||
def school_params | ||
|
@@ -230,7 +241,7 @@ def omniauth_data | |
def ordered_schools | ||
if params[:id].present? | ||
load_teacher | ||
@ordered_schools ||= [ @teacher.school ] + | ||
@ordered_schools ||= [@teacher.school] + | ||
School.all.order(:name).reject { |s| s.id == @teacher.school_id } | ||
else | ||
@ordered_schools ||= School.all.order(:name) | ||
|
@@ -260,4 +271,30 @@ def sanitize_params | |
def load_pages | ||
@pages ||= Page.where(viewer_permissions: Page.viewable_pages(current_user)) | ||
end | ||
|
||
def update_primary_email(primary_email) | ||
return unless primary_email.present? | ||
|
||
# First, ensure the current primary email is marked as not primary if it's not the same as the new one | ||
@teacher.email_addresses.find_by(primary: true)&.update(primary: false) | ||
|
||
primary_email_record = @teacher.email_addresses.find_or_initialize_by(email: primary_email) | ||
primary_email_record.primary = true | ||
|
||
primary_email_record.save if primary_email_record.changed? | ||
end | ||
|
||
def update_personal_emails(personal_emails) | ||
return unless personal_emails.present? | ||
personal_emails = personal_emails.reject(&:empty?) | ||
return if personal_emails.empty? | ||
|
||
current_emails = @teacher.email_addresses.pluck(:email) | ||
|
||
new_emails = personal_emails - current_emails | ||
|
||
new_emails.each do |email| | ||
@teacher.email_addresses.build(email:, primary: false) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: email_addresses | ||
# | ||
# id :bigint not null, primary key | ||
# email :string not null | ||
# primary :boolean default(FALSE), not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# teacher_id :bigint not null | ||
# | ||
# Indexes | ||
# | ||
# index_email_addresses_on_email (email) UNIQUE | ||
# index_email_addresses_on_teacher_id (teacher_id) | ||
# index_email_addresses_on_teacher_id_and_primary (teacher_id,primary) UNIQUE WHERE ("primary" = true) | ||
# | ||
# Foreign Keys | ||
# | ||
# fk_rails_... (teacher_id => teachers.id) | ||
# | ||
class EmailAddress < ApplicationRecord | ||
belongs_to :teacher | ||
|
||
# Rail's bulit-in validation for email format regex | ||
validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP } | ||
validate :only_one_primary_email_per_teacher | ||
|
||
before_save :normalize_email | ||
before_save :flag_teacher_if_email_changed | ||
|
||
private | ||
def only_one_primary_email_per_teacher | ||
if primary? && EmailAddress.where(teacher_id:, primary: true).where.not(id:).exists? | ||
errors.add(:primary, "There can only be one primary email per teacher.") | ||
end | ||
end | ||
|
||
def normalize_email | ||
self.email = email.strip.downcase | ||
end | ||
|
||
def flag_teacher_if_email_changed | ||
if self.email_changed? && !self.new_record? | ||
teacher.email_changed_flag = true | ||
teacher.handle_relevant_changes | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,9 +57,9 @@ status ONLY IF the person viewing this page is an admin. %> | |
|
||
<div class='form-group row'> | ||
<div class='col-6'> | ||
<%= f.label :email, "School Email", class: "label-required" %> | ||
<%= f.text_field :email, placeholder: '[email protected]', class: 'form-control', | ||
required: true, type: :email, readonly: @readonly %> | ||
<%= label_tag 'email[primary]', "School Email", class: "label-required" %> | ||
<%= text_field_tag 'email[primary]', @teacher.primary_email, placeholder: '[email protected]', | ||
class: 'form-control', required: true, type: :email, readonly: @readonly %> | ||
</div> | ||
|
||
<div class='col-6'> | ||
|
@@ -81,13 +81,14 @@ status ONLY IF the person viewing this page is an admin. %> | |
</div> | ||
|
||
<%# For now... only admins can enter/edit personal emails. %> | ||
<%- if current_user&.admin? || @teacher.personal_email.present? %> | ||
|
||
<%- if current_user&.admin? || @teacher.personal_emails.present? %> | ||
<div class="form-group row"> | ||
<div class='col-12'> | ||
<%= f.label :personal_email do %> | ||
<%= label_tag "email[personal_1]" do %> | ||
Personal Email</i> | ||
<% end %> | ||
<%= f.text_field :personal_email, placeholder: '[email protected]', class: 'form-control', | ||
<%= text_field_tag "email[personal_1]", @teacher.personal_emails, placeholder: '[email protected]', class: 'form-control', | ||
required: false, type: :email, readonly: @readonly %> | ||
</div> | ||
</div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't for this PR, but the creation workflow when signed in seems like it doesn't make sense anymore. We should revisit this...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Marked. We will discuss it during next available meeting.