Skip to content

Commit

Permalink
Create SessionDate model
Browse files Browse the repository at this point in the history
This adds a model which will capture the dates for a particular session,
it's a one-to-many relationship with a unique constraint on the session
and date value.

This will eventually replace the single date column on sessions as the
latest designs have sessions across multiple dates.
  • Loading branch information
thomasleese committed Sep 25, 2024
1 parent 27dd4fd commit 69d6851
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/models/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Session < ApplicationRecord
belongs_to :location, optional: true

has_many :consent_forms
has_many :dates, class_name: "SessionDate"
has_many :patient_sessions

has_and_belongs_to_many :immunisation_imports
Expand Down
26 changes: 26 additions & 0 deletions app/models/session_date.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: session_dates
#
# id :bigint not null, primary key
# value :date not null
# session_id :bigint not null
#
# Indexes
#
# index_session_dates_on_session_id (session_id)
# index_session_dates_on_session_id_and_value (session_id,value) UNIQUE
#
# Foreign Keys
#
# fk_rails_... (session_id => sessions.id)
#
class SessionDate < ApplicationRecord
audited

belongs_to :session

validates :value, uniqueness: { scope: :session }
end
13 changes: 13 additions & 0 deletions db/migrate/20240925075935_create_session_dates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class CreateSessionDates < ActiveRecord::Migration[7.2]
def change
# rubocop:disable Rails/CreateTableWithTimestamps
create_table :session_dates do |t|
t.references :session, foreign_key: true, null: false
t.date :value, null: false
t.index %i[session_id value], unique: true
end
# rubocop:enable Rails/CreateTableWithTimestamps
end
end
10 changes: 9 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2024_09_24_140304) do
ActiveRecord::Schema[7.2].define(version: 2024_09_25_081048) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -461,6 +461,13 @@
t.index ["vaccine_id", "programme_id"], name: "index_programmes_vaccines_on_vaccine_id_and_programme_id"
end

create_table "session_dates", force: :cascade do |t|
t.bigint "session_id", null: false
t.date "value", null: false
t.index ["session_id", "value"], name: "index_session_dates_on_session_id_and_value", unique: true
t.index ["session_id"], name: "index_session_dates_on_session_id"
end

create_table "sessions", force: :cascade do |t|
t.date "date"
t.bigint "location_id"
Expand Down Expand Up @@ -629,6 +636,7 @@
add_foreign_key "programmes", "teams"
add_foreign_key "programmes_sessions", "programmes"
add_foreign_key "programmes_sessions", "sessions"
add_foreign_key "session_dates", "sessions"
add_foreign_key "sessions", "teams"
add_foreign_key "triage", "patient_sessions"
add_foreign_key "triage", "programmes"
Expand Down
Binary file modified erd.pdf
Binary file not shown.
25 changes: 25 additions & 0 deletions spec/factories/session_dates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: session_dates
#
# id :bigint not null, primary key
# value :date not null
# session_id :bigint not null
#
# Indexes
#
# index_session_dates_on_session_id (session_id)
# index_session_dates_on_session_id_and_value (session_id,value) UNIQUE
#
# Foreign Keys
#
# fk_rails_... (session_id => sessions.id)
#
FactoryBot.define do
factory :session_date do
session
value { Date.current }
end
end
24 changes: 24 additions & 0 deletions spec/models/session_date_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: session_dates
#
# id :bigint not null, primary key
# value :date not null
# session_id :bigint not null
#
# Indexes
#
# index_session_dates_on_session_id (session_id)
# index_session_dates_on_session_id_and_value (session_id,value) UNIQUE
#
# Foreign Keys
#
# fk_rails_... (session_id => sessions.id)
#
describe SessionDate do
subject(:session_date) { build(:session_date) }

it { should be_valid }
end

0 comments on commit 69d6851

Please sign in to comment.