diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 87764b5a..29197264 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -9,7 +9,7 @@ class CoursesController < ApplicationController # GET /courses # GET /courses.json def index - courses = Course.all.with_averages + courses = Course.with_averages if params[:school_id].to_i.zero? (@filterrific = initialize_filterrific( courses, diff --git a/app/controllers/schools_controller.rb b/app/controllers/schools_controller.rb index 9b0e4a3d..c3264cdb 100644 --- a/app/controllers/schools_controller.rb +++ b/app/controllers/schools_controller.rb @@ -9,13 +9,13 @@ class SchoolsController < ApplicationController # GET /schools.json def index (@filterrific = initialize_filterrific( - School.all, + School.with_averages, params[:filterrific], select_options: { sorted_by: School.options_for_sorted_by } )) || return - @schools = @filterrific.find.page(params[:page]) + @schools = @filterrific.find end # GET /schools/1 diff --git a/app/models/course.rb b/app/models/course.rb index da0e7bb2..8841e943 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -94,6 +94,7 @@ class Course < ApplicationRecord .left_joins(:reviews, :school) .group('courses.id, schools.id') } + def full_number "#{department} #{number}" end diff --git a/app/models/school.rb b/app/models/school.rb index 5d14d308..93b9faa0 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -10,6 +10,9 @@ class School < ApplicationRecord available_filters: %i[ sorted_by search_query + with_avg_rating_gte + with_avg_difficulty_lte + with_avg_work_lte ] ) @@ -47,11 +50,38 @@ class School < ApplicationRecord order(Arel.sql("LOWER(schools.short_name) #{direction}")) when /^name_/ order(Arel.sql("LOWER(schools.name) #{direction}")) + when /^avg_rating_/ + order(Arel.sql("avg_rating #{direction}")) + when /^avg_difficulty_/ + order(Arel.sql("avg_difficulty #{direction}")) + when /^avg_work_/ + order(Arel.sql("avg_work #{direction}")) else raise(ArgumentError, "Invalid sort option: #{sort_option.inspect}") end } + scope :with_avg_rating_gte, ->(ref_num) { + having('avg(reviews.rating) >= ?', ref_num) + } + + scope :with_avg_difficulty_lte, ->(ref_num) { + having('avg(reviews.difficulty) <= ?', ref_num) + } + + scope :with_avg_work_lte, ->(ref_num) { + having('avg(reviews.work_required) <= ?', ref_num) + } + + scope :with_averages, -> { + select('schools.*, + avg(reviews.rating) as avg_rating, + avg(reviews.difficulty) as avg_difficulty, + avg(reviews.work_required) as avg_work') + .left_joins(courses: [:reviews]) + .group('schools.id') + } + def self.options_for_select schools = School.arel_table order(schools[:name].lower).pluck(:name, :id) @@ -62,7 +92,10 @@ def self.options_for_sorted_by ['Name (a-z)', 'name_asc'], ['Name (z-a)', 'name_desc'], ['Short Name (a-z)', 'short_name_asc'], - ['Short Name (z-a)', 'short_name_desc'] + ['Short Name (z-a)', 'short_name_desc'], + ['Average Rating (highest first)', 'avg_rating_desc'], + ['Average Work Required (lowest first)', 'avg_work_asc'], + ['Average Difficulty (lowest first)', 'avg_difficulty_asc'] ] end end diff --git a/app/views/schools/_list.html.erb b/app/views/schools/_list.html.erb index 75a89957..22eaa305 100644 --- a/app/views/schools/_list.html.erb +++ b/app/views/schools/_list.html.erb @@ -5,7 +5,7 @@ <% if schools.load.empty? %> No schools at this time! <% else %> - <%= page_entries_info schools, class: 'align-middle' %> + Displaying <%= pluralize(schools.size, 'school') %> <% end %>