Skip to content

Commit

Permalink
Merge branch 'main' into 187375265/add-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ArushC committed Apr 22, 2024
2 parents 482e3c8 + ca6c172 commit 2b3154b
Show file tree
Hide file tree
Showing 29 changed files with 537 additions and 131 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.2.3" # TODO: use file: '.tool-versions' when heroku supports it. (bundler >= 2.5)

gem "rails", "6.1.7.6"
gem "rails", "6.1.7.7"

# Production app server
gem "puma", "~> 5"
Expand Down
114 changes: 57 additions & 57 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,62 +24,62 @@ GIT
GEM
remote: https://rubygems.org/
specs:
actioncable (6.1.7.6)
actionpack (= 6.1.7.6)
activesupport (= 6.1.7.6)
actioncable (6.1.7.7)
actionpack (= 6.1.7.7)
activesupport (= 6.1.7.7)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (6.1.7.6)
actionpack (= 6.1.7.6)
activejob (= 6.1.7.6)
activerecord (= 6.1.7.6)
activestorage (= 6.1.7.6)
activesupport (= 6.1.7.6)
actionmailbox (6.1.7.7)
actionpack (= 6.1.7.7)
activejob (= 6.1.7.7)
activerecord (= 6.1.7.7)
activestorage (= 6.1.7.7)
activesupport (= 6.1.7.7)
mail (>= 2.7.1)
actionmailer (6.1.7.6)
actionpack (= 6.1.7.6)
actionview (= 6.1.7.6)
activejob (= 6.1.7.6)
activesupport (= 6.1.7.6)
actionmailer (6.1.7.7)
actionpack (= 6.1.7.7)
actionview (= 6.1.7.7)
activejob (= 6.1.7.7)
activesupport (= 6.1.7.7)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (6.1.7.6)
actionview (= 6.1.7.6)
activesupport (= 6.1.7.6)
actionpack (6.1.7.7)
actionview (= 6.1.7.7)
activesupport (= 6.1.7.7)
rack (~> 2.0, >= 2.0.9)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (6.1.7.6)
actionpack (= 6.1.7.6)
activerecord (= 6.1.7.6)
activestorage (= 6.1.7.6)
activesupport (= 6.1.7.6)
actiontext (6.1.7.7)
actionpack (= 6.1.7.7)
activerecord (= 6.1.7.7)
activestorage (= 6.1.7.7)
activesupport (= 6.1.7.7)
nokogiri (>= 1.8.5)
actionview (6.1.7.6)
activesupport (= 6.1.7.6)
actionview (6.1.7.7)
activesupport (= 6.1.7.7)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (6.1.7.6)
activesupport (= 6.1.7.6)
activejob (6.1.7.7)
activesupport (= 6.1.7.7)
globalid (>= 0.3.6)
activemodel (6.1.7.6)
activesupport (= 6.1.7.6)
activerecord (6.1.7.6)
activemodel (= 6.1.7.6)
activesupport (= 6.1.7.6)
activemodel (6.1.7.7)
activesupport (= 6.1.7.7)
activerecord (6.1.7.7)
activemodel (= 6.1.7.7)
activesupport (= 6.1.7.7)
activerecord-import (1.4.0)
activerecord (>= 4.2)
activestorage (6.1.7.6)
actionpack (= 6.1.7.6)
activejob (= 6.1.7.6)
activerecord (= 6.1.7.6)
activesupport (= 6.1.7.6)
activestorage (6.1.7.7)
actionpack (= 6.1.7.7)
activejob (= 6.1.7.7)
activerecord (= 6.1.7.7)
activesupport (= 6.1.7.7)
marcel (~> 1.0)
mini_mime (>= 1.1.0)
activesupport (6.1.7.6)
activesupport (6.1.7.7)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
Expand Down Expand Up @@ -374,7 +374,7 @@ GEM
puma (5.6.8)
nio4r (~> 2.0)
racc (1.7.3)
rack (2.2.8)
rack (2.2.8.1)
rack-mini-profiler (2.3.3)
rack (>= 1.2.0)
rack-protection (3.2.0)
Expand All @@ -384,20 +384,20 @@ GEM
rack
rack-test (2.1.0)
rack (>= 1.3)
rails (6.1.7.6)
actioncable (= 6.1.7.6)
actionmailbox (= 6.1.7.6)
actionmailer (= 6.1.7.6)
actionpack (= 6.1.7.6)
actiontext (= 6.1.7.6)
actionview (= 6.1.7.6)
activejob (= 6.1.7.6)
activemodel (= 6.1.7.6)
activerecord (= 6.1.7.6)
activestorage (= 6.1.7.6)
activesupport (= 6.1.7.6)
rails (6.1.7.7)
actioncable (= 6.1.7.7)
actionmailbox (= 6.1.7.7)
actionmailer (= 6.1.7.7)
actionpack (= 6.1.7.7)
actiontext (= 6.1.7.7)
actionview (= 6.1.7.7)
activejob (= 6.1.7.7)
activemodel (= 6.1.7.7)
activerecord (= 6.1.7.7)
activestorage (= 6.1.7.7)
activesupport (= 6.1.7.7)
bundler (>= 1.15.0)
railties (= 6.1.7.6)
railties (= 6.1.7.7)
sprockets-rails (>= 2.0.0)
rails-controller-testing (1.0.5)
actionpack (>= 5.0.1.rc1)
Expand All @@ -410,9 +410,9 @@ GEM
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
railties (6.1.7.6)
actionpack (= 6.1.7.6)
activesupport (= 6.1.7.6)
railties (6.1.7.7)
actionpack (= 6.1.7.7)
activesupport (= 6.1.7.7)
method_source
rake (>= 12.2)
thor (~> 1.0)
Expand All @@ -421,7 +421,7 @@ GEM
rb-fsevent (0.11.0)
rb-inotify (0.10.1)
ffi (~> 1.0)
rdoc (6.6.2)
rdoc (6.6.3.1)
psych (>= 4.0.0)
regexp_parser (2.8.2)
reline (0.4.2)
Expand Down Expand Up @@ -528,7 +528,7 @@ GEM
temple (0.8.2)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
thor (1.3.0)
thor (1.3.1)
thread_safe (0.3.6)
tilt (2.0.10)
timeout (0.4.1)
Expand Down Expand Up @@ -598,7 +598,7 @@ DEPENDENCIES
pronto-rubocop
puma (~> 5)
rack-mini-profiler (~> 2.0)
rails (= 6.1.7.6)
rails (= 6.1.7.7)
rails-controller-testing
rspec-rails
rubocop
Expand Down
17 changes: 17 additions & 0 deletions app/controllers/concerns/school_params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module SchoolParams
private
def unique_school_params
{
name: school_params[:name],
country: school_params[:country],
city: school_params[:city],
state: school_params[:state]
}
end

def school_params
params.require(:school).permit(:name, :country, :city, :state, :website, :grade_level, :school_type, :country, { tags: [] }, :nces_id)
end
end
4 changes: 2 additions & 2 deletions app/controllers/email_templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def update
flash[:success] = "Updated #{@email_template.title} template successfully."
redirect_to email_templates_path
else
flash.now[:alert] = "An error occurred: #{@email_template.errors.full_messages.join(', ')}"
flash.now[:alert] = "An error occurred: #{@email_template.errors.full_messages.join(", ")}"
render "edit"
end
end
Expand All @@ -36,7 +36,7 @@ def create
flash[:success] = "Created #{@email_template.title} successfully."
redirect_to email_templates_path
else
flash.now[:alert] = "An error occurred: #{@email_template.errors.full_messages.join(', ')}"
flash.now[:alert] = "An error occurred: #{@email_template.errors.full_messages.join(", ")}"
render "new"
end
end
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/main_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def index
elsif is_teacher? && !current_user.validated?
message = "Your application is #{mapped_application_status(current_user.application_status)}. You may update your information. Please check your email for more information."
# Both `info_needed` and `not_reviewed` flash message should be flash[:warn] style
# flash[:warn] for yellow warning message in bootstrap style
redirect_to edit_teacher_path(current_user.id), warn: message
# if this user is denied, redirect to new
else
Expand All @@ -35,8 +34,7 @@ def mapped_application_status(status)
status_map = {
"info_needed" => "requested to be updated",
"not_reviewed" => "not reviewed"
}
# Fetch the mapped status if present; otherwise, return the original status
}.with_indifferent_access
status_map.fetch(status, status)
end
end
55 changes: 55 additions & 0 deletions app/controllers/merge_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

class MergeController < ApplicationController
before_action :require_admin

def preview
@from_teacher = Teacher.find(params[:from])
@into_teacher = Teacher.find(params[:into])
@result_teacher = merge_teachers(@from_teacher, @into_teacher)
end

def execute
@from_teacher = Teacher.find(params[:from])
@into_teacher = Teacher.find(params[:into])
@merged_teacher = merge_teachers(@from_teacher, @into_teacher)

merged_attributes = @merged_teacher.attributes.except("id")
@into_teacher.update!(merged_attributes)
@from_teacher.destroy
redirect_to teachers_path, notice: "Teachers merged successfully."
end

private
# Returns a merged teacher without saving it to the database.
# Rendered in the preview, and only saved to the DB in a call to merge.
def merge_teachers(from_teacher, into_teacher)
merged_attributes = {}
into_teacher.attributes.each do |attr_name, attr_value|
from_teacher_attr_value = from_teacher.attributes[attr_name]
if attr_value.blank?
merged_attributes[attr_name] = from_teacher_attr_value
elsif from_teacher_attr_value.blank?
merged_attributes[attr_name] = attr_value
else
case attr_name
when "session_count"
merged_attributes[attr_name] = attr_value + from_teacher_attr_value
when "ip_history"
merged_attributes[attr_name] = (attr_value + from_teacher_attr_value).uniq
when "last_session_at"
# The resulting last session time is the most recent one
merged_attributes[attr_name] = attr_value > from_teacher_attr_value ? attr_value : from_teacher_attr_value
when "created_at"
# The resulting record creation time is the least recent one
merged_attributes[attr_name] = attr_value < from_teacher_attr_value ? attr_value : from_teacher_attr_value
else
merged_attributes[attr_name] = attr_value
end
end
end

merged_teacher = Teacher.new(merged_attributes)
merged_teacher
end
end
9 changes: 3 additions & 6 deletions app/controllers/schools_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

class SchoolsController < ApplicationController
include SchoolParams
before_action :require_admin

def index
Expand All @@ -12,11 +13,11 @@ def show
end

def search
School.all.collect { |school| ["#{school.name}, #{school.country}, #{school.city}, #{school.state}", school.name] }
School.search_list
end

def create
@school = School.find_by(name: school_params[:name], country: school_params[:country], city: school_params[:city], state: school_params[:state])
@school = School.find_by(**unique_school_params)
if @school
@school.assign_attributes(school_params)
else
Expand Down Expand Up @@ -64,10 +65,6 @@ def destroy
end

private
def school_params
params.require(:school).permit(:name, :country, :city, :state, :website, :grade_level, :school_type, :country, { tags: [] }, :nces_id)
end

def load_ordered_schools
@ordered_schools ||= School.all.order(:name)
end
Expand Down
Loading

0 comments on commit 2b3154b

Please sign in to comment.