Skip to content

Commit

Permalink
Merge branch 'main' of github.com:cs169/BJC-Teacher-Tracker-App into …
Browse files Browse the repository at this point in the history
…teacher-homeschool-feature
  • Loading branch information
ArushC committed Apr 23, 2024
2 parents 47085fc + ca6c172 commit f095b1a
Show file tree
Hide file tree
Showing 21 changed files with 426 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
6 changes: 4 additions & 2 deletions app/controllers/teachers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class TeachersController < ApplicationController
include CsvProcess

before_action :load_pages, only: [:new, :create, :edit, :update]
before_action :load_teacher, except: [:new, :index, :create, :import]
before_action :load_teacher, except: [:new, :index, :create, :import, :search]
before_action :sanitize_params, only: [:new, :create, :edit, :update]
before_action :require_login, except: [:new, :create]
before_action :require_admin, only: [:validate, :deny, :destroy, :index, :show]
before_action :require_admin, only: [:validate, :deny, :destroy, :index, :show, :search]
before_action :require_edit_permission, only: [:edit, :update, :resend_welcome_email]

rescue_from ActiveRecord::RecordNotUnique, with: :deny_access
Expand All @@ -32,6 +32,7 @@ def index
end

def show
@all_teachers_except_current = Teacher.where.not(id: @teacher.id)
@school = @teacher.school
@status = is_admin? ? "Admin" : "Teacher"
end
Expand Down Expand Up @@ -208,6 +209,7 @@ def import

private
def load_teacher
@teachers = Teacher.all
@teacher ||= Teacher.find(params[:id])
end

Expand Down
4 changes: 4 additions & 0 deletions app/helpers/merge_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module MergeHelper
end
1 change: 1 addition & 0 deletions app/javascript/styles/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ $fa-font-path: '~@fortawesome/fontawesome-free/webfonts';
@import 'layout';
@import 'forms';
@import 'edit_teachers';
@import 'merge_modal';
@import './sidebar.scss';
@import "./selectize.scss";

Expand Down
41 changes: 41 additions & 0 deletions app/javascript/styles/merge_modal.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* Modal styling */
.merge_modal {
display: none; /* Hide the modal by default */
position: fixed;
z-index: 1000; /* Ensure modal appears on top of other elements */
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto; /* Enable scrolling if content exceeds the modal size */
background-color: rgba(0, 0, 0, 0.4); /* Semi-transparent black background */
}

/* Modal content */
.merge_modal-content {
background-color: #fefefe;
margin: 15% auto; /* Center modal vertically and horizontally */
padding: 20px; /* You can increase the padding to add more hspace */
border: 1px solid #888;
border-radius: 5px;
width: 95%; /* You can adjust the width of the modal content as needed */
position: relative; /* Enable positioning of the close button */
}

/* Close button */
.close {
position: absolute; /* Position the close button absolutely */
top: 10px; /* Adjust this value to move the close button up or down */
right: 10px; /* Position the close button on the right side of the modal content */
color: #aaa;
font-size: 28px;
font-weight: bold;
cursor: pointer;
}

.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
4 changes: 2 additions & 2 deletions app/views/main/dashboard.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<table class="table js-dataTable">
<thead class="thead-dark">
<tr>
<%= render 'teachers/table_headers' %>
<%= render 'teachers/table_headers', include_id: false %>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<% @unreviewed_teachers.each do |teacher| %>
<tr>
<%= render 'teachers/teacher', teacher: teacher %>
<%= render 'teachers/teacher', teacher: teacher, merge_table: false %>
<td>
<div class="btn-group" role="group" aria-label="Validate or Remove Teacher">
<%= button_to("✔️", validate_teacher_path(teacher.id),
Expand Down
2 changes: 2 additions & 0 deletions app/views/merge/merge.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Merge#merge</h1>
<p>Find me in app/views/merge/merge.html.erb</p>
67 changes: 67 additions & 0 deletions app/views/merge/preview.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!DOCTYPE html>
<html>
<head>
<title>Merge <%= @from_teacher.full_name %> into <%= @into_teacher.full_name %></title>
<style>
.teacher-info {
display: flex;
}

.teacher-info .teacher {
flex: 1;
}

.teacher-info .teacher + .teacher {
margin-left: 20px; /* Adjust the margin as needed */
}

.header {
display: flex;
justify-content: space-between;
align-items: center;
}

.title {
flex-grow: 1; /* Allow the title to grow to take up remaining space */
}

.button {
margin-left: 10px; /* Adjust margin as needed */
}

.btn-green {
background-color: green;
}

.btn-orange {
background-color: orange;
}

</style>
</head>
<body>
<div class="header">
<div class="title">
<%= content_tag :h1, "Preview Merge of #{@from_teacher.full_name} into #{@into_teacher.full_name}" %>
</div>
<div class="button">
<%= link_to "Switch Merge Order", preview_merge_path(from: @into_teacher.id, into: @from_teacher.id), method: :get, class: "btn btn-primary switch-merge-button btn-orange" %>
</div>
<div class="button">
<%= link_to "Confirm Merge", merge_path(from: @from_teacher.id, into: @into_teacher.id), method: :patch, class: "btn btn-primary confirm-merge-button btn-green" %>
</div>
</div>

<div class="teacher-info">
<%= tag.div class: 'teacher' do %>
<%= render 'teachers/teacher_info', teacher: @from_teacher, render_edit: true, render_smaller: true, render_delete: false, display_deletion_warning_icon: true, is_show_page: false %>
<% end %>
<%= tag.div class: 'teacher' do %>
<%= render 'teachers/teacher_info', teacher: @into_teacher, render_edit: true, render_smaller: true, render_delete: false, display_deletion_warning_icon: false, is_show_page: false %>
<% end %>
<%= tag.div class: 'teacher' do %>
<%= render 'teachers/teacher_info', teacher: @result_teacher, render_edit: false, render_smaller: true, render_delete: false, display_deletion_warning_icon: false, is_show_page: false %>
<% end %>
</div>
</body>
</html>
3 changes: 3 additions & 0 deletions app/views/teachers/_table_headers.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<% if include_id %>
<th scope="col">ID</th>
<% end %>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Education</th>
Expand Down
9 changes: 8 additions & 1 deletion app/views/teachers/_teacher.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<% if merge_table %>
<td><%= teacher.id %></td>
<% end %>
<td>
<%= link_to(teacher.full_name, teacher_path(teacher)) %>
<% if merge_table %>
<%= link_to teacher.full_name, preview_merge_path(@teacher.id, teacher.id), method: :get %>
<% else %>
<%= link_to(teacher.full_name, teacher_path(teacher)) %>
<% end %>
<%= link_to('(Web)', teacher.personal_website, target: '_blank') if teacher.personal_website.present? %>
</td>
<td>
Expand Down
33 changes: 28 additions & 5 deletions app/views/teachers/_teacher_info.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h3><%= teacher.full_name %></h3>
<h3><%= teacher.full_name %>
<% if display_deletion_warning_icon %>
<p style="font-size: smaller;">
</p>
<% end %>
</h3>
<div>
<%= link_to "Edit Information", edit_teacher_path(teacher), class: "btn btn-primary mr-2" %>
<%= link_to "Edit Personal Emails", edit_teacher_email_address_path(teacher), class: "btn btn-secondary mr-2" %>
<%= link_to("Delete", teacher_path(teacher), method: "delete", class: "btn btn-danger", data: {confirm: "Are you sure?"}) %>
<% if is_show_page && @is_admin %>
<button id="merge-button" class="btn btn-warning mr-2">Merge</button>
<% end %>
<% if render_edit %>
<% button_class = render_smaller ? " btn-sm" : "" %>
<%= link_to "Edit Information", edit_teacher_path(teacher), class: "btn btn-primary mr-2" + button_class %>
<%= link_to "Edit Personal Emails", edit_teacher_email_address_path(teacher), class: "btn btn-secondary mr-2" + button_class %>
<% end %>
<% if render_delete %>
<%= link_to("Delete", teacher_path(teacher), method: "delete", class: "btn btn-danger", data: {confirm: "Are you sure?"}) %>
<% end %>
</div>
</div>
<div class="card-body">
<% if @is_admin %>
<div class="row mb-4">
<div class="col-sm-4 font-weight-bold">Teacher ID:</div>
<div class="col-sm-8">
<%= teacher.id %>
</div>
</div>
<% end %>
<div class="row mb-4">
<div class="col-sm-4 font-weight-bold">Email:</div>
<div class="col-sm-6">
Expand All @@ -18,7 +40,7 @@
</div>
</div>
<div class="row mb-4">
<div class="col-sm-4 font-weight-bold">Personal Email:</div>
<div class="col-sm-4 font-weight-bold">Personal Emails:</div>
<div class="col-sm-6">
<% teacher.personal_emails.each do |email| %>
<div class="d-flex align-items-center mb-2">
Expand Down Expand Up @@ -117,6 +139,7 @@
</div>

<script>

function copyToClipboard(text) {
// Create a temporary textarea element to hold the text to copy
const textarea = document.createElement("textarea");
Expand Down
8 changes: 4 additions & 4 deletions app/views/teachers/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
<table class="table table-striped js-dataTable js-teachersTable">
<thead class="thead-dark">
<tr>
<%= render 'table_headers' %>
<%= render 'table_headers', include_id: false %>
</tr>
</thead>
<tbody>
<% @all_teachers.each do |teacher| %>
<tr>
<%= render 'teacher', teacher: teacher %>
<%= render 'teacher', teacher: teacher, merge_table: false %>
</tr>
<% end %>
</tbody>
Expand All @@ -44,13 +44,13 @@
<table class="table table-striped js-dataTable">
<thead class="thead-dark">
<tr>
<%= render 'table_headers' %>
<%= render 'table_headers', include_id: false %>
</tr>
</thead>
<tbody>
<% @admins.each do |admin| %>
<tr>
<%= render 'teacher', teacher: admin %>
<%= render 'teacher', teacher: admin, merge_table: false %>
</tr>
<% end %>
</tbody>
Expand Down
Loading

0 comments on commit f095b1a

Please sign in to comment.