diff --git a/app/presenters/analyze/graph/students_by_gender.rb b/app/presenters/analyze/graph/students_by_gender.rb index 6e12cdc8..8e3fb50a 100644 --- a/app/presenters/analyze/graph/students_by_gender.rb +++ b/app/presenters/analyze/graph/students_by_gender.rb @@ -9,11 +9,11 @@ module Analyze end def to_s - 'Students by Gender' + "Students by Gender" end def slug - 'students-by-gender' + "students-by-gender" end def columns @@ -21,6 +21,7 @@ module Analyze genders.each do |gender| array << column_for_gender_code(code: gender.qualtrics_code) end + array.sort_by!(&:to_s) array << Analyze::Graph::Column::AllStudent end end diff --git a/app/presenters/analyze/presenter.rb b/app/presenters/analyze/presenter.rb index 418bc4dd..9e5492d0 100644 --- a/app/presenters/analyze/presenter.rb +++ b/app/presenters/analyze/presenter.rb @@ -79,12 +79,12 @@ module Analyze gender_params = params[:genders] return genders unless gender_params - gender_params.split(",").map { |gender| Gender.find_by_designation(gender) }.compact + gender_params.split(",").sort.map { |gender| Gender.find_by_designation(gender) }.compact end end def genders - @genders ||= Gender.all + @genders ||= Gender.all.order(designation: :ASC) end def groups diff --git a/spec/presenters/analyze/presenter_spec.rb b/spec/presenters/analyze/presenter_spec.rb index a6d79241..2c8d6984 100644 --- a/spec/presenters/analyze/presenter_spec.rb +++ b/spec/presenters/analyze/presenter_spec.rb @@ -1,77 +1,77 @@ -require 'rails_helper' +require "rails_helper" describe Analyze::Presenter do - let(:category) { create(:category, category_id: '1', name: 'first category') } - let(:category_2) { create(:category, category_id: '2', name: 'second category') } - let(:wrong_category) { create(:category, category_id: '999', name: 'wrong category') } - let(:subcategory) { create(:subcategory, subcategory_id: '1A', name: 'first subcategory', category:) } - let(:subcategory_2) { create(:subcategory, subcategory_id: '2A', name: 'second subcategory', category: category_2) } + let(:category) { create(:category, category_id: "1", name: "first category") } + let(:category_2) { create(:category, category_id: "2", name: "second category") } + let(:wrong_category) { create(:category, category_id: "999", name: "wrong category") } + let(:subcategory) { create(:subcategory, subcategory_id: "1A", name: "first subcategory", category:) } + let(:subcategory_2) { create(:subcategory, subcategory_id: "2A", name: "second subcategory", category: category_2) } let(:wrong_subcategory) do - create(:subcategory, subcategory_id: '99A', name: 'wrong subcategory', category: wrong_category) + create(:subcategory, subcategory_id: "99A", name: "wrong subcategory", category: wrong_category) end - let(:measure) { create(:measure, measure_id: '1A-i', name: 'first measure', subcategory:) } - let(:measure_2) { create(:measure, measure_id: '2A-i', name: 'second measure', subcategory: subcategory_2) } + let(:measure) { create(:measure, measure_id: "1A-i", name: "first measure", subcategory:) } + let(:measure_2) { create(:measure, measure_id: "2A-i", name: "second measure", subcategory: subcategory_2) } let(:wrong_measure) do - create(:wrong_measure, measure_id: '99A', name: 'wrong measure', subcategory: wrong_subcategory) + create(:wrong_measure, measure_id: "99A", name: "wrong measure", subcategory: wrong_subcategory) end let(:scale) { create(:student_scale, measure:) } let(:survey_item) { create(:student_survey_item, scale:) } let(:school) { create(:school) } let(:academic_year) { create(:academic_year) } - let(:ay_2021_22) { create(:academic_year, range: '2021-22') } - let(:ay_2022_23) { create(:academic_year, range: '2022-23') } - let(:wrong_ay) { create(:academic_year, range: '9999-99') } - let(:black) { create(:race, designation: 'black', qualtrics_code: 1) } - let(:white) { create(:race, designation: 'white', qualtrics_code: 2) } - let(:wrong_race) { create(:race, designation: 'wrong race', qualtrics_code: 9999) } - let(:female) { create(:gender, designation: 'female', qualtrics_code: 1) } - let(:male) { create(:gender, designation: 'male', qualtrics_code: 2) } - let(:wrong_gender) { create(:gender, designation: 'wrong_gender', qualtrics_code: 2) } - - context '.category' do + let(:ay_2021_22) { create(:academic_year, range: "2021-22") } + let(:ay_2022_23) { create(:academic_year, range: "2022-23") } + let(:wrong_ay) { create(:academic_year, range: "9999-99") } + let(:black) { create(:race, designation: "black", qualtrics_code: 1) } + let(:white) { create(:race, designation: "white", qualtrics_code: 2) } + let(:wrong_race) { create(:race, designation: "wrong race", qualtrics_code: 9999) } + let(:female) { create(:gender, designation: "female", qualtrics_code: 1) } + let(:male) { create(:gender, designation: "male", qualtrics_code: 2) } + let(:wrong_gender) { create(:gender, designation: "wrong_gender", qualtrics_code: 2) } + + context ".category" do before :each do category category_2 wrong_category end - context 'when a category is provided in the params hash' do - it 'returns the category with the given id' do - params = { category: '1' } + context "when a category is provided in the params hash" do + it "returns the category with the given id" do + params = { category: "1" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.category).to eq category - params = { category: '2' } + params = { category: "2" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.category).to eq category_2 end end - context 'when no category is provided in the params hash' do - it 'returns the first category' do + context "when no category is provided in the params hash" do + it "returns the first category" do params = {} presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.category).to eq category end end - context 'when a category that doesnt exist in the database is passed' do - it 'returns the first category' do - params = { category: '4' } + context "when a category that doesnt exist in the database is passed" do + it "returns the first category" do + params = { category: "4" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.category).to eq category end end end - context '.categories' do - it 'returns all categories' do + context ".categories" do + it "returns all categories" do params = {} presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.categories).to eq Category.all.order(:category_id) end end - context '.subcategory' do + context ".subcategory" do before :each do category category_2 @@ -80,203 +80,203 @@ describe Analyze::Presenter do subcategory_2 wrong_subcategory end - context 'when a subcategory is provided in the params hash' do - it 'returns the subcategory with the given id' do - params = { subcategory: '1A' } + context "when a subcategory is provided in the params hash" do + it "returns the subcategory with the given id" do + params = { subcategory: "1A" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.subcategory).to eq subcategory - params = { subcategory: '2A' } + params = { subcategory: "2A" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.subcategory).to eq subcategory_2 end end - context 'when no subcategory is provided in the params hash' do - it 'returns the first subcategory' do + context "when no subcategory is provided in the params hash" do + it "returns the first subcategory" do params = {} presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.subcategory).to eq subcategory end end - context 'when a subcategory that doesnt exist in the database is passed' do - it 'returns the first subcategory' do - params = { subcategory: '4A' } + context "when a subcategory that doesnt exist in the database is passed" do + it "returns the first subcategory" do + params = { subcategory: "4A" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.subcategory).to eq subcategory end end end - context '.subcategories' do + context ".subcategories" do before :each do category wrong_category subcategory wrong_subcategory end - it 'returns all subcategories for a given category' do - params = { category: '1' } + it "returns all subcategories for a given category" do + params = { category: "1" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.subcategories).to eq category.subcategories.all.order(:subcategory_id) end end - context '.measures' do + context ".measures" do before :each do category subcategory measure end - it 'returns all measures for a given subcategory' do - params = { category: '1' } + it "returns all measures for a given subcategory" do + params = { category: "1" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.measures).to eq category.subcategories.flat_map(&:measures) end end - context '.academic_years' do + context ".academic_years" do before :each do ay_2021_22 ay_2022_23 end - it 'returns all academic years' do + it "returns all academic years" do params = {} presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.academic_years).to eq AcademicYear.all.order(:range) end end - context '.selected_academic_years' do + context ".selected_academic_years" do before :each do ay_2021_22 ay_2022_23 wrong_ay end - context 'when no academic years are provided in the params hash' do - it 'returns an empty array if no params are provided' do + context "when no academic years are provided in the params hash" do + it "returns an empty array if no params are provided" do params = {} presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.selected_academic_years).to eq [] end end - context 'when a single academic year is provided in the params hash' do - it 'returns the academic year with the given id' do - params = { academic_years: '2021-22' } + context "when a single academic year is provided in the params hash" do + it "returns the academic year with the given id" do + params = { academic_years: "2021-22" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.selected_academic_years).to eq [AcademicYear.find_by_range('2021-22')] + expect(presenter.selected_academic_years).to eq [AcademicYear.find_by_range("2021-22")] - params = { academic_years: '2022-23' } + params = { academic_years: "2022-23" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.selected_academic_years).to eq [AcademicYear.find_by_range('2022-23')] + expect(presenter.selected_academic_years).to eq [AcademicYear.find_by_range("2022-23")] end end - context 'when multiple academic years are provided in the params hash' do - it 'returns the academic year with the given ids' do - params = { academic_years: '2021-22,2022-23' } + context "when multiple academic years are provided in the params hash" do + it "returns the academic year with the given ids" do + params = { academic_years: "2021-22,2022-23" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.selected_academic_years).to eq [AcademicYear.find_by_range('2021-22'), - AcademicYear.find_by_range('2022-23')] + expect(presenter.selected_academic_years).to eq [AcademicYear.find_by_range("2021-22"), + AcademicYear.find_by_range("2022-23")] end end end - context '.races' do + context ".races" do before :each do black white wrong_race end - it 'returns all races' do + it "returns all races" do params = {} presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.races).to eq Race.all.order(designation: :ASC) end end - context '.selected_races' do + context ".selected_races" do before :each do black white wrong_race end - context 'when no races are provided in the params hash' do - it 'returns an array with all races if no params are provided' do + context "when no races are provided in the params hash" do + it "returns an array with all races if no params are provided" do params = {} presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.selected_races).to eq presenter.races end end - context 'when one race is provided in the params hash' do - it 'returns a single race with the given slug' do - params = { races: 'white' } + context "when one race is provided in the params hash" do + it "returns a single race with the given slug" do + params = { races: "white" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.selected_races).to eq [Race.find_by_slug('white')] + expect(presenter.selected_races).to eq [Race.find_by_slug("white")] - params = { races: 'black' } + params = { races: "black" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.selected_races).to eq [Race.find_by_slug('black')] + expect(presenter.selected_races).to eq [Race.find_by_slug("black")] end end - context 'when multiple races are provided in the params hash' do - it 'returns multiple races with the given slugs' do - params = { races: 'white,black' } + context "when multiple races are provided in the params hash" do + it "returns multiple races with the given slugs" do + params = { races: "white,black" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.selected_races).to eq [Race.find_by_slug('white'), Race.find_by_slug('black')] + expect(presenter.selected_races).to eq [Race.find_by_slug("white"), Race.find_by_slug("black")] end end end - context '.graph' do - context 'when no params are provided' do - it 'returns the default(first) graph object' do + context ".graph" do + context "when no params are provided" do + it "returns the default(first) graph object" do params = {} presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.graph.to_s).to eq Analyze::Graph::AllData.new.to_s end end - context 'when a graph is provided in the params hash' do - it 'returns the graph object with the given slug' do - params = { graph: 'all_data' } + context "when a graph is provided in the params hash" do + it "returns the graph object with the given slug" do + params = { graph: "all_data" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.graph.to_s).to eq Analyze::Graph::AllData.new.to_s - params = { graph: 'students-and-teachers' } + params = { graph: "students-and-teachers" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.graph.to_s).to eq Analyze::Graph::StudentsAndTeachers.new.to_s - params = { graph: 'students-by-race' } + params = { graph: "students-by-race" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.graph.to_s).to eq Analyze::Graph::StudentsByRace.new(races: nil).to_s - params = { graph: 'students-by-grade' } + params = { graph: "students-by-grade" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.graph.to_s).to eq Analyze::Graph::StudentsByGrade.new(grades: nil).to_s - params = { graph: 'students-by-gender' } + params = { graph: "students-by-gender" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.graph.to_s).to eq Analyze::Graph::StudentsByGender.new(genders: nil).to_s end end - context 'when a parameter that does not match a graph is provided' do - it 'returns the default(first) graph object' do - params = { graph: 'invalid parameter' } + context "when a parameter that does not match a graph is provided" do + it "returns the default(first) graph object" do + params = { graph: "invalid parameter" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.graph.to_s).to eq Analyze::Graph::AllData.new.to_s end end end - context '.grades' do + context ".grades" do before :each do school academic_year @@ -286,8 +286,8 @@ describe Analyze::Presenter do create_list(:survey_item_response, 20, school:, academic_year:, grade: 4, survey_item:) end - context 'when no grades are provided in the params hash' do - it 'returns the set of grades for which there are more than ten responses' do + context "when no grades are provided in the params hash" do + it "returns the set of grades for which there are more than ten responses" do params = {} presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.grades).to eq [1, 2, 3, 4] @@ -295,7 +295,7 @@ describe Analyze::Presenter do end end - context '.selected_grades' do + context ".selected_grades" do before :each do school academic_year @@ -305,86 +305,86 @@ describe Analyze::Presenter do create_list(:survey_item_response, 20, school:, academic_year:, grade: 4, survey_item:) end - context 'when no grades are provided in the params hash' do - it 'returns only the set of grades selected even if other grades have sufficient responses' do - params = { grades: '1,2,3' } + context "when no grades are provided in the params hash" do + it "returns only the set of grades selected even if other grades have sufficient responses" do + params = { grades: "1,2,3" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.selected_grades).to eq [1, 2, 3] end end end - context '.selected_genders' do + context ".selected_genders" do before :each do male female wrong_gender end - context 'when no parameters are provided' do - it 'returns all genders' do + context "when no parameters are provided" do + it "returns all genders" do params = {} presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.selected_genders).to eq Gender.all + expect(presenter.selected_genders).to eq Gender.all.order(designation: :ASC) end end - context 'when a single gender is provided in the params hash' do - it 'returns the gender with the given designation' do - params = { genders: 'female' } + context "when a single gender is provided in the params hash" do + it "returns the gender with the given designation" do + params = { genders: "female" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.selected_genders).to eq [Gender.find_by_designation('female')] + expect(presenter.selected_genders).to eq [Gender.find_by_designation("female")] - params = { genders: 'male' } + params = { genders: "male" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.selected_genders).to eq [Gender.find_by_designation('male')] + expect(presenter.selected_genders).to eq [Gender.find_by_designation("male")] end end - context 'when multiple genders are provided in the params hash' do - it 'returns multilple genders with the given designations' do - params = { genders: 'female,male' } + context "when multiple genders are provided in the params hash" do + it "returns multilple genders with the given designations" do + params = { genders: "female,male" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.selected_genders).to eq [Gender.find_by_designation('female'), - Gender.find_by_designation('male')] + expect(presenter.selected_genders).to eq [Gender.find_by_designation("female"), + Gender.find_by_designation("male")] end end end - context '.genders' do + context ".genders" do before :each do female male end - it 'returns all genders' do + it "returns all genders" do params = {} presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.genders).to eq Gender.all + expect(presenter.genders).to eq Gender.all.order(designation: :ASC) end end - context '.group' do - context 'when no parameters are provided' do - it 'returns the first item in the list of groups' do + context ".group" do + context "when no parameters are provided" do + it "returns the first item in the list of groups" do params = {} presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.group.slug).to eq presenter.groups.first.slug end end - context 'when a group is provided in the params hash' do - it 'returns the group with the given slug' do - params = { group: 'gender' } + context "when a group is provided in the params hash" do + it "returns the group with the given slug" do + params = { group: "gender" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.group.slug).to eq 'gender' + expect(presenter.group.slug).to eq "gender" - params = { group: 'grade' } + params = { group: "grade" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.group.slug).to eq 'grade' + expect(presenter.group.slug).to eq "grade" - params = { group: 'race' } + params = { group: "race" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.group.slug).to eq 'race' + expect(presenter.group.slug).to eq "race" # Not yet implemented # params = { group: 'income' } @@ -393,75 +393,75 @@ describe Analyze::Presenter do end end - context 'when a parameter that does not match a group is provided' do - it 'returns the first item in the list of groups' do - params = { group: 'invalid group' } + context "when a parameter that does not match a group is provided" do + it "returns the first item in the list of groups" do + params = { group: "invalid group" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) expect(presenter.group.slug).to eq presenter.groups.first.slug end end end - context '.slice' do - context 'when no parameters are provided' do - it 'returns the first item in the list of slices' do + context ".slice" do + context "when no parameters are provided" do + it "returns the first item in the list of slices" do params = {} presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.slice.slug).to eq 'all-data' + expect(presenter.slice.slug).to eq "all-data" end end - context 'when a slice is provided in the params hash' do - it 'returns the slice with the given slug' do - params = { source: 'survey-data-only', slice: 'students-and-teachers' } + context "when a slice is provided in the params hash" do + it "returns the slice with the given slug" do + params = { source: "survey-data-only", slice: "students-and-teachers" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.slice.slug).to eq 'students-and-teachers' + expect(presenter.slice.slug).to eq "students-and-teachers" end end - context 'when a slice is provided but the source is left blank ' do - it 'returns the slice from the default source (all-data)' do - params = { slice: 'students-and-teachers' } + context "when a slice is provided but the source is left blank " do + it "returns the slice from the default source (all-data)" do + params = { slice: "students-and-teachers" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.slice.slug).to eq 'all-data' + expect(presenter.slice.slug).to eq "all-data" end end - context 'when a parameter that does not match a slice is provided' do - it 'it returns the first slice from the chosen source' do - params = { source: 'survey-data-only', slice: 'invalid-slice' } + context "when a parameter that does not match a slice is provided" do + it "it returns the first slice from the chosen source" do + params = { source: "survey-data-only", slice: "invalid-slice" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.slice.slug).to eq 'students-and-teachers' + expect(presenter.slice.slug).to eq "students-and-teachers" end end end - context '.source' do - context 'when no parameters are provided' do - it 'returns the first item in the list of sources' do + context ".source" do + context "when no parameters are provided" do + it "returns the first item in the list of sources" do params = {} presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.slice.slug).to eq 'all-data' + expect(presenter.slice.slug).to eq "all-data" end end - context 'when a source is provided in the params hash' do - it 'returns the source with the given slug' do - params = { source: 'all-data' } + context "when a source is provided in the params hash" do + it "returns the source with the given slug" do + params = { source: "all-data" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.source.slug).to eq 'all-data' + expect(presenter.source.slug).to eq "all-data" - params = { source: 'survey-data-only' } + params = { source: "survey-data-only" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.source.slug).to eq 'survey-data-only' + expect(presenter.source.slug).to eq "survey-data-only" end end - context 'when a parameter that does not match a source is provided' do - it 'returns the first item in the list of sources' do - params = { source: 'invalid-source' } + context "when a parameter that does not match a source is provided" do + it "returns the first item in the list of sources" do + params = { source: "invalid-source" } presenter = Analyze::Presenter.new(params:, school:, academic_year:) - expect(presenter.slice.slug).to eq 'all-data' + expect(presenter.slice.slug).to eq "all-data" end end end