Forráskód Böngészése

Create Assignment model

Frans Bergman 7 éve
szülő
commit
1b0537cb8b

+ 10 - 0
app/models/assignment.rb

@@ -0,0 +1,10 @@
+class Assignment < ApplicationRecord
+  belongs_to :course
+  validates :course_id, presence: true
+
+  validates :name, presence: true, length: { maximum: 255 }
+
+  validates :due_at, presence: true
+
+  default_scope { order(due_at: :asc) }
+end

+ 1 - 0
app/models/course.rb

@@ -12,6 +12,7 @@ class Course < ApplicationRecord
   has_many :users, through: :course_participations
 
   has_many :lectures
+  has_many :assignments
 
   private
     def date_order

+ 2 - 0
app/models/user.rb

@@ -42,6 +42,8 @@ class User < ApplicationRecord
   has_many :course_participations
   has_many :courses, through: :course_participations
 
+  has_many :assignments, through: :courses
+
   # Returns the hash digest of the given string.
   def User.digest(string)
     cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :

+ 12 - 0
db/migrate/20180321081841_create_assignments.rb

@@ -0,0 +1,12 @@
+class CreateAssignments < ActiveRecord::Migration[5.1]
+  def change
+    create_table :assignments do |t|
+      t.belongs_to :course, foreign_key: true
+      t.datetime :due_at
+      t.string :name
+      t.text :description
+
+      t.timestamps
+    end
+  end
+end

+ 11 - 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: 20180317095809) do
+ActiveRecord::Schema.define(version: 20180321081841) do
 
   create_table "administrations", force: :cascade do |t|
     t.integer "user_id"
@@ -22,6 +22,16 @@ ActiveRecord::Schema.define(version: 20180317095809) do
     t.index ["user_id"], name: "index_administrations_on_user_id"
   end
 
+  create_table "assignments", force: :cascade do |t|
+    t.integer "course_id"
+    t.datetime "due_at"
+    t.string "name"
+    t.text "description"
+    t.datetime "created_at", null: false
+    t.datetime "updated_at", null: false
+    t.index ["course_id"], name: "index_assignments_on_course_id"
+  end
+
   create_table "conversation_participations", force: :cascade do |t|
     t.integer "user_id"
     t.integer "conversation_id"

+ 8 - 0
db/seeds.rb

@@ -63,3 +63,11 @@ end
   end_at = n.hours.ago
   Lecture.create(course: course, description: description, location: location, starts_at: start_at, ends_at: end_at)
 end
+
+10.times do |n|
+  description = Faker::Lorem.paragraph
+  name = Faker::Lorem.sentence(2)
+  course = Course.find(Faker::Number.between(1, 10))
+  due_at = Faker::Number.between(1, 10).days.since
+  Assignment.create(course: course, name: name, description: description, due_at: due_at)
+end

+ 13 - 0
test/fixtures/assignments.yml

@@ -0,0 +1,13 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+  course: one
+  due_at: 2018-03-21 09:18:41
+  name: MyString
+  description: MyText
+
+two:
+  course: two
+  due_at: 2018-03-21 09:18:41
+  name: MyString
+  description: MyText

+ 16 - 0
test/models/assignment_test.rb

@@ -0,0 +1,16 @@
+require 'test_helper'
+
+class AssignmentTest < ActiveSupport::TestCase
+  def setup
+    @assignment = assignments(:one)
+  end
+
+  test "should be valid" do
+    assert @assignment.valid?
+  end
+
+  test "date should be present" do
+    @assignment.due_at = nil
+    assert_not @assignment.valid?
+  end
+end