Skip to content

Commit d064486

Browse files
committed
data modeling
1 parent 5c984ff commit d064486

29 files changed

Lines changed: 348 additions & 6 deletions

app/models/academic_class.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class AcademicClass < ApplicationRecord
2+
belongs_to :term
3+
4+
has_and_belongs_to_many :faculties
5+
has_many :meeting_times, dependent: :destroy
6+
has_many :rooms, through: :meeting_times
7+
8+
enum :schedule_type, {
9+
lecture: 1, # LEC
10+
laboratory: 2 # LAB
11+
}
12+
13+
end

app/models/application_record.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
class ApplicationRecord < ActiveRecord::Base
22
primary_abstract_class
3+
has_paper_trail
34
end

app/models/building.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Building < ApplicationRecord
2+
has_many :rooms
3+
end

app/models/faculty.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Faculty < ApplicationRecord
2+
has_and_belongs_to_many :academic_classes
3+
4+
def full_name
5+
"#{first_name} #{last_name}"
6+
end
7+
8+
def initials
9+
"#{first_name[0]}#{last_name[0]}"
10+
end
11+
12+
def u_name
13+
def fwd
14+
"#{first_name[0]}. #{last_name}"
15+
end
16+
17+
def rev
18+
"#{last_name}, #{first_name[0]}."
19+
end
20+
end
21+
22+
23+
24+
end

app/models/meeting_time.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class MeetingTime < ApplicationRecord
2+
belongs_to :academic_class
3+
belongs_to :room
4+
has_one :building, through: :room
5+
6+
enum :meeting_schedule_type, {
7+
lecture: 1, # LEC
8+
laboratory: 2 # LAB
9+
}
10+
11+
enum :meeting_type, {
12+
class_meeting: 1 # CLAS
13+
}
14+
15+
end

app/models/room.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Room < ApplicationRecord
2+
belongs_to :building
3+
4+
def floor
5+
# get first digit of room number
6+
number.to_s[0].to_i
7+
end
8+
9+
end

app/models/term.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Term < ApplicationRecord
2+
before_create :check_term_validity
3+
has_many :academic_classes, dependent: :destroy
4+
5+
enum :semester, {
6+
spring: 1,
7+
fall: 2,
8+
summer: 3
9+
}
10+
11+
def name
12+
"#{semester.to_s.capitalize} #{year}"
13+
end
14+
15+
private
16+
17+
def check_term_validity
18+
unless [ 1, 2, 3 ].include?(semester)
19+
errors.add(:semester, "must be 1 (Spring), 2 (Fall), or 3 (Summer)")
20+
end
21+
end
22+
23+
end

app/models/user.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,19 @@ class User < ApplicationRecord
33
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
44
devise :database_authenticatable, :registerable,
55
:recoverable, :rememberable, :validatable
6+
7+
before_create :check_access_level
8+
9+
private
10+
11+
def full_name
12+
"#{first_name} #{last_name}"
13+
end
14+
15+
def check_access_level
16+
unless [0, 1, 2].include?(access_level)
17+
errors.add(:access_level, "must be 0 (Student), 1 (Admin), or 2 (Owner)")
18+
end
19+
end
20+
621
end
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
class AddFieldsToUsers < ActiveRecord::Migration[8.0]
22
def change
3-
add_column :users, :first_name, :string
4-
add_column :users, :last_name, :string
53
add_column :users, :access_level, :integer, default: 0, null: false
64
end
75
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class CreateTerms < ActiveRecord::Migration[8.0]
2+
def change
3+
create_table :terms do |t|
4+
5+
t.string :uid, null: false # e.g., "202610 (Fall 2025) or 202620 (202620)"
6+
t.integer :year, null: false # e.g., 2024
7+
t.integer :semester, null: false # e.g., 1 for Spring, 2 for Fall, 3 for Summer
8+
9+
t.timestamps
10+
end
11+
12+
add_index :terms, [:year, :semester], unique: true
13+
14+
end
15+
end

0 commit comments

Comments
 (0)