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

187216522 international schools #295

Merged
merged 69 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 67 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
f2ee4d2
Added frontend + db logic for country field in international schools
razztech Mar 8, 2024
1000250
Added some new frontend logic
razztech Mar 8, 2024
277e4de
Made changes to UI logic and updated tests
razztech Mar 11, 2024
43b0a3d
Update tests to use country dropdown
razztech Mar 11, 2024
bfe7a19
Merge pull request #39 from cs169/187129573-change-teacher-status
perryzjc Mar 11, 2024
f3cd0cd
All tests are passing
razztech Mar 11, 2024
dc852f3
Finished table country display
razztech Mar 11, 2024
fa48319
Added filtering with a different column
razztech Mar 11, 2024
fadef9a
Fixed rubocop
razztech Mar 11, 2024
1320a4e
Added state textfield test for international schools
razztech Mar 11, 2024
2fe9505
Merge branch 'main' into 187216522-international-schools
razztech Mar 12, 2024
b2c8e47
Fixed merge issues for cucumber tests
razztech Mar 12, 2024
c9df771
Added international school test, moved custom web step to form_steps.rb:
razztech Mar 12, 2024
5d8654b
Fixed rubocop
razztech Mar 12, 2024
6660f2e
Added country validation
razztech Mar 12, 2024
39123b8
Added new test for country
razztech Mar 12, 2024
d27c514
Changed UI based on Michael's feedback
razztech Mar 13, 2024
29de296
Made UI changes requested by Michael 2
razztech Mar 13, 2024
2c0363d
Changed from VanillaJS to jQuerry suggested by Arush
razztech Mar 13, 2024
5135d22
Merge pull request #38 from cs169/187186207/iter1-golden-repo-fixes
ArushC Mar 13, 2024
677bb6e
Further change from VanillaJS to jQuerry suggested by Arush
perryzjc Mar 13, 2024
772f2df
Add cucumber test for unexpected user behavior
perryzjc Mar 13, 2024
05d989e
fix glitch
ArushC Mar 14, 2024
a2ea667
resolve merge conflict
ArushC Mar 14, 2024
fa1155b
ok actually fixed merge conflict now
ArushC Mar 14, 2024
a155d65
no choice, have to resolve conflict manually
ArushC Mar 14, 2024
da5f122
Merge branch 'main' into 187216522-international-schools
ArushC Mar 14, 2024
71b92b6
Merge pull request #42 from cs169/187216522-international-schools
ArushC Mar 14, 2024
3f190c3
admin update teacher now redirect to show teacher page
ArushC Mar 18, 2024
58abf8d
fix javascript to make state select required
ArushC Mar 18, 2024
7dd7452
standardize all form submission backend validation to have format 'An…
ArushC Mar 20, 2024
8244232
missed one
ArushC Mar 21, 2024
63b3a91
changed tests to reflect more informative error messages
ArushC Mar 21, 2024
1bab7b5
added two more tests
ArushC Mar 21, 2024
4f4d828
rubocop fix
ArushC Mar 21, 2024
f978204
fixed cucumber test bugs
ArushC Mar 21, 2024
8b96209
Merge branch 'beautyjoy:main' into 187216522-international-schools
cycomachead Apr 5, 2024
2ded341
Merge branch 'main' into 187216522-international-schools
cycomachead Apr 8, 2024
ef2d3c4
Removed empty lines
razztech Apr 8, 2024
ff543d6
Update email_template.feature
ArushC Apr 12, 2024
2e1c1d1
Update schools_controller.rb
ArushC Apr 12, 2024
d58eda9
fix cucumber tests
ArushC Apr 12, 2024
838c4c0
change new --> edit redirect for update school failure
ArushC Apr 12, 2024
b1fe2d5
actually fixed it now
ArushC Apr 12, 2024
66eb04c
it turns out I don't know how to spell the word 'occured'
ArushC Apr 12, 2024
6e46e5c
fixed another myterious cucumber test bug
ArushC Apr 12, 2024
f9388fc
remove country from teachers table
ArushC Apr 12, 2024
8ca3872
removed one unnecessary deletion
ArushC Apr 12, 2024
05df57c
update location method to display country
ArushC Apr 12, 2024
38b2ce1
update selectize options
ArushC Apr 12, 2024
8f831eb
revert placeholder
ArushC Apr 12, 2024
fb9e877
update location method to display country
ArushC Apr 12, 2024
4cf6fbc
update selectize options
ArushC Apr 12, 2024
92e5647
Merge branch '187216522-international-schools' of github.com:cs169/BJ…
ArushC Apr 12, 2024
eeea4ca
add back teachers to table to prevent cucumber bugs
ArushC Apr 12, 2024
fa97d32
make search_list change
ArushC Apr 13, 2024
c027698
added unique school params and DRY out code
ArushC Apr 13, 2024
f41d0b9
converted valid states list using country gem
ArushC Apr 13, 2024
1f682da
fixed rubocop
ArushC Apr 13, 2024
c85070e
Merge branch 'beautyjoy:main' into 187216522-international-schools
cycomachead Apr 22, 2024
22df601
Update app/models/school.rb
cycomachead Apr 23, 2024
a2bf6e1
Update app/views/schools/_form.html.erb
cycomachead Apr 23, 2024
875b65b
Update app/views/schools/show.html.erb
cycomachead Apr 23, 2024
53fcb0b
Update app/views/teachers/_table_headers.erb
cycomachead Apr 23, 2024
eb884a9
Update app/views/teachers/_teacher.erb
cycomachead Apr 23, 2024
9207b11
Update app/views/teachers/_teacher_info.html.erb
cycomachead Apr 23, 2024
57dd25a
delint...
cycomachead Apr 23, 2024
c4ad7f5
Apply suggestions from code review
cycomachead May 10, 2024
163e2a9
move datatables stuff to new file
cycomachead May 10, 2024
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
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ gem "activerecord-import", require: false

gem "httparty", "~> 0.21.0"


gem "country_select", "~> 8.0"

group :development do
gem "annotate"

Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ GEM
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
concurrent-ruby (1.2.3)
countries (5.7.2)
unaccent (~> 0.3)
country_select (8.0.3)
countries (~> 5.0)
crass (1.0.6)
cucumber (7.1.0)
builder (~> 3.2, >= 3.2.4)
Expand Down Expand Up @@ -528,6 +532,7 @@ GEM
concurrent-ruby (~> 1.0)
tzinfo-data (1.2021.5)
tzinfo (>= 1.0.0)
unaccent (0.4.0)
unicode-display_width (2.4.2)
version_gem (1.1.3)
virtus (2.0.0)
Expand Down Expand Up @@ -563,6 +568,7 @@ DEPENDENCIES
axe-core-cucumber
axe-core-rspec
bootsnap (>= 1.4.4)
country_select (~> 8.0)
cucumber-rails
database_cleaner
debug
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] = "Failed to save #{@email_template.title} template: #{@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] = "Failed to submit information: #{@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: 2 additions & 2 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def create
flash[:success] = "Created #{@page.title} page successfully."
redirect_to action: "show", url_slug: @page.url_slug
else
flash.now[:alert] = "An error occurred! #{@page.errors.full_messages}"
flash.now[:alert] = "An error occurred: #{@page.errors.full_messages.join(", ")}"
render "edit"
end
end
Expand Down Expand Up @@ -58,7 +58,7 @@ def update
flash[:success] = "Updated #{@page.title} page successfully."
redirect_to pages_path
else
flash.now[:alert] = "An error occurred! #{@page.errors.full_messages.to_sentence}"
flash.now[:alert] = "An error occurred: #{@page.errors.full_messages.join(', ')}"
render "edit"
end
end
Expand Down
16 changes: 7 additions & 9 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.city}, #{school.state}", school.name] }
School.search_list
end

def create
@school = School.find_by(name: school_params[:name], city: school_params[:city], state: school_params[:state])
@school = School.find_by(**unique_school_params)
if @school
@school.assign_attributes(school_params)
else
Expand All @@ -27,7 +28,7 @@ def create
flash[:success] = "Created #{@school.name} successfully."
redirect_to schools_path
else
flash[:alert] = "Failed to submit information :("
flash[:alert] = "An error occurred: #{@school.errors.full_messages.join(', ')}"
render "new"
end
end
Expand All @@ -45,10 +46,11 @@ def update
@school = School.find(params[:id])
@school.assign_attributes(school_params)
if @school.save
flash[:success] = "Update #{@school.name} successfully."
flash[:success] = "Updated #{@school.name} successfully."
redirect_to school_path(@school)
else
render "edit", alert: "Failed to submit information :("
flash[:alert] = "An error occurred: #{@school.errors.full_messages.join(', ')}"
render "edit"
end
end

Expand All @@ -63,10 +65,6 @@ def destroy
end

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

def load_ordered_schools
@ordered_schools ||= School.all.order(:name)
end
Expand Down
22 changes: 11 additions & 11 deletions app/controllers/teachers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "activerecord-import"

class TeachersController < ApplicationController
include SchoolParams
include CsvProcess

before_action :load_pages, only: [:new, :create, :edit, :update]
Expand Down Expand Up @@ -63,9 +64,10 @@ def create

load_school
if @school.new_record?
return unless params[:school]
@school = School.new(school_params)
unless @school.save
flash[:alert] = "An error occurred! #{@school.errors.full_messages}"
flash[:alert] = "An error occurred: #{@school.errors.full_messages.join(', ')}"
render "new" && return
end
end
Expand All @@ -81,7 +83,7 @@ def create
TeacherMailer.teacher_form_submission(@teacher).deliver_now
redirect_to root_path
else
redirect_to new_teacher_path, alert: "An error occurred while trying to save. #{@teacher.errors.full_messages}"
redirect_to new_teacher_path, alert: "An error occurred: #{@teacher.errors.full_messages.join(', ')}"
end
end

Expand All @@ -100,7 +102,10 @@ def update
@teacher.school = @school
else
@school.update(school_params) if school_params
@school.save!
unless @school.save
flash[:alert] = "An error occurred: #{@school.errors.full_messages.join(', ')}"
render "edit" && return
end
@teacher.school = @school
end
send_email_if_application_status_changed_and_email_resend_enabled
Expand All @@ -114,15 +119,15 @@ def update
end
if [email protected]
redirect_to edit_teacher_path(current_user.id),
alert: "Failed to update data. #{@teacher.errors.full_messages.to_sentence}"
alert: "An error occurred: #{@teacher.errors.full_messages.join(', ')}"
return
end
if [email protected]? && !current_user.admin?
TeacherMailer.form_submission(@teacher).deliver_now
TeacherMailer.teacher_form_submission(@teacher).deliver_now
end
if is_admin?
redirect_to teachers_path, notice: "Saved #{@teacher.full_name}"
redirect_to edit_teacher_path(current_user.id), notice: "Saved #{@teacher.full_name}"
return
else
@teacher.try_append_ip(request.remote_ip)
Expand Down Expand Up @@ -202,7 +207,7 @@ def load_school
if teacher_params[:school_id].present?
@school ||= School.find(teacher_params[:school_id])
end
@school ||= School.find_or_create_by(name: school_params[:name], city: school_params[:city], state: school_params[:state])
@school ||= School.find_or_create_by(**unique_school_params)
end

def teacher_params
Expand All @@ -215,11 +220,6 @@ def teacher_params
params.require(:teacher).permit(*teacher_attributes)
end

def school_params
return unless params[:school]
params.require(:school).permit(:name, :city, :state, :website, :grade_level, :school_type)
end

def omniauth_data
@omniauth_data ||= session[:auth_data]&.slice("first_name", "last_name", "email")
end
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/packs/datatables.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $(function() {
$.fn.dataTable.ext.search.push((_, searchData) => {
let enabled = $('input:checkbox[name="statusFilter"]:checked').map((_i, el) => el.value).get();
// Include all rows when no checkboxes are selected.
return enabled.length === 0 || enabled.includes(searchData[6]);
return enabled.length === 0 || enabled.includes(searchData[7]);
});

let $tables = $('.js-dataTable').DataTable({
Expand Down
4 changes: 2 additions & 2 deletions app/models/email_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class EmailTemplate < ApplicationRecord
validates :title,
inclusion: TeacherMailer.instance_methods(false).map { |method| method.to_s.titlecase },
if: -> { self.required? }
validates :body, presence: { message: "cannot be blank" }
validates :to, presence: { message: "cannot be blank" }
validates :body, presence: true
ArushC marked this conversation as resolved.
Show resolved Hide resolved
validates :to, presence: true

before_destroy :prevent_deleting_required_emails

Expand Down
29 changes: 22 additions & 7 deletions app/models/school.rb
ArushC marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#
# id :integer not null, primary key
# city :string
# country :string
# grade_level :integer
# lat :float
# lng :float
Expand All @@ -25,10 +26,11 @@
#

class School < ApplicationRecord
VALID_STATES = [ "AL", "AK", "AS", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FM", "FL", "GA", "GU", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MH", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "MP", "OH", "OK", "OR", "PW", "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VI", "VA", "WA", "WV", "WI", "WY", "International"].freeze

validates :state, inclusion: { in: VALID_STATES }
validates :name, :city, :state, :website, presence: true
VALID_STATES = ISO3166::Country["US"].subdivisions.keys.freeze
validates :name, :city, :website, :country, presence: true
validates :country, inclusion: { in: ISO3166::Country.all.map(&:alpha2), message: "%{value} is not a valid country" }
validates :state, presence: true, if: -> { country == "US" }
validates :state, inclusion: { in: VALID_STATES }, if: -> { country == "US" }
validates_format_of :website, with: /.+\..+/, on: :create

before_save :update_gps_data, if: -> { lat.nil? || lng.nil? }
Expand Down Expand Up @@ -58,7 +60,8 @@ def website
end

def location
"#{city}, #{state}"
country_text = country == "US" ? "" : ", #{country}"
"#{city}, #{state}#{country_text}"
end

# TODO: Consider renaming this.
Expand All @@ -75,11 +78,11 @@ def display_grade_level
end

def selectize_options
[name_location, to_json(only: [:id, :name, :city, :state, :website]) ]
[name_location, to_json(only: [:id, :name, :city, :state, :country, :website]) ]
end

def name_location
"#{name} (#{city}, #{state})"
"#{name} (#{city}, #{state}, #{country})"
end

def update_gps_data
Expand All @@ -98,6 +101,18 @@ def maps_marker_data
}
end

def format_school(data)
name, city, state, country = data
country_str = country == "US" ? "" : ", #{country}"
"#{name} (#{city}, #{state}#{country_str})"
end

def self.search_list
School.pluck(:name, :city, :state, :country).map do |data|
format_school(data)
end
cycomachead marked this conversation as resolved.
Show resolved Hide resolved
end

def self.grade_level_options
School.grade_levels.map { |key, _val| [key.to_s.titlecase, key] }
end
Expand Down
59 changes: 51 additions & 8 deletions app/views/schools/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
<div class="form-group h3">Create a new School</div>
<div class='form-group row'>
<div class='col-6'>
<div class='col'>
<%= f.label :name, "School Name", class: "label-required" %>
<%= f.text_field :name, placeholder: 'UC Berkeley', class: 'form-control',
required: false, id: 'school_name' %>
</div>

</div>
<div class='form-group row'>
<div class='col-4'>
<%= f.label :city, class: "label-required", for: "school_city" %>
<%= f.text_field :city, placeholder: 'Berkeley', class: 'form-control',
required: false, id: 'school_city' %>
</div>

<div class='col-2'>
<div class='col-4' id="state_select_container">
<%= f.label :state, class: "label-required", for: "school_state" %>
<%= f.select(
:state,
School::VALID_STATES,
{ include_blank: "State" },
{ class: 'form-control', required: false, id: 'school_state'}
<%= f.select :state, School::VALID_STATES, { include_blank: "Select a state" }, { id: "state_select", class: 'form-control' } %>
</div>

<div class='col-4' id="state_textfield_container">
<%= f.label :state, for: "school_state" %>
<%= f.text_field :state, placeholder: "Name of State", class: 'form-control', id: "state_textfield" %>
</div>

<div class='col-4'>
<%= f.label :country, "Country", class: "label-required", for: "school_country" %>
<%= f.country_select(
:country,
{ priority_countries: ['United States'], include_blank: "Select a country" },
{ class: 'form-control', required: false, id: 'school_country', format: :with_full_country_name, selected: 'United States'}
) %>
</div>

</div>

<div class="form-group">
Expand Down Expand Up @@ -88,3 +99,35 @@
class: "form-control", title: "Please enter a valid NCES ID.", id: "school_nces_id" %>
</div>
<%- end %>

<script>
$(document).ready(function() {
const countrySelected = $('#school_country');
const stateSelectContainer = $('#state_select_container');
const stateTextfieldContainer = $('#state_textfield_container');
const stateSelect = $('#state_select');
const stateTextfield = $('#state_textfield');

function handleCountryChange() {
if (countrySelected.val() === 'US') {
stateSelectContainer.show();
ArushC marked this conversation as resolved.
Show resolved Hide resolved
stateSelect.attr('required', '') //make state select required
stateTextfieldContainer.hide();

stateTextfield.removeAttr('name');
stateSelect.attr('name', 'school[state]');

ArushC marked this conversation as resolved.
Show resolved Hide resolved
} else {
stateTextfieldContainer.show();
stateSelect.removeAttr('required')
stateSelectContainer.hide();


stateSelect.removeAttr('name');
stateTextfield.attr('name', 'school[state]');
}
}
countrySelected.change(handleCountryChange);
handleCountryChange();
});
</script>
Loading
Loading