diff --git a/app/models/item.rb b/app/models/item.rb new file mode 100644 index 0000000..e33eb18 --- /dev/null +++ b/app/models/item.rb @@ -0,0 +1,7 @@ +class Item < ApplicationRecord + # model association + belongs_to :todo + + # validation + validates_presence_of :name +end diff --git a/app/models/todo.rb b/app/models/todo.rb new file mode 100644 index 0000000..6b6956c --- /dev/null +++ b/app/models/todo.rb @@ -0,0 +1,7 @@ +class Todo < ApplicationRecord + # model association + has_many :items, dependent: :destroy + + # validations + validates_presence_of :title, :created_by +end diff --git a/db/migrate/20211110194428_create_todos.rb b/db/migrate/20211110194428_create_todos.rb new file mode 100644 index 0000000..be1f3b1 --- /dev/null +++ b/db/migrate/20211110194428_create_todos.rb @@ -0,0 +1,10 @@ +class CreateTodos < ActiveRecord::Migration[6.1] + def change + create_table :todos do |t| + t.string :title + t.string :created_by + + t.timestamps + end + end +end diff --git a/db/migrate/20211110194855_create_items.rb b/db/migrate/20211110194855_create_items.rb new file mode 100644 index 0000000..53fc718 --- /dev/null +++ b/db/migrate/20211110194855_create_items.rb @@ -0,0 +1,11 @@ +class CreateItems < ActiveRecord::Migration[6.1] + def change + create_table :items do |t| + t.string :name + t.boolean :done + t.references :todo, null: false, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..4635d0c --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,32 @@ +# 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. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 2021_11_10_194855) do + + create_table "items", force: :cascade do |t| + t.string "name" + t.boolean "done" + t.integer "todo_id", null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["todo_id"], name: "index_items_on_todo_id" + end + + create_table "todos", force: :cascade do |t| + t.string "title" + t.string "created_by" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + + add_foreign_key "items", "todos" +end diff --git a/spec/models/item_spec.rb b/spec/models/item_spec.rb new file mode 100644 index 0000000..7ecafdf --- /dev/null +++ b/spec/models/item_spec.rb @@ -0,0 +1,10 @@ +require 'rails_helper' + +RSpec.describe Item, type: :model do + # Association test + # ensure an item record belongs to a single todo record + it { should belong_to(:todo) } + # Validation test + # ensure column name is present before saving + it { should validate_presence_of(:name) } +end diff --git a/spec/models/todo_spec.rb b/spec/models/todo_spec.rb new file mode 100644 index 0000000..be399c4 --- /dev/null +++ b/spec/models/todo_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +RSpec.describe Todo, type: :model do + # Association test + # ensure Todo model has a 1:m relationship with the Item model + it { should have_many(:items).dependent(:destroy) } + # Validation tests + # ensure columns title and created_by are present before saving + it { should validate_presence_of(:title) } + it { should validate_presence_of(:created_by) } +end