Skip to content

Commit f5746f4

Browse files
committed
Refactor "discard/trash" tasks logic with same approach
1 parent e0d47e4 commit f5746f4

9 files changed

+64
-66
lines changed

app/models/published_vacancy.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ def find_publisher_by_contact_email
8686
.find_by(email: contact_email)
8787
end
8888

89+
def self.discard_out_of_scope
90+
kept.joins(:organisations)
91+
.where(organisations: { detailed_school_type: Organisation::OUT_OF_SCOPE_DETAILED_SCHOOL_TYPES })
92+
.distinct
93+
.find_each(&:trash!)
94+
end
95+
8996
private
9097

9198
def remove_google_index

app/models/subscription.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ def set_location_data!
9292
save!
9393
end
9494

95+
def self.discard_invalid
96+
find_each.reject(&:valid?).each(&:discard!)
97+
end
98+
9599
private
96100

97101
# A subscription with location area has a polygon area seat buffered by radius, no geopoint.

lib/tasks/data.rake

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ namespace :backfills do
7878
end
7979
end
8080

81+
namespace :subscriptions do
82+
desc "Discard subscriptions that fail validation (probably due to invalid email address)"
83+
task discard_invalid: :environment do
84+
Subscription.discard_invalid
85+
end
86+
end
87+
88+
namespace :vacancies do
89+
desc "Trash published vacancies from out-of-scope schools"
90+
task discard_out_of_scope: :environment do
91+
PublishedVacancy.discard_out_of_scope
92+
end
93+
end
94+
8195
namespace :publishers do
8296
desc "Reset 'New features' attributes so all publishers are shown 'New features' page"
8397
task reset_new_features_attributes: :environment do

lib/tasks/discard_invalid_subscriptions.rake

Lines changed: 0 additions & 5 deletions
This file was deleted.

lib/tasks/discard_out_of_scope_vacancies.rake

Lines changed: 0 additions & 13 deletions
This file was deleted.

spec/lib/tasks/discard_invalid_subscriptions_rake_task_spec.rb

Lines changed: 0 additions & 18 deletions
This file was deleted.

spec/lib/tasks/discard_out_of_scope_vacancies_rake_task_spec.rb

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require "rails_helper"
2+
3+
RSpec.describe PublishedVacancy do
4+
describe ".discard_out_of_scope" do
5+
it "trashes vacancies from out-of-scope schools" do
6+
in_scope_school = create(:school, detailed_school_type: "Academy sponsor led")
7+
in_scope_vacancy = create(:vacancy, organisations: [in_scope_school])
8+
out_of_scope_school = create(:school, detailed_school_type: "Other independent school")
9+
further_education_school = create(:school, detailed_school_type: "Further education")
10+
higher_education_school = create(:school, detailed_school_type: "Higher education institutions")
11+
12+
out_of_scope_vacancy = create(:vacancy, organisations: [out_of_scope_school])
13+
further_ed_vacancy = create(:vacancy, organisations: [further_education_school])
14+
higher_ed_vacancy = create(:vacancy, organisations: [higher_education_school])
15+
16+
expect {
17+
described_class.discard_out_of_scope
18+
}.to change { described_class.kept.count }.by(-3)
19+
20+
expect(out_of_scope_vacancy.reload).to be_discarded
21+
expect(further_ed_vacancy.reload).to be_discarded
22+
expect(higher_ed_vacancy.reload).to be_discarded
23+
expect(in_scope_vacancy.reload).not_to be_discarded
24+
end
25+
end
26+
end

spec/models/subscription_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,17 @@
361361
end
362362
end
363363
end
364+
365+
describe ".discard_invalid" do
366+
before do
367+
create(:subscription)
368+
build(:subscription, email: "invalid").save!(validate: false)
369+
end
370+
371+
it "marks the invalid subscription as discarded" do
372+
expect {
373+
described_class.discard_invalid
374+
}.to change { Subscription.kept.count }.by(-1)
375+
end
376+
end
364377
end

0 commit comments

Comments
 (0)