Skip to content

Commit b1fc7e5

Browse files
committed
Add submitted_at column to consents
This adds a new column on consents which records when the consent was submitted, which in most cases is the same as when the consent was created, but in the case of manually matching a consent form with a patient, it'll be when the consent form was first submitted.
1 parent d6ee775 commit b1fc7e5

5 files changed

Lines changed: 25 additions & 1 deletion

File tree

app/models/consent.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# reason_for_refusal :integer
1313
# response :integer not null
1414
# route :integer not null
15+
# submitted_at :datetime not null
1516
# withdrawn_at :datetime
1617
# created_at :datetime not null
1718
# updated_at :datetime not null
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
class AddSubmittedAtToConsents < ActiveRecord::Migration[8.0]
4+
def up
5+
add_column :consents, :submitted_at, :datetime
6+
7+
Consent
8+
.includes(:consent_form)
9+
.find_each do |consent|
10+
submitted_at = consent.consent_form&.recorded_at || consent.created_at
11+
consent.update_column(:submitted_at, submitted_at)
12+
end
13+
14+
change_column_null :consents, :submitted_at, false
15+
end
16+
17+
def down
18+
remove_column :consents, :submitted_at
19+
end
20+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[8.0].define(version: 2025_04_29_140846) do
13+
ActiveRecord::Schema[8.0].define(version: 2025_05_15_173205) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "pg_catalog.plpgsql"
1616
enable_extension "pg_trgm"
@@ -237,6 +237,7 @@
237237
t.datetime "withdrawn_at"
238238
t.datetime "invalidated_at"
239239
t.boolean "notify_parents"
240+
t.datetime "submitted_at", null: false
240241
t.index ["organisation_id"], name: "index_consents_on_organisation_id"
241242
t.index ["parent_id"], name: "index_consents_on_parent_id"
242243
t.index ["patient_id"], name: "index_consents_on_patient_id"

spec/factories/consents.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# reason_for_refusal :integer
1313
# response :integer not null
1414
# route :integer not null
15+
# submitted_at :datetime not null
1516
# withdrawn_at :datetime
1617
# created_at :datetime not null
1718
# updated_at :datetime not null

spec/models/consent_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# reason_for_refusal :integer
1313
# response :integer not null
1414
# route :integer not null
15+
# submitted_at :datetime not null
1516
# withdrawn_at :datetime
1617
# created_at :datetime not null
1718
# updated_at :datetime not null

0 commit comments

Comments
 (0)