This repository has been archived by the owner on Dec 15, 2020. It is now read-only.
forked from DFE-Digital/teaching-vacancies
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vacancy.rb
192 lines (157 loc) · 5.34 KB
/
vacancy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
require 'elasticsearch/model'
require 'auditor'
class Vacancy < ApplicationRecord
include ApplicationHelper
include Auditor::Model
include VacancyJobSpecificationValidations
include VacancyCandidateSpecificationValidations
include VacancyApplicationDetailValidations
include Elasticsearch::Model
include Redis::Objects
index_name [Rails.env, model_name.collection.tr('\/', '-')].join('_')
document_type 'vacancy'
settings index: {
analysis: {
analyzer: {
stopwords: {
tokenizer: 'standard',
filter: ['standard', 'lowercase', 'english_stopwords', 'stopwords']
}
},
filter: {
english_stopwords: {
type: 'stop',
stopwords: '_english_'
},
stopwords: {
type: 'stop',
stopwords: ['part', 'full', 'time']
}
}
}
} do
mappings dynamic: 'false' do
indexes :job_title, type: :text, analyzer: :stopwords
indexes :job_description, analyzer: 'english'
indexes :school do
indexes :name, analyzer: 'english'
indexes :phase, type: :keyword
indexes :postcode, type: :text
indexes :town, type: :text
indexes :county, type: :text
indexes :address, type: :text
end
indexes :subject do
indexes :name, type: :text
end
indexes :first_supporting_subject do
indexes :name, type: :text
end
indexes :second_supporting_subject do
indexes :name, type: :text
end
indexes :expires_on, type: :date
indexes :starts_on, type: :date
indexes :updated_at, type: :date
indexes :publish_on, type: :date
indexes :status, type: :keyword
indexes :working_pattern, type: :keyword
indexes :minimum_salary, type: :integer
indexes :maximum_salary, type: :integer
indexes :coordinates, type: :geo_point, ignore_malformed: true
indexes :newly_qualified_teacher, type: :boolean
end
end
extend FriendlyId
friendly_id :slug_candidates, use: %w[slugged history]
enum status: %i[published draft trashed]
enum working_pattern: %i[full_time part_time]
belongs_to :school, optional: false
belongs_to :subject, optional: true
belongs_to :first_supporting_subject, class_name: 'Subject', optional: true
belongs_to :second_supporting_subject, class_name: 'Subject', optional: true
belongs_to :min_pay_scale, class_name: 'PayScale', optional: true
belongs_to :max_pay_scale, class_name: 'PayScale', optional: true
belongs_to :leadership, optional: true
has_one :feedback
delegate :name, to: :school, prefix: true, allow_nil: false
delegate :geolocation, to: :school, prefix: true, allow_nil: true
acts_as_gov_uk_date :starts_on, :ends_on, :publish_on, :expires_on
scope :applicable, (-> { where('expires_on >= ?', Time.zone.today) })
scope :active, (-> { where(status: %i[published draft]) })
scope :listed, (-> { published.where('publish_on <= ?', Time.zone.today) })
scope :published_on_count, (->(date) { published.where('date(publish_on) = ?', date).count })
scope :pending, (-> { published.where('publish_on > ?', Time.zone.today) })
scope :expired, (-> { published.where('expires_on < ?', Time.zone.today) })
scope :live, (-> { published.where('publish_on <= ?', Time.zone.today).where('expires_on > ?', Time.zone.today) })
paginates_per 10
validates :slug, presence: true
after_commit on: %i[create update] do
__elasticsearch__.index_document
end
counter :page_view_counter
def location
@location ||= SchoolPresenter.new(school).location
end
def coordinates
return if school_geolocation.nil?
{
lat: school_geolocation.x.to_f,
lon: school_geolocation.y.to_f
}
end
def self.public_search(filters:, sort:)
query = VacancySearchBuilder.new(filters: filters, sort: sort).call
results = ElasticSearchFinder.new.call(query[:search_query], query[:search_sort])
Rollbar.log(:info, 'A search returned 0 results', filters.to_hash) if results.count.zero?
results
end
def listed?
published? && !publish_on.future? && expires_on.future?
end
# rubocop:disable Naming/UncommunicativeMethodParamName
def as_indexed_json(_ = {})
as_json(
methods: %i[coordinates],
include: {
school: { only: %i[phase name postcode address town] },
subject: { only: %i[name] },
first_supporting_subject: { only: %i[name] },
second_supporting_subject: { only: %i[name] }
}
)
end
# rubocop:enable Naming/UncommunicativeMethodParamName
def trash!
self.status = :trashed
save(validate: false)
end
def application_link=(value)
# Data may not include a scheme/protocol so we must be careful when creating
# links that Rails doesn't make them incorrectly relative.
begin
value = Addressable::URI.heuristic_parse(value).to_s
rescue Addressable::URI::InvalidURIError
Rails.logger.debug('Validation error: Invalid application link format')
end
super(value)
end
def refresh_slug
self.slug = nil
send(:set_slug)
end
def minimum_salary=(salary)
self[:minimum_salary] = salary.to_s.strip
end
def maximum_salary=(salary)
self[:maximum_salary] = salary.to_s.strip
end
private
def slug_candidates
[
:job_title,
%i[job_title school_name],
%i[job_title location],
]
end
end