Skip to content

Commit

Permalink
Move v2 classes into Courses module
Browse files Browse the repository at this point in the history
- Replace `SearchCoursesForm` with `Courses::SearchForm` for improved namespacing.
- Introduce `Courses::Query` to handle course query logic, replacing the existing `CoursesQuery`.
- Move `CoursesQuery::Logger` to `Courses::QueryLogger` for consistency with the new namespacing.
  • Loading branch information
tomas-stefano committed Jan 8, 2025
1 parent 2d2ab2f commit 3165590
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 196 deletions.
4 changes: 2 additions & 2 deletions app/controllers/find/v2/results_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Find
module V2
class ResultsController < Find::ApplicationController
def index
@search_courses_form = SearchCoursesForm.new(search_courses_params)
@courses = CoursesQuery.call(params: @search_courses_form.search_params)
@search_courses_form = ::Courses::SearchForm.new(search_courses_params)
@courses = ::Courses::Query.call(params: @search_courses_form.search_params)
@courses_count = @courses.count

@pagy, @results = pagy(@courses)
Expand Down
60 changes: 60 additions & 0 deletions app/forms/courses/search_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

module Courses
class SearchForm < ApplicationForm
include ActiveModel::Attributes

attribute :can_sponsor_visa, :boolean
attribute :subjects
attribute :send_courses, :boolean
attribute :applications_open, :boolean
attribute :study_types
attribute :qualifications
attribute :level
attribute :funding

attribute :age_group
attribute :qualification

def search_params
attributes
.symbolize_keys
.then { |params| params.except(*old_parameters) }
.then { |params| transform_old_parameters(params) }
.compact
end

def level
return 'further_education' if old_further_education_parameters?

super
end

def secondary_subjects
Subject
.where(type: %w[SecondarySubject ModernLanguagesSubject])
.where.not(subject_name: ['Modern Languages'])
.order(:subject_name)
end

def primary_subjects
Subject.where(type: 'PrimarySubject').order(:subject_name)
end

private

def transform_old_parameters(params)
params.tap do
params[:level] = level
end
end

def old_further_education_parameters?
age_group == 'further_education' || qualification == ['pgce pgde']
end

def old_parameters
%i[age_group qualification]
end
end
end
58 changes: 0 additions & 58 deletions app/forms/search_courses_form.rb

This file was deleted.

134 changes: 134 additions & 0 deletions app/services/courses/query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# frozen_string_literal: true

module Courses
class Query
def self.call(...)
new(...).call
end

attr_reader :scope, :params, :applied_scopes

def initialize(params:)
@params = params
@applied_scopes = {}
@scope = RecruitmentCycle
.current
.courses
.joins(:site_statuses)
.where(
site_statuses: {
status: SiteStatus.statuses[:running],
publish: SiteStatus.publishes[:published]
}
)
end

def call
@scope = visa_sponsorship_scope
@scope = subjects_scope
@scope = study_modes_scope
@scope = qualifications_scope
@scope = further_education_scope
@scope = applications_open_scope
@scope = special_education_needs_scope
@scope = funding_scope
@scope = @scope.distinct

log_query_info

@scope
end

def visa_sponsorship_scope
return @scope if params[:can_sponsor_visa].blank?

@applied_scopes[:can_sponsor_visa] = params[:can_sponsor_visa]

@scope
.where(
can_sponsor_student_visa: true
)
.or(
@scope.where(
can_sponsor_skilled_worker_visa: true
)
)
end

def subjects_scope
return @scope if params[:subjects].blank?

@applied_scopes[:subjects] = params[:subjects]

@scope.joins(:subjects).where(subjects: { subject_code: params[:subjects] })
end

def study_modes_scope
return @scope if params[:study_types].blank?

@applied_scopes[:study_modes] = params[:study_types]

case params[:study_types]
when ['full_time']
@scope.where(study_mode: [Course.study_modes[:full_time], Course.study_modes[:full_time_or_part_time]])
when ['part_time']
@scope.where(study_mode: [Course.study_modes[:part_time], Course.study_modes[:full_time_or_part_time]])
else
@scope
end
end

def qualifications_scope
return @scope if params[:qualifications].blank?

@applied_scopes[:qualifications_scope] = params[:qualifications]

case params[:qualifications]
when ['qts']
@scope.where(qualification: [Course.qualifications[:qts]])
when ['qts_with_pgce_or_pgde'], ['qts_with_pgce']
@scope.where(qualification: [Course.qualifications[:pgce_with_qts], Course.qualifications[:pgde_with_qts]])
else
@scope
end
end

def further_education_scope
return @scope if params[:level] != 'further_education'

@applied_scopes[:level] = params[:level]

@scope.where(level: Course.levels[:further_education])
end

def applications_open_scope
return @scope if params[:applications_open].blank?

@applied_scopes[:applications_open] = params[:applications_open]

@scope.where(application_status: Course.application_statuses[:open])
end

def special_education_needs_scope
return @scope if params[:send_courses].blank?

@applied_scopes[:send_courses] = params[:send_courses]

@scope.where(is_send: true)
end

def funding_scope
return @scope if params[:funding].blank?

@applied_scopes[:funding] = params[:funding]

@scope.where(funding: params[:funding])
end

private

def log_query_info
Courses::QueryLogger.new(self).call
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class CoursesQuery
class Logger
module Courses
class QueryLogger
attr_reader :courses_query

delegate :scope, :applied_scopes, to: :courses_query
Expand Down
Loading

0 comments on commit 3165590

Please sign in to comment.