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

[DO NOT REVIEW YET] #4466

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion app/controllers/api/public/v1/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def index
private

def courses
@courses ||= CourseSearchService.call(
@courses ||= APICourseSearchService.call(
filter: params[:filter],
sort: params[:sort],
course_scope: recruitment_cycle.courses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def show
private

def courses
@courses ||= CourseSearchService.call(filter: params[:filter],
course_scope: provider.courses)
@courses ||= APICourseSearchService.call(filter: params[:filter],
course_scope: provider.courses)
end

def course
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/find/sitemaps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Find
class SitemapsController < ApplicationController
def show
@courses = ::CourseSearchService.call(filter: nil, sort: nil, course_scope: RecruitmentCycle.current.courses.findable)
@courses = ::WebCourseSearchService.call(filter: nil, sort: nil, course_scope: RecruitmentCycle.current.courses.findable)

expires_in(1.day, public: true)
end
Expand Down
14 changes: 14 additions & 0 deletions app/services/api_course_search_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class APICourseSearchService < CourseSearchService
def ordering_strategy
@ordering_strategy ||= CourseOrdering::Strategy.find(
sort:,
default: :api_default_ordering
).new(course_search_service: self)
end

def with_course_type(scope)
scope # return all course types in the API
end
end
13 changes: 13 additions & 0 deletions app/services/course_filters/applications_open_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module CourseFilters
class ApplicationsOpenFilter < BaseFilter
def call(scope)
scope.application_status_open
end

def add_filter?
filter[:applications_open].to_s.downcase == 'true'
end
end
end
40 changes: 40 additions & 0 deletions app/services/course_filters/base_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module CourseFilters
class BaseFilter
delegate :filter, to: :course_search_service
attr_reader :course_search_service

def initialize(course_search_service)
@course_search_service = course_search_service
end

def applicable_filter?
log_filters
add_filter?
end

def call(scope)
raise NotImplementedError, 'Subclasses must implement the #call method'
end

def add_filter?
raise NotImplementedError, 'Subclasses must implement the #add_filter? method'
end

private

# Purple in the logs when filter is applied, Dark Red for not applied
FILTER_APPLIED_COLOR = "\e[35m"
FILTER_NOT_APPLIED_COLOR = "\e[31m"

def log_filters
return unless Rails.env.local?

color = add_filter? ? FILTER_APPLIED_COLOR : FILTER_NOT_APPLIED_COLOR
reset = "\e[0m" # Reset to default color

Rails.logger.info("#{color}#{self.class} filter #{add_filter? ? 'applied' : 'NOT applied'}#{reset}")
end
end
end
41 changes: 41 additions & 0 deletions app/services/course_filters/degree_grades_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

module CourseFilters
class DegreeGradesFilter < BaseFilter
def call(scope)
scope = scope.with_degree_grades(degree_grades_accepted) if degrees_accepted?
scope = scope.with_degree_grades(degree_grades) if degree_grades.any?
scope
end

def add_filter?
degrees_accepted? || degree_grades.any?
end

def degrees_accepted?
filter[:degree_required].present?
end

def degree_grades_accepted
return [] unless degrees_accepted?

degree_required_parameter = filter[:degree_required].to_sym

accepted_degrees = {
show_all_courses: 'two_one,two_two,third_class,not_required',
two_two: 'two_two,third_class,not_required',
third_class: 'third_class,not_required',
not_required: 'not_required'
}

(accepted_degrees[degree_required_parameter] || accepted_degrees[:show_all_courses]).split(',')
end

def degree_grades
return [] if filter[:degree_grade].blank?
return [] unless filter[:degree_grade].is_a?(String)

filter[:degree_grade].split(',')
end
end
end
14 changes: 14 additions & 0 deletions app/services/course_filters/engineers_teach_physics_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module CourseFilters
class EngineersTeachPhysicsFilter < BaseFilter
def call(scope)
scope.engineers_teach_physics
end

def add_filter?
filter[:engineers_teach_physics].to_s.downcase == 'true' ||
filter[:campaign_name] == 'engineers_teach_physics'
end
end
end
13 changes: 13 additions & 0 deletions app/services/course_filters/findable_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module CourseFilters
class FindableFilter < BaseFilter
def call(scope)
scope.findable
end

def add_filter?
filter[:findable].to_s.downcase == 'true'
end
end
end
13 changes: 13 additions & 0 deletions app/services/course_filters/funding_types_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module CourseFilters
class FundingTypesFilter < BaseFilter
def call(scope)
scope.with_funding_types(filter[:funding_type].split(','))
end

def add_filter?
filter[:funding_type].present?
end
end
end
18 changes: 18 additions & 0 deletions app/services/course_filters/locations_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module CourseFilters
class LocationsFilter < BaseFilter
def call(scope)
scope.within(
filter[:radius],
origin: [filter[:latitude], filter[:longitude]]
)
end

def add_filter?
filter.key?(:latitude) &&
filter.key?(:longitude) &&
filter.key?(:radius)
end
end
end
13 changes: 13 additions & 0 deletions app/services/course_filters/provider_name_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module CourseFilters
class ProviderNameFilter < BaseFilter
def call(scope)
scope.with_provider_name(filter[:'provider.provider_name'])
end

def add_filter?
filter[:'provider.provider_name'].present?
end
end
end
31 changes: 31 additions & 0 deletions app/services/course_filters/qualifications_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module CourseFilters
class QualificationsFilter < BaseFilter
delegate :course_type, to: :course_search_service

def call(scope)
scope.with_qualifications(qualifications)
end

def add_filter?
qualifications.present?
end

def qualifications
return [] if filter[:qualification].blank? || course_type == :undergraduate

filter[:qualification] = filter[:qualification].values if filter[:qualification].is_a?(Hash)
filter[:qualification] = filter[:qualification].split(',') if filter[:qualification].is_a?(String)

if filter[:qualification].include?('pgce pgde')
filter[:qualification] -= ['pgce pgde']
filter[:qualification] |= %w[pgce pgde]
end

filter[:qualification] |= %w[pgde_with_qts] if filter[:qualification].is_a?(Array) && filter[:qualification].include?('pgce_with_qts')

filter[:qualification]
end
end
end
13 changes: 13 additions & 0 deletions app/services/course_filters/salary_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module CourseFilters
class SalaryFilter < BaseFilter
def call(scope)
scope.with_salary
end

def add_filter?
filter[:funding] == 'salary'
end
end
end
13 changes: 13 additions & 0 deletions app/services/course_filters/send_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module CourseFilters
class SendFilter < BaseFilter
def call(scope)
scope.with_send
end

def add_filter?
filter[:send_courses].to_s.downcase == 'true'
end
end
end
23 changes: 23 additions & 0 deletions app/services/course_filters/study_modes_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module CourseFilters
class StudyModesFilter < BaseFilter
def call(scope)
scope.with_study_modes(study_types)
end

def add_filter?
study_types.present?
end

def study_types
# this passes for strings and arrays
return [] if filter[:study_type].blank?
return filter[:study_type] if filter[:study_type].is_a? Array

filter[:study_type] = filter[:study_type].values if filter[:study_type].is_a?(Hash)

filter[:study_type].split(',')
end
end
end
21 changes: 21 additions & 0 deletions app/services/course_filters/subjects_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module CourseFilters
class SubjectsFilter < BaseFilter
def call(scope)
scope.with_subjects(subject_codes)
end

def add_filter?
subject_codes.present?
end

def subject_codes
return [] if filter[:subjects].blank?
return filter[:subjects] if filter[:subjects].is_a? Array
return filter[:subjects].values if filter[:subjects].is_a?(Hash)

filter[:subjects].split(',')
end
end
end
13 changes: 13 additions & 0 deletions app/services/course_filters/update_since_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module CourseFilters
class UpdateSinceFilter < BaseFilter
def call(scope)
scope.changed_since(filter[:updated_since])
end

def add_filter?
filter[:updated_since].present?
end
end
end
13 changes: 13 additions & 0 deletions app/services/course_filters/visa_sponsorship_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module CourseFilters
class VisaSponsorshipFilter < BaseFilter
def call(scope)
scope.can_sponsor_visa
end

def add_filter?
filter[:can_sponsor_visa].to_s.downcase == 'true'
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module CourseOrdering
class APIDefaultOrderingStrategy < BaseStrategy
def order(scope)
scope.order('course.id ASC')
end
end
end
17 changes: 17 additions & 0 deletions app/services/course_ordering/base_strategy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module CourseOrdering
class BaseStrategy
attr_reader :course_search_service

delegate :filter, to: :course_search_service

def initialize(course_search_service:)
@course_search_service = course_search_service
end

def order(scope)
raise NotImplementedError, 'Subclasses must implement the #order method'
end
end
end
9 changes: 9 additions & 0 deletions app/services/course_ordering/course_asc_strategy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module CourseOrdering
class CourseAscStrategy < BaseStrategy
def order(scope)
scope.ascending_course_canonical_order
end
end
end
9 changes: 9 additions & 0 deletions app/services/course_ordering/course_desc_strategy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module CourseOrdering
class CourseDescStrategy < BaseStrategy
def order(scope)
scope.descending_course_canonical_order
end
end
end
Loading
Loading