-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
1 parent
2d2ab2f
commit 3165590
Showing
8 changed files
with
200 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
4 changes: 2 additions & 2 deletions
4
app/services/courses_query/logger.rb → app/services/courses/query_logger.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.