Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create SessionDate model #1830

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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