Skip to content

Commit

Permalink
Moving job filtering to a service and off the main model
Browse files Browse the repository at this point in the history
  • Loading branch information
Ches-ctrl committed May 25, 2024
1 parent b4d9870 commit 47af1bb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 70 deletions.
14 changes: 9 additions & 5 deletions app/controllers/jobs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ class JobsController < ApplicationController
before_action :set_saved_jobs, only: [:index]
before_action :set_job_applications, only: [:index]

# TODO: Sort in filter

def index
@jobs = Job.filter_and_sort(params)
.includes(associated_tables)
.order(sort_order(params[:sort]))
.page(params[:page]).per(20)
filtered_jobs = JobFilter.new(params).filter_and_sort

@jobs = filtered_jobs.includes(associated_tables)
.order(sort_order(params[:sort]))
.page(params[:page])
.per(20)

@resources, @total_jobs = CategorySidebar.new(params).build
@resources, @total_jobs = CategorySidebar.new(filtered_jobs, params).build
end

def show
Expand Down
57 changes: 1 addition & 56 deletions app/models/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,65 +51,10 @@ class Job < ApplicationRecord
after_create :set_date_created, :update_requirements, :standardize_attributes

# == Class Methods ========================================================
def self.filter_and_sort(params)
filters = {
date_posted: filter_by_when_posted(params[:posted]),
seniority: filter_by_seniority(params[:seniority]),
locations: filter_by_location(params[:location]),
roles: filter_by_role(params[:role]),
employment_type: filter_by_employment(params[:type])
}.compact

associations = build_associations(params)
jobs = left_joins(associations).where(filters)
params[:query].present? ? jobs.search_job(params[:query]) : jobs
end

def self.including_any(params, param)
filter_and_sort params.except(param)
end

# == Class Methods (Private) ==============================================
def self.build_associations(params)
associations = []
associations << :company if params.include?(:company)
associations << :locations if params.include?(:location)
associations << :roles if params.include?(:role)
return associations
end

def self.filter_by_when_posted(param)
return unless param.present?

number = Constants::DateConversion::CONVERT_TO_DAYS[param] || 99_999
number.days.ago..Date.today
end

def self.filter_by_location(param)
return unless param.present?

locations = param.split.map { |location| location.gsub('_', ' ').split.map(&:capitalize).join(' ') unless location == 'remote' }
{ city: locations }
JobFilter.new(params.except(param)).filter_and_sort
end

def self.filter_by_role(param)
{ name: param.split } if param.present?
end

def self.filter_by_seniority(param)
return unless param.present?

param.split.map { |seniority| seniority.split('-').map(&:capitalize).join('-') }
end

def self.filter_by_employment(param)
return unless param.present?

param.split.map { |employment| employment.gsub('_', '-').capitalize }
end

private_class_method :build_associations, :filter_by_when_posted, :filter_by_location, :filter_by_role, :filter_by_seniority, :filter_by_employment

# == Instance Methods =====================================================
def application_criteria
return [] if read_attribute(:application_criteria).nil?
Expand Down
7 changes: 6 additions & 1 deletion app/services/category_sidebar.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
class CategorySidebar
include Constants::CategorySidebar

def initialize(params)
def initialize(filtered_jobs, params)
@params = params

# Leaving this here as presumably we need to pass the sorted jobs to the sidebar?
@filtered_jobs = filtered_jobs

# Why are we doing a new Job.includes query here after the job filtering process?
@jobs = Job.includes(:roles, :locations).select("jobs.id, jobs.date_posted, jobs.seniority, jobs.remote, jobs.employment_type")
end

Expand Down
25 changes: 17 additions & 8 deletions app/services/job_filter.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
class JobFilter
def initialize(params)
p "hello filter"
@jobs = Job.all
def initialize(params, scope = Job.all)
@jobs = scope
@params = params
end

def filter_and_sort
p "hello filter and sort"
jobs = @jobs
jobs = apply_filters(jobs)
apply_search(jobs)
end

private

def apply_filters(jobs)
filters = {
date_posted: filter_by_when_posted(@params[:posted]),
seniority: filter_by_seniority(@params[:seniority]),
Expand All @@ -16,12 +22,9 @@ def filter_and_sort
}.compact

associations = build_associations
jobs = @jobs.left_joins(associations).where(filters)
@params[:query].present? ? jobs.search_job(@params[:query]) : jobs
jobs.left_joins(associations).where(filters)
end

private

def build_associations
associations = []
associations << :company if @params.include?(:company)
Expand All @@ -30,6 +33,12 @@ def build_associations
associations
end

def apply_search(jobs)
return jobs unless @params[:query].present?

jobs.search_job(@params[:query])
end

def filter_by_when_posted(param)
return unless param.present?

Expand Down

0 comments on commit 47af1bb

Please sign in to comment.