forked from beautyjoy/BJC-Teacher-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 187375265/add-testing
- Loading branch information
Showing
29 changed files
with
537 additions
and
131 deletions.
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
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,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 |
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 |
---|---|---|
@@ -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 |
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.