Skip to content

Commit c06aa50

Browse files
Add SchoolMovesExport model
Jira-Issue: MAV-1521
1 parent 967c2f4 commit c06aa50

6 files changed

Lines changed: 114 additions & 0 deletions

File tree

app/models/export.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Export < ApplicationRecord
3434
LocationPatientsExport
3535
SessionPatientsExport
3636
VaccinationRecordsExport
37+
SchoolMovesExport
3738
],
3839
dependent: :destroy
3940

app/models/school_moves_export.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
# == Schema Information
4+
#
5+
# Table name: school_moves_exports
6+
#
7+
# id :bigint not null, primary key
8+
# date_from :date
9+
# date_to :date
10+
# created_at :datetime not null
11+
# updated_at :datetime not null
12+
#
13+
class SchoolMovesExport < ApplicationRecord
14+
has_one :export, as: :exportable, touch: true
15+
delegate :team, to: :export, allow_nil: true
16+
17+
def file_type = :csv
18+
19+
def type_label = "School moves"
20+
21+
def name = "School moves"
22+
23+
def filename
24+
parts = ["school_moves_export"]
25+
parts << date_from.to_fs(:govuk) if date_from.present?
26+
parts << "to" if date_from.present? && date_to.present?
27+
parts << date_to.to_fs(:govuk) if date_to.present?
28+
"#{parts.join("_")}.csv"
29+
end
30+
31+
def generate_file
32+
Reports::SchoolMovesExporter.new(
33+
team:,
34+
start_date: date_from,
35+
end_date: date_to
36+
).csv_data
37+
end
38+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
class CreateSchoolMovesExports < ActiveRecord::Migration[8.1]
4+
def change
5+
create_table :school_moves_exports do |t|
6+
t.date :date_from
7+
t.date :date_to
8+
9+
t.timestamps
10+
end
11+
end
12+
end

db/schema.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,13 @@
873873
t.index ["school_id"], name: "index_school_moves_on_school_id"
874874
end
875875

876+
create_table "school_moves_exports", force: :cascade do |t|
877+
t.datetime "created_at", null: false
878+
t.date "date_from"
879+
t.date "date_to"
880+
t.datetime "updated_at", null: false
881+
end
882+
876883
create_table "session_notifications", force: :cascade do |t|
877884
t.bigint "patient_id", null: false
878885
t.datetime "sent_at", default: -> { "CURRENT_TIMESTAMP" }, null: false
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
# == Schema Information
4+
#
5+
# Table name: school_moves_exports
6+
#
7+
# id :bigint not null, primary key
8+
# date_from :date
9+
# date_to :date
10+
# created_at :datetime not null
11+
# updated_at :datetime not null
12+
#
13+
FactoryBot.define do
14+
factory :school_moves_export do
15+
end
16+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
# == Schema Information
4+
#
5+
# Table name: school_moves_exports
6+
#
7+
# id :bigint not null, primary key
8+
# date_from :date
9+
# date_to :date
10+
# created_at :datetime not null
11+
# updated_at :datetime not null
12+
#
13+
describe SchoolMovesExport do
14+
describe "#name" do
15+
subject { described_class.new.name }
16+
17+
it { should eq("School moves") }
18+
end
19+
20+
describe "#filename" do
21+
subject(:filename) { export.filename }
22+
23+
context "with date range" do
24+
let(:export) do
25+
described_class.new(
26+
date_from: Date.new(2024, 1, 1),
27+
date_to: Date.new(2025, 12, 31)
28+
)
29+
end
30+
31+
it { should eq("school_moves_export_2024-01-01_to_2025-12-31.csv") }
32+
end
33+
34+
context "without dates" do
35+
let(:export) { described_class.new }
36+
37+
it { should eq("school_moves_export.csv") }
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)