Skip to content

Commit

Permalink
fixed merge conflicts wrt main
Browse files Browse the repository at this point in the history
  • Loading branch information
ArushC committed Apr 22, 2024
2 parents 7fb905a + a1fda8e commit 46a8388
Show file tree
Hide file tree
Showing 32 changed files with 1,466 additions and 84 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,7 @@ group :test do
# Accessibility Testing
gem "axe-core-rspec"
gem "axe-core-cucumber"

# Test suite speedup
gem "rails-controller-testing"
end
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ GEM
bundler (>= 1.15.0)
railties (= 6.1.7.7)
sprockets-rails (>= 2.0.0)
rails-controller-testing (1.0.5)
actionpack (>= 5.0.1.rc1)
actionview (>= 5.0.1.rc1)
activesupport (>= 5.0.1.rc1)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
Expand Down Expand Up @@ -595,6 +599,7 @@ DEPENDENCIES
puma (~> 5)
rack-mini-profiler (~> 2.0)
rails (= 6.1.7.7)
rails-controller-testing
rspec-rails
rubocop
rubocop-faker
Expand Down
46 changes: 46 additions & 0 deletions app/controllers/email_addresses_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

class EmailAddressesController < ApplicationController
before_action :require_login
before_action :require_admin
before_action :set_teacher

def edit
end

def update
if update_personal_emails
redirect_to teacher_path(@teacher), notice: "Personal email addresses updated successfully."
else
flash.now[:alert] = "An error occurred: " + (@error_messages || "Unknown error")
render :edit
end
end

private
def set_teacher
@teacher = Teacher.find(params[:teacher_id])
end

def update_personal_emails
EmailAddress.transaction do
params[:teacher][:email_addresses_attributes].each do |_, email_attr|
if email_attr[:id].present?
email = EmailAddress.find(email_attr[:id])
if email_attr[:_destroy] == "1"
email.destroy!
else
email.update!(email: email_attr[:email])
end
else
@teacher.email_addresses.create!(email: email_attr[:email]) unless email_attr[:email].blank?
end
end
end
true
rescue ActiveRecord::RecordInvalid => e
@error_messages = e.record&.errors&.full_messages&.join(", ")
@error_messages ||= "A validation error occurred, but no specific error details are available."
false
end
end
72 changes: 72 additions & 0 deletions app/controllers/pd_registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true

class PdRegistrationsController < ApplicationController
before_action :require_login
before_action :require_admin
before_action :set_pd_registration, only: [:show, :edit, :update, :destroy]
before_action :set_professional_development, only: [:new, :create, :edit, :update, :destroy]

def index
end

def show
end

def new
@pd_registration = PdRegistration.new
end

def edit
end

def create
@pd_registration = PdRegistration.new(pd_registration_params.merge(
professional_development_id: @professional_development.id))

if @pd_registration.save
redirect_to professional_development_path(@professional_development),
notice: "Registration for professional development was successfully created."
else
flash.now[:alert] = @pd_registration.errors.full_messages.to_sentence
render "professional_developments/show"
end
end

def update
if @pd_registration.update(pd_registration_params)
redirect_to professional_development_path(@professional_development),
notice: "Registration was successfully updated."
else
flash.now[:alert] = @pd_registration.errors.full_messages.to_sentence
render "professional_developments/show"
end
end

def destroy
if @pd_registration.destroy
redirect_to professional_development_path(@professional_development),
notice: "Registration was successfully cancelled."
else
flash.now[:alert] = @pd_registration.errors.full_messages.to_sentence
render "professional_developments/show"
end
end

private
def set_pd_registration
@pd_registration = PdRegistration.find(params[:id])
rescue ActiveRecord::RecordNotFound
redirect_to professional_development_path, alert: "Registration not found."
end

def set_professional_development
@professional_development = ProfessionalDevelopment.find_by(id: params[:professional_development_id])
unless @professional_development
redirect_to professional_developments_path, alert: "Professional Development not found."
end
end

def pd_registration_params
params.require(:pd_registration).permit(:teacher_id, :attended, :role, :professional_development_id)
end
end
61 changes: 61 additions & 0 deletions app/controllers/professional_developments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

class ProfessionalDevelopmentsController < ApplicationController
before_action :set_professional_development, only: [:show, :edit, :update, :destroy]
before_action :require_login
before_action :require_admin

def index
@professional_developments = ProfessionalDevelopment.all
end

def show
end

def new
@professional_development = ProfessionalDevelopment.new
end

def edit
end

def create
@professional_development = ProfessionalDevelopment.new(professional_development_params)

if @professional_development.save
redirect_to @professional_development, notice: "Professional development created successfully."
else
flash.now[:alert] = @professional_development.errors.full_messages.to_sentence
render :new
end
end

def update
if @professional_development.update(professional_development_params)
redirect_to @professional_development, notice: "Professional development updated successfully."
else
flash.now[:alert] = @professional_development.errors.full_messages.to_sentence
render :edit
end
end

def destroy
if @professional_development.destroy
redirect_to professional_developments_url, notice: "Professional development deleted successfully."
else
redirect_to professional_developments_url, alert: "Failed to delete professional development."
end
end

private
def set_professional_development
@professional_development = ProfessionalDevelopment.find(params[:id])
rescue ActiveRecord::RecordNotFound
redirect_to professional_developments_url, alert: "Professional development not found."
end

def professional_development_params
params.require(:professional_development).permit(:name, :city, :state, :country, :start_date, :end_date,
:grade_level)
end
end
118 changes: 66 additions & 52 deletions app/controllers/teachers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,7 @@ def new
# TODO: This needs to be re-written.
# If you are logged in and not an admin, this should fail.
def create
@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.primary_email}'. Please log in."
if existing_teacher
return
end

Expand Down Expand Up @@ -103,13 +96,10 @@ def update
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
Expand All @@ -120,32 +110,29 @@ def update
end
@teacher.school = @school
end
send_email_if_application_status_changed_and_email_resend_enabled
if @teacher.denied? && !is_admin?
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_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]."

valid_school = update_school_through_teacher
if !valid_school
return
end
unless @teacher.save
redirect_to edit_teacher_path(params[:id]),
alert: "An error occurred: #{@teacher.errors.full_messages.join(', ')}"

send_email_if_application_status_changed_and_email_resend_enabled

if fail_to_update
return
end

if !@teacher.validated? && !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(params[:id]), notice: "Saved #{@teacher.full_name}"
return
else
@teacher.try_append_ip(request.remote_ip)
redirect_to edit_teacher_path(params[:id]), notice: "Successfully updated your information"
end
redirect_to edit_teacher_path(params[:id]), notice: "Successfully updated your information"
end

def send_email_if_application_status_changed_and_email_resend_enabled
Expand Down Expand Up @@ -217,6 +204,49 @@ def deny_access
redirect_to new_teacher_path, alert: "Email address or Snap username already in use. Please use a different email or Snap username."
end

def existing_teacher
# Find by email, but allow updating other info.
@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 true
elsif @teacher
redirect_to login_path, notice: "You already have signed up with '#{@teacher.email}'. Please log in."
return true
end
false
end

def update_school_through_teacher
if !teacher_params[:school_id].present?
@school.update(school_params) if school_params
unless @school.save
flash[:alert] = "An error occurred: #{@school.errors.full_messages.join(', ')}"
render "new"
return false
end
end
@teacher.school = @school
true
end

def fail_to_update
if @teacher.denied? && !is_admin?
redirect_to root_path, alert: "Failed to update your information. You have already been denied. If you have questions, please email [email protected]."
return true
elsif (@teacher.email_changed_flag || @teacher.snap_changed?) && !is_admin?
flash.now[:alert] = "Failed to update your information. If you want to change your email or Snap! username, please email [email protected]."
render "edit"
return true
elsif !@teacher.save
flash.now[:alert] = "Failed to update data. #{@teacher.errors.full_messages.to_sentence}"
render "edit"
return true
end
false
end

def load_school
if teacher_params[:school_id].present?
@school ||= School.find(teacher_params[:school_id])
Expand Down Expand Up @@ -248,22 +278,20 @@ def ordered_schools
end

def sanitize_params
if params[:teacher]
if params[:teacher][:status]
params[:teacher][:status] = params[:teacher][:status].to_i
end
if params[:teacher][:education_level]
params[:teacher][:education_level] = params[:teacher][:education_level].to_i
end
teacher = params[:teacher]
if teacher && teacher[:status]
teacher[:status] = teacher[:status].to_i
end
if teacher && teacher[:education_level]
teacher[:education_level] = teacher[:education_level].to_i
end

if params[:school]
if params[:school][:grade_level]
params[:school][:grade_level] = params[:school][:grade_level].to_i
end
if params[:school][:school_type]
params[:school][:school_type] = params[:school][:school_type].to_i
end
school = params[:school]
if school && school[:grade_level]
school[:grade_level] = school[:grade_level].to_i
end
if school && school[:school_type]
school[:school_type] = school[:school_type].to_i
end
end

Expand All @@ -282,18 +310,4 @@ def update_primary_email(primary_email)

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
3 changes: 2 additions & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def admin_nav_links
"Dashboard": dashboard_path,
"Schools": schools_path,
"Teachers": teachers_path,
"Email Templates": email_templates_path
"Email Templates": email_templates_path,
"PD": professional_developments_path,
}
end

Expand Down
Loading

0 comments on commit 46a8388

Please sign in to comment.