Browse Source

Add Lecture model

Frans Bergman 7 years ago
parent
commit
d520420634

+ 1 - 0
app/models/course.rb

@@ -11,6 +11,7 @@ class Course < ApplicationRecord
   has_many :course_participations, dependent: :destroy
   has_many :users, through: :course_participations
 
+  has_many :lectures
 
   private
     def date_order

+ 19 - 0
app/models/lecture.rb

@@ -0,0 +1,19 @@
+class Lecture < ApplicationRecord
+  belongs_to :course
+  validates :course_id, presence: true
+
+  validates :location, presence: true
+
+  validates :starts_at, presence: true
+  validates :ends_at, presence: true
+  validate :date_order
+
+  default_scope { order(starts_at: :asc) }
+
+  private
+    def date_order
+      if ends_at < starts_at
+        errors.add(:ends_on, "should come after starts_on")
+      end
+    end
+end

+ 13 - 0
db/migrate/20180317095809_create_lectures.rb

@@ -0,0 +1,13 @@
+class CreateLectures < ActiveRecord::Migration[5.1]
+  def change
+    create_table :lectures do |t|
+      t.belongs_to :course, foreign_key: true
+      t.datetime :starts_at
+      t.datetime :ends_at
+      t.text :description
+      t.string :location
+
+      t.timestamps
+    end
+  end
+end

+ 12 - 1
db/schema.rb

@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20180219151405) do
+ActiveRecord::Schema.define(version: 20180317095809) do
 
   create_table "administrations", force: :cascade do |t|
     t.integer "user_id"
@@ -58,6 +58,17 @@ ActiveRecord::Schema.define(version: 20180219151405) do
     t.index ["school_id"], name: "index_courses_on_school_id"
   end
 
+  create_table "lectures", force: :cascade do |t|
+    t.integer "course_id"
+    t.datetime "starts_at"
+    t.datetime "ends_at"
+    t.text "description"
+    t.string "location"
+    t.datetime "created_at", null: false
+    t.datetime "updated_at", null: false
+    t.index ["course_id"], name: "index_lectures_on_course_id"
+  end
+
   create_table "messages", force: :cascade do |t|
     t.integer "user_id"
     t.integer "conversation_id"

+ 9 - 0
db/seeds.rb

@@ -54,3 +54,12 @@ end
   CourseParticipation.create!(course: course, user: user, role: :teacher)
   CourseParticipation.create!(course: course, user: User.find(2), role: :student)
 end
+
+10.times do |n|
+  description = Faker::Lorem.sentence(10)
+  location = Faker::Address.street_address
+  course = Course.find(1)
+  start_at = (n + 1).hours.ago
+  end_at = n.hours.ago
+  Lecture.create(course: course, description: description, location: location, starts_at: start_at, ends_at: end_at)
+end

+ 15 - 0
test/fixtures/lectures.yml

@@ -0,0 +1,15 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+  course: one
+  starts_at: 2018-03-17 10:58:09
+  ends_at: 2018-03-17 10:58:09
+  description: MyText
+  location: MyString
+
+two:
+  course: two
+  starts_at: 2018-03-17 10:58:09
+  ends_at: 2018-03-17 10:58:09
+  description: MyText
+  location: MyString

+ 17 - 0
test/models/lecture_test.rb

@@ -0,0 +1,17 @@
+require 'test_helper'
+
+class LectureTest < ActiveSupport::TestCase
+  def setup
+    @lecture = lectures(:one)
+  end
+
+  test "should be valid" do
+    assert @lecture.valid?
+  end
+
+  test "dates should be in order" do
+    @lecture.starts_at = 1.hours.ago
+    @lecture.ends_at = 2.hours.ago
+    assert_not @lecture.valid?
+  end
+end