Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #527 from omu/add-available-course-models
Browse files Browse the repository at this point in the history
Add available course models
  • Loading branch information
msdundar authored Nov 14, 2018
2 parents 2deda33 + cab8953 commit bb20a99
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/models/available_course.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class AvailableCourse < ApplicationRecord
# relations
belongs_to :academic_term
belongs_to :curriculum
belongs_to :course
has_many :groups, class_name: 'AvailableCourseGroup', dependent: :destroy
end
12 changes: 12 additions & 0 deletions app/models/available_course_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class AvailableCourseGroup < ApplicationRecord
# relations
belongs_to :available_course
has_many :lecturers, class_name: 'AvailableCourseLecturer', foreign_key: :group_id,
inverse_of: :group, dependent: :destroy

# validations
validates :name, presence: true, uniqueness: { scope: :available_course }
validates :quota, numericality: { only_integer: true, greater_than_or_equal_to: 1 }, allow_blank: true
end
13 changes: 13 additions & 0 deletions app/models/available_course_lecturer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class AvailableCourseLecturer < ApplicationRecord
# relations
belongs_to :group, class_name: 'AvailableCourseGroup'
belongs_to :lecturer, class_name: 'Employee'

# validations
validates :coordinator, presence: true, inclusion: { in: [true, false] }

# scopes
scope :coordinator, -> { where(coordinator: true) }
end
1 change: 1 addition & 0 deletions app/models/curriculum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Curriculum < ApplicationRecord
has_many :semesters, class_name: 'CurriculumSemester',
inverse_of: :curriculum, dependent: :destroy
has_many :courses, through: :semesters
has_many :available_courses, dependent: :destroy

# nested models
accepts_nested_attributes_for :semesters, reject_if: :all_blank, allow_destroy: true
Expand Down
1 change: 1 addition & 0 deletions app/models/employee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Employee < ApplicationRecord
has_many :units, through: :duties
has_many :positions, through: :duties
has_many :administrative_functions, through: :duties
has_many :available_course_lecturers, foreign_key: :lecturer_id, inverse_of: :lecturer, dependent: :destroy

# validations
validates :title_id, uniqueness: { scope: %i[user active] }
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20181112174827_create_available_courses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateAvailableCourses < ActiveRecord::Migration[5.2]
def change
create_table :available_courses do |t|
t.references :academic_term
t.references :curriculum
t.references :course
t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20181112175328_create_available_course_groups.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateAvailableCourseGroups < ActiveRecord::Migration[5.2]
def change
create_table :available_course_groups do |t|
t.string :name, null: false, limit: 50
t.integer :quota
t.references :available_course
t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20181112175343_create_available_course_lecturers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateAvailableCourseLecturers < ActiveRecord::Migration[5.2]
def change
create_table :available_course_lecturers do |t|
t.boolean :coordinator, default: false, null: false
t.references :group, foreign_key: { to_table: :available_course_groups }
t.references :lecturer, foreign_key: { to_table: :employees }
t.timestamps
end
end
end
32 changes: 32 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,36 @@
t.index ["user_id"], name: "index_articles_on_user_id"
end

create_table "available_course_groups", force: :cascade do |t|
t.string "name", limit: 50, null: false
t.integer "quota"
t.bigint "available_course_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["available_course_id"], name: "index_available_course_groups_on_available_course_id"
end

create_table "available_course_lecturers", force: :cascade do |t|
t.boolean "coordinator", default: false, null: false
t.bigint "group_id"
t.bigint "lecturer_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["group_id"], name: "index_available_course_lecturers_on_group_id"
t.index ["lecturer_id"], name: "index_available_course_lecturers_on_lecturer_id"
end

create_table "available_courses", force: :cascade do |t|
t.bigint "academic_term_id"
t.bigint "curriculum_id"
t.bigint "course_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["academic_term_id"], name: "index_available_courses_on_academic_term_id"
t.index ["course_id"], name: "index_available_courses_on_course_id"
t.index ["curriculum_id"], name: "index_available_courses_on_curriculum_id"
end

create_table "calendar_events", force: :cascade do |t|
t.datetime "start_date", null: false
t.datetime "end_date"
Expand Down Expand Up @@ -632,6 +662,8 @@
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

add_foreign_key "available_course_lecturers", "available_course_groups", column: "group_id"
add_foreign_key "available_course_lecturers", "employees", column: "lecturer_id"
add_foreign_key "calendar_title_types", "calendar_titles", column: "title_id"
add_foreign_key "calendar_title_types", "calendar_types", column: "type_id"
add_foreign_key "courses", "languages"
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/available_course_groups.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ati_group_1:
available_course: ati_fall_2017_2018
name: Group 1
quota: 10
8 changes: 8 additions & 0 deletions test/fixtures/available_course_lecturers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ati_group_1_lecturer_john:
group: ati_group_1
lecturer: chief_john
coordinator: true
ati_group_1_lecturer_serhat:
group: ati_group_1
lecturer: serhat_active
coordinator: false
4 changes: 4 additions & 0 deletions test/fixtures/available_courses.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ati_fall_2017_2018:
academic_term: fall_2017_2018
curriculum: one
course: ati
29 changes: 29 additions & 0 deletions test/models/course/available_course_group_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'test_helper'

class AvailableCourseGroupTest < ActiveSupport::TestCase
# relations
%i[
available_course
lecturers
].each do |property|
test "a available_course_group can communicate with #{property}" do
assert available_course_groups(:ati_group_1).send(property)
end
end

# validations: presence
test 'should not save available_course_group without name' do
available_course_groups(:ati_group_1).name = nil
assert_not available_course_groups(:ati_group_1).valid?
assert_not_empty available_course_groups(:ati_group_1).errors[:name]
end

# validations: uniqueness
test 'name should be unique' do
fake = available_course_groups(:ati_group_1).dup
assert_not fake.valid?
assert_not_empty fake.errors[:name]
end
end
29 changes: 29 additions & 0 deletions test/models/course/available_course_lecturer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'test_helper'

class AvailableCourseLecturerTest < ActiveSupport::TestCase
# relations
%i[
group
lecturer
].each do |property|
test "a available_course_lecturer can communicate with #{property}" do
assert available_course_lecturers(:ati_group_1_lecturer_john).send(property)
end
end

# validations: presence
test 'should not save available_course_group without coordinator info' do
available_course_lecturers(:ati_group_1_lecturer_john).coordinator = nil
assert_not available_course_lecturers(:ati_group_1_lecturer_john).valid?
assert_not_empty available_course_lecturers(:ati_group_1_lecturer_john).errors[:coordinator]
end

# scopes
test 'coordinator scope returns coordinator lecturers' do
lecturers = available_course_groups(:ati_group_1).lecturers
assert lecturers.coordinator.to_a.include?(available_course_lecturers(:ati_group_1_lecturer_john))
assert_not lecturers.coordinator.to_a.include?(available_course_lecturers(:ati_group_1_lecturer_serhat))
end
end
17 changes: 17 additions & 0 deletions test/models/course/available_course_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require 'test_helper'

class AvailableCourseTest < ActiveSupport::TestCase
# relations
%i[
academic_term
curriculum
course
groups
].each do |property|
test "a available_course can communicate with #{property}" do
assert available_courses(:ati_fall_2017_2018).send(property)
end
end
end
1 change: 1 addition & 0 deletions test/models/course/curriculum_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class CurriculumTest < ActiveSupport::TestCase
programs
semesters
courses
available_courses
].each do |relation|
test "curriculum can communicate with #{relation}" do
assert @curriculum.send(relation)
Expand Down
1 change: 1 addition & 0 deletions test/models/employee/employee_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class EmployeeTest < ActiveSupport::TestCase
units
positions
administrative_functions
available_course_lecturers
].each do |property|
test "an employee can communicate with #{property}" do
assert employees(:serhat_active).send(property)
Expand Down

0 comments on commit bb20a99

Please sign in to comment.