Skip to content
This repository was archived by the owner on Aug 4, 2023. It is now read-only.

#1.1 create patient model #5

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
12 changes: 12 additions & 0 deletions app/models/doctor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Doctor < ApplicationRecord

validates :first_name, :last_name, :practicing_from, :qualification_number, :employment, presence: true
validates_uniqueness_of :qualification_number

enum employment: {
full_time: 0,
part_time: 1,
intern: 2,
contract: 3
}
end
10 changes: 10 additions & 0 deletions app/models/patient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Patient < ApplicationRecord

validates :first_name, :last_name, :age, presence: true

enum gender: {
male: 0,
female: 1,
other: 2
}
end
11 changes: 11 additions & 0 deletions db/migrate/20191120224317_create_patients.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreatePatients < ActiveRecord::Migration[5.2]
def change
create_table :patients do |t|
t.string :first_name, null: false
t.string :last_name, null: false
t.integer :age, null: false
t.integer :gender, null: false
t.timestamps
end
end
end
14 changes: 14 additions & 0 deletions db/migrate/20191120225245_create_doctors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateDoctors < ActiveRecord::Migration[5.2]
def change
create_table :doctors do |t|
t.string :first_name, null: false
t.string :last_name, null: false
t.timestamp :practicing_from, null: false
t.integer :qualification_number, null: false
t.integer :employment, null: false, default: 0
t.timestamps
end

add_index :doctors, :qualification_number, unique: true
end
end
28 changes: 24 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
Expand All @@ -12,7 +10,29 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 0) do
ActiveRecord::Schema.define(version: 2019_11_20_225245) do

# These are extensions that must be enabled in order to support this database
enable_extension 'plpgsql'
enable_extension "plpgsql"

create_table "doctors", force: :cascade do |t|
t.string "first_name", null: false
t.string "last_name", null: false
t.datetime "practicing_from", null: false
t.integer "qualification_number", null: false
t.integer "employment", default: 0, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["qualification_number"], name: "index_doctors_on_qualification_number", unique: true
end

create_table "patients", force: :cascade do |t|
t.string "first_name", null: false
t.string "last_name", null: false
t.integer "age", null: false
t.integer "gender", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
9 changes: 9 additions & 0 deletions spec/factories/doctors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FactoryBot.define do
factory :doctor do
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
practicing_from { Time.current - rand(20).years }
sequence(:qualification_number) { |n| n }
employment { 0 }
end
end
8 changes: 8 additions & 0 deletions spec/factories/patients.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :patient do
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
age { rand(100) }
gender { 1 }
end
end
21 changes: 21 additions & 0 deletions spec/models/doctor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'rails_helper'

describe Doctor do
context 'validations' do
it { should validate_presence_of :first_name }
it { should validate_presence_of :last_name }
it { should validate_presence_of :practicing_from }
it { should validate_presence_of :qualification_number }
it { should validate_presence_of :employment }

context 'uniqueness' do
let!(:doctor) { create(:doctor) }

it { should validate_uniqueness_of(:qualification_number) }
end
end

context 'enumerators' do
it { should define_enum_for(:employment).with_values(%w(full_time part_time intern contract)) }
end
end
13 changes: 13 additions & 0 deletions spec/models/patient_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'rails_helper'

describe Patient do
context 'validations' do
it { should validate_presence_of :first_name }
it { should validate_presence_of :last_name }
it { should validate_presence_of :age }
end

context 'enumerators' do
it { should define_enum_for(:gender).with_values(%w(male female other)) }
end
end