Skip to content
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

Refactor Teacher#edit and #update #321

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions app/controllers/teachers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ def new
end
end

# TODO: This needs to be re-written.
# If you are logged in and not an admin, this should fail.
def create
if existing_teacher
redirect_to login_path,
notice: "You already have signed up with '#{params[:email]}'. Please log in."
return
end

Expand Down Expand Up @@ -105,6 +105,15 @@ def upload_file
end

def 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
elsif @teacher.id != current_user.id && !is_admin?
Sentry.capture_message("BAD UPDATE: #{current_user.id} attempted to edit #{@teacher.id}")
redirect_to root_path, alert: "You are attempting to update another user's record."
return
end

load_school
ordered_schools

Expand All @@ -124,11 +133,14 @@ def update
@teacher.school = @school
end

valid_school = update_school_through_teacher
if !valid_school
return
if @teacher.admin_attributes_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]." and return
end

valid_school = update_school_through_teacher
return if !valid_school

attach_new_files_if_any
send_email_if_application_status_changed_and_email_resend_enabled

Expand All @@ -137,15 +149,16 @@ def update
end

if [email protected]? && !current_user.admin?
@teacher.not_reviewed!
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}"
redirect_to teacher_path(@teacher), notice: "Saved #{@teacher.full_name}"
else
@teacher.try_append_ip(request.remote_ip)
redirect_to edit_teacher_path(params[:id]), notice: "Successfully updated your information"
redirect_to root_path, notice: "Saved successfully. Thanks!"
end
end

Expand Down
13 changes: 5 additions & 8 deletions app/models/teacher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,6 @@ def handle_relevant_changes
reset_validation_status
end

def reset_validation_status
return if application_status_changed? || school_id_changed?
if info_needed?
not_reviewed!
end
end

def full_name
"#{first_name} #{last_name}"
end
Expand Down Expand Up @@ -165,6 +158,10 @@ def snap_username
self.snap
end

def admin_attributes_changed?
self.email_changed? || self.personal_email_changed? || self.snap_changed?
end

def try_append_ip(ip)
return if ip_history.include?(ip)
self.ip_history << ip
Expand Down Expand Up @@ -288,7 +285,7 @@ def email_attributes
}.transform_values { |value| value.blank? ? "(blank)" : value }
end

# TODO: The school data needs to be cleaned up.
# TODO: Move this to a TeacherCSVExports lib file
def self.csv_export
attributes = %w|
id
Expand Down
Loading