Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into cleanup-schools-bug-fix
Browse files Browse the repository at this point in the history
include the new tests
  • Loading branch information
ArushC committed Apr 5, 2024
2 parents 8a049fe + 0480057 commit 809ce23
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/controllers/teachers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def teacher_params
teacher_attributes << [:personal_email, :application_status,
:request_reason, :skip_email]
end
params.require(:teacher).permit(*teacher_attributes)
params.require(:teacher).permit(*teacher_attributes, languages: [])
end

def school_params
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/packs/schools.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ let create_school = function (input, callback) {
$("#school_form").show();
toggle_required(['name', 'city', 'state', 'website'], true);
$(".btn-primary").show();
let oringial_school_id = $('#teacher_school_id').val();
let original_school_id = $('#teacher_school_id').val();
var reset_button = $("#close_button");
var name_input = $("#school_name");
// Unset the existing saved school id.
Expand All @@ -38,7 +38,7 @@ let create_school = function (input, callback) {
if (selectizeCallback != null) {
selectizeCallback();
selectizeCallback = null;
$('#teacher_school_id').val(oringial_school_id);
$('#teacher_school_id').val(original_school_id);
}
toggle_required(['name', 'city', 'state', 'website'], true);
$("#school_form").hide();
Expand Down
23 changes: 23 additions & 0 deletions app/models/teacher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# email :string
# first_name :string
# ip_history :inet default([]), is an Array
# languages :string default(["\"English\""]), is an Array
# last_name :string
# last_session_at :datetime
# more_info :string
Expand All @@ -37,10 +38,14 @@
# fk_rails_... (school_id => schools.id)
#
class Teacher < ApplicationRecord
WORLD_LANGUAGES = [ "Afrikaans", "Albanian", "Arabic", "Armenian", "Basque", "Bengali", "Bulgarian", "Catalan", "Cambodian", "Chinese (Mandarin)", "Croatian", "Czech", "Danish", "Dutch", "English", "Estonian", "Fiji", "Finnish", "French", "Georgian", "German", "Greek", "Gujarati", "Hebrew", "Hindi", "Hungarian", "Icelandic", "Indonesian", "Irish", "Italian", "Japanese", "Javanese", "Korean", "Latin", "Latvian", "Lithuanian", "Macedonian", "Malay", "Malayalam", "Maltese", "Maori", "Marathi", "Mongolian", "Nepali", "Norwegian", "Persian", "Polish", "Portuguese", "Punjabi", "Quechua", "Romanian", "Russian", "Samoan", "Serbian", "Slovak", "Slovenian", "Spanish", "Swahili", "Swedish ", "Tamil", "Tatar", "Telugu", "Thai", "Tibetan", "Tonga", "Turkish", "Ukrainian", "Urdu", "Uzbek", "Vietnamese", "Welsh", "Xhosa" ].freeze

validates :first_name, :last_name, :email, :status, presence: true
validates :email, uniqueness: true
validates :personal_email, uniqueness: true, if: -> { personal_email.present? }
validate :ensure_unique_personal_email, if: -> { email_changed? || personal_email_changed? }
validate :valid_languages
before_validation :sort_and_clean_languages

enum application_status: {
validated: "Validated",
Expand Down Expand Up @@ -163,6 +168,24 @@ def self.education_level_options
Teacher.education_levels.map { |sym, val| [sym.to_s.titlecase, val] }
end

def self.language_options
WORLD_LANGUAGES
end

def display_languages
languages.join(", ")
end

def valid_languages
!languages.empty? && languages.all? { |value| WORLD_LANGUAGES.include?(value) }
end

def sort_and_clean_languages
# Due to an identified bug in the Selectize plugin, an empty string is occasionally appended to the 'languages' list.
# To ensure data integrity, the following code removes any occurrences of empty strings from the list.
languages.sort!.reject!(&:blank?)
end

def display_education_level
if education_level_before_type_cast.to_i == -1
"?"
Expand Down
23 changes: 23 additions & 0 deletions app/views/teachers/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,23 @@ status ONLY IF the person viewing this page is an admin. %>
</div>
</div>

<div class='form-group row'>
<div class='col-12'>
<%= f.label :languages, "What language(s) are spoken in the classroom?",
class: "label-required" %>
<%= f.select(
:languages,
options_for_select(Teacher.language_options, @teacher.languages),
{},
multiple: true,
include_blank: "Select an option",
class: 'selectize', required: true
) %>
</div>
</div>



<div class='form-group row'>
<div class='col-12'>
<%= f.label :more_info, "More Information", class: "label-required" %>
Expand All @@ -143,6 +160,12 @@ status ONLY IF the person viewing this page is an admin. %>
<script>

$(document).ready(function() {
$('.selectize').selectize({
plugins: ['remove_button'],
delimiter: ",",
persist: false,
create: false
});
//on loading the page, immediately update the reason field depending on
//the value of the application status
updateReasonField();
Expand Down
6 changes: 6 additions & 0 deletions app/views/teachers/_teacher_info.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<%= teacher.display_education_level %>
</div>
</div>
<div class="row mb-4">
<div class="col-sm-4 font-weight-bold">Classroom Language(s):</div>
<div class="col-sm-8">
<%= teacher.display_languages %>
</div>
</div>
<div class="row mb-4">
<div class="col-sm-4 font-weight-bold">Current Status:</div>
<div class="col-sm-8">
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240314160959_add_languages_to_teachers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddLanguagesToTeachers < ActiveRecord::Migration[6.1]
def change
add_column :teachers, :languages, :string, array: true, default: ['English']
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_03_07_225738) do
ActiveRecord::Schema.define(version: 2024_03_14_160959) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -118,6 +118,7 @@
t.inet "ip_history", default: [], array: true
t.integer "session_count", default: 0
t.string "personal_email"
t.string "languages", default: ["English"], array: true
t.index ["email", "first_name"], name: "index_teachers_on_email_and_first_name"
t.index ["email", "personal_email"], name: "index_teachers_on_email_and_personal_email", unique: true
t.index ["email"], name: "index_teachers_on_email", unique: true
Expand Down
35 changes: 35 additions & 0 deletions features/admin.feature
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,41 @@ Feature: basic admin functionality
And I press "Update"
Then I see a confirmation "Saved"

Scenario: Teacher info displays alphabetically ordered comma separated classroom languages
Given the following schools exist:
| name | country | city | state | website |
| UC Berkeley | US | Berkeley | CA | https://www.berkeley.edu |
Given the following teachers exist:
| first_name | last_name | admin | email | school | languages |
| Joseph | Mamoa | false | testteacher@berkeley.edu | UC Berkeley | - English\n- French\n- Spanish |
Given I am on the BJC home page
And I have an admin email
And I follow "Log In"
Then I can log in with Google
When I go to the show page for Joseph Mamoa
Then I should see "English, French, Spanish"

Scenario: Adding/removing classroom languages in arbitrary order still displays alphabetically sorted
Given the following schools exist:
| name | country | city | state | website |
| UC Berkeley | US | Berkeley | CA | https://www.berkeley.edu |
Given the following teachers exist:
| first_name | last_name | admin | email | school | languages |
| Joseph | Mamoa | false | testteacher@berkeley.edu | UC Berkeley | - English\n- Hindi |
Given I am on the BJC home page
And I have an admin email
And I follow "Log In"
Then I can log in with Google
When I go to the edit page for Joseph Mamoa
And I select "Spanish" from the languages dropdown
And I select "French" from the languages dropdown
And I select "German" from the languages dropdown
And I remove "English" from the languages dropdown
And I remove "Hindi" from the languages dropdown
And I press "Update"
And I go to the show page for Joseph Mamoa
Then I should see "French, German, Spanish"

Scenario: Changing application status as admin sends emails
Given the following schools exist:
| name | country | city | state | website | grade_level | school_type |
Expand Down
22 changes: 22 additions & 0 deletions features/step_definitions/form_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@
select(input, from: "application_status_select_value")
end

# assumes that languages dropdown is the FIRST selectize menu to appear on the page
When(/^I select "(.*?)" from the languages dropdown$/) do |option|
first(".selectize-input").click # Click on the dropdown to open it
find(".selectize-dropdown-content .option", text: option).click # Click on the desired option
end

# also assumes that languages dropdown is the FIRST selectize menu to appear on the page
When(/^I remove "(.*?)" from the languages dropdown$/) do |item|
first(".selectize-input .item", text: item).find(".remove").click
end

Then(/^the languages dropdown should have the option "(.*?)" selected$/) do |selected_option|
# Find the Selectize dropdown by its CSS class
selectize_dropdown = first(".selectize-input")

# Find the selected option within the dropdown
selected_option_element = selectize_dropdown.find(".item", text: selected_option)

# Assert that the selected option exists
expect(selected_option_element).to be_visible
end

Given(/^I set my request reason as "(.*)"$/) do |input|
fill_in("request_reason", with: input)
end
Expand Down
12 changes: 10 additions & 2 deletions features/step_definitions/teacher_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,20 @@
more_info: "I'm teaching a college course",
admin: false,
personal_website: "https://snap.berkeley.edu",
application_status: "Not Reviewed"
application_status: "Not Reviewed",
languages: ["English"]
}

teachers_table.symbolic_hashes.each do |teacher|
teachers_default.each do |key, value|
teacher[key] = teacher[key].presence || value
# Parse the 'languages' field as an array of strings using YAML.safe_load
if key == :languages
languages = if teacher[key].present? then YAML.safe_load(teacher[key]) else nil end
teacher[key] = languages.presence || value
else
# Handle other fields as usual
teacher[key] = teacher[key].presence || value
end
end

school_name = teacher.delete(:school)
Expand Down
2 changes: 2 additions & 0 deletions features/teacher.feature
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ Scenario: Logging in as a teacher with Google account should be able to edit the
And I enter my "City" as "Cupertino"
And I select "CA" from "State" dropdown
And I enter my "School Website" as "https://chs.fuhsd.org"
And I select "Spanish" from the languages dropdown
And I press "Update"
Then I see a confirmation "Successfully updated your information"
Then the "First Name" field should contain "Joe"
And the languages dropdown should have the option "Spanish" selected

Scenario: Logging in as a teacher with Microsoft account should be able to edit their info
Given the following schools exist:
Expand Down
1 change: 1 addition & 0 deletions spec/factories/teachers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# email :string
# first_name :string
# ip_history :inet default([]), is an Array
# languages :string default(["\"English\""]), is an Array
# last_name :string
# last_session_at :datetime
# more_info :string
Expand Down
3 changes: 2 additions & 1 deletion spec/fixtures/teachers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# email :string
# first_name :string
# ip_history :inet default([]), is an Array
# languages :string default(["\"English\""]), is an Array
# last_name :string
# last_session_at :datetime
# more_info :string
Expand Down Expand Up @@ -109,4 +110,4 @@ barney:
application_status: Denied
school: berkeley
education_level: -1


1 change: 1 addition & 0 deletions spec/models/teacher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# email :string
# first_name :string
# ip_history :inet default([]), is an Array
# languages :string default(["\"English\""]), is an Array
# last_name :string
# last_session_at :datetime
# more_info :string
Expand Down

0 comments on commit 809ce23

Please sign in to comment.