From 8cdc817ee42f30a828ef87d4ab673e1a87b44f94 Mon Sep 17 00:00:00 2001 From: Gloritz Date: Tue, 7 Jan 2020 20:44:58 +0100 Subject: [PATCH 1/6] cat index page --- app/assets/javascripts/cats.coffee | 3 +++ app/assets/stylesheets/cats.scss | 3 +++ app/controllers/cats_controller.rb | 5 +++++ app/helpers/cats_helper.rb | 2 ++ app/views/cats/index.html.erb | 5 +++++ config/routes.rb | 1 + test/controllers/cats_controller_test.rb | 7 +++++++ 7 files changed, 26 insertions(+) create mode 100644 app/assets/javascripts/cats.coffee create mode 100644 app/assets/stylesheets/cats.scss create mode 100644 app/controllers/cats_controller.rb create mode 100644 app/helpers/cats_helper.rb create mode 100644 app/views/cats/index.html.erb create mode 100644 test/controllers/cats_controller_test.rb diff --git a/app/assets/javascripts/cats.coffee b/app/assets/javascripts/cats.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/cats.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/cats.scss b/app/assets/stylesheets/cats.scss new file mode 100644 index 0000000..3b00fa0 --- /dev/null +++ b/app/assets/stylesheets/cats.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the cats controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/cats_controller.rb b/app/controllers/cats_controller.rb new file mode 100644 index 0000000..a281cd2 --- /dev/null +++ b/app/controllers/cats_controller.rb @@ -0,0 +1,5 @@ +class CatsController < ApplicationController + def index + @cats = ["Carlo", "Merle", "Puppy", "Tarzan", "Mausi"] + end +end diff --git a/app/helpers/cats_helper.rb b/app/helpers/cats_helper.rb new file mode 100644 index 0000000..cadafa9 --- /dev/null +++ b/app/helpers/cats_helper.rb @@ -0,0 +1,2 @@ +module CatsHelper +end diff --git a/app/views/cats/index.html.erb b/app/views/cats/index.html.erb new file mode 100644 index 0000000..0f71111 --- /dev/null +++ b/app/views/cats/index.html.erb @@ -0,0 +1,5 @@ +
    +<%- @cats.each do |cat| %> +
  1. <%= cat %>
  2. +<%- end %> +
diff --git a/config/routes.rb b/config/routes.rb index 787824f..9bf9bd8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,4 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + resources :cats end diff --git a/test/controllers/cats_controller_test.rb b/test/controllers/cats_controller_test.rb new file mode 100644 index 0000000..386a2ae --- /dev/null +++ b/test/controllers/cats_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class CatsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end From 8cc52ffc3c4bd450baf83979c50391a2cfe78f3f Mon Sep 17 00:00:00 2001 From: Gloritz Date: Tue, 14 Jan 2020 19:47:55 +0100 Subject: [PATCH 2/6] add cat model and db table --- app/models/cat.rb | 2 ++ db/migrate/20200114183254_create_cats.rb | 12 ++++++++++++ db/schema.rb | 24 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 app/models/cat.rb create mode 100644 db/migrate/20200114183254_create_cats.rb create mode 100644 db/schema.rb diff --git a/app/models/cat.rb b/app/models/cat.rb new file mode 100644 index 0000000..de58804 --- /dev/null +++ b/app/models/cat.rb @@ -0,0 +1,2 @@ +class Cat < ApplicationRecord +end diff --git a/db/migrate/20200114183254_create_cats.rb b/db/migrate/20200114183254_create_cats.rb new file mode 100644 index 0000000..3e59e03 --- /dev/null +++ b/db/migrate/20200114183254_create_cats.rb @@ -0,0 +1,12 @@ +class CreateCats < ActiveRecord::Migration[5.2] + def change + create_table :cats do |t| + t.string :name + t.integer :age + t.string :color + t.text :food + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..8863930 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,24 @@ +# 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. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 2020_01_14_183254) do + + create_table "cats", force: :cascade do |t| + t.string "name" + t.integer "age" + t.string "color" + t.text "food" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end From defc86bb597b0264c4be73dc2d52df9271a20a81 Mon Sep 17 00:00:00 2001 From: Gloritz Date: Tue, 14 Jan 2020 21:04:44 +0100 Subject: [PATCH 3/6] show database in the view --- app/controllers/cats_controller.rb | 2 +- app/views/cats/index.html.erb | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/controllers/cats_controller.rb b/app/controllers/cats_controller.rb index a281cd2..9766022 100644 --- a/app/controllers/cats_controller.rb +++ b/app/controllers/cats_controller.rb @@ -1,5 +1,5 @@ class CatsController < ApplicationController def index - @cats = ["Carlo", "Merle", "Puppy", "Tarzan", "Mausi"] + @cats = Cat.all end end diff --git a/app/views/cats/index.html.erb b/app/views/cats/index.html.erb index 0f71111..753be6a 100644 --- a/app/views/cats/index.html.erb +++ b/app/views/cats/index.html.erb @@ -1,5 +1,10 @@
    <%- @cats.each do |cat| %> -
  1. <%= cat %>
  2. +
  3. + <%= cat.name %>, + <%= cat.age %>, + <%= cat.color %>, + <%= cat.food %> +
  4. <%- end %>
From aab51173663a31d33d8d07173eee5247d3a3cd26 Mon Sep 17 00:00:00 2001 From: Gloritz Date: Tue, 21 Jan 2020 18:55:52 +0100 Subject: [PATCH 4/6] correct the style --- app/views/cats/index.html.erb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/views/cats/index.html.erb b/app/views/cats/index.html.erb index 753be6a..7c386f3 100644 --- a/app/views/cats/index.html.erb +++ b/app/views/cats/index.html.erb @@ -1,10 +1,10 @@
    -<%- @cats.each do |cat| %> -
  1. - <%= cat.name %>, - <%= cat.age %>, - <%= cat.color %>, - <%= cat.food %> -
  2. -<%- end %> + <%- @cats.each do |cat| %> +
  3. + <%= cat.name %>, + <%= cat.age %>, + <%= cat.color %>, + <%= cat.food %> +
  4. + <%- end %>
From c062e35cd350af604203a8ba3fb350d1f7813fc3 Mon Sep 17 00:00:00 2001 From: Gloritz Date: Tue, 21 Jan 2020 20:40:58 +0100 Subject: [PATCH 5/6] add validations & tests --- app/models/cat.rb | 7 +++++++ test/models/cat_test.rb | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 test/models/cat_test.rb diff --git a/app/models/cat.rb b/app/models/cat.rb index de58804..57790d6 100644 --- a/app/models/cat.rb +++ b/app/models/cat.rb @@ -1,2 +1,9 @@ class Cat < ApplicationRecord + + validates :name, :age, presence: true + validates :name, uniqueness: true + validates :age, inclusion: { in: (1..10) } + validates :color, inclusion: { in: ["brown", "tiger", "black", "white", "pink"] }, allow_nil: true + validates :food, exclusion: { in: ["pineapple", "beer", "chocolate", "wine", "butter"] } + end diff --git a/test/models/cat_test.rb b/test/models/cat_test.rb new file mode 100644 index 0000000..4f1f100 --- /dev/null +++ b/test/models/cat_test.rb @@ -0,0 +1,16 @@ + +require 'test_helper' + +class CatTest < ActiveSupport::TestCase + + test "should not save cat without name" do + cat = Cat.new + assert_not cat.save + end + + test "should not save cat without age" do + cat = Cat.new(name:'m') + assert_not cat.save + end + +end From a24726438d639e1702ba801797c4980f460ce654 Mon Sep 17 00:00:00 2001 From: Gloritz Date: Wed, 5 Feb 2020 20:49:51 +0100 Subject: [PATCH 6/6] add owner column and test and validations for owner --- app/models/cat.rb | 2 ++ .../20200205190411_add_owner_to_cats.rb | 5 +++++ db/schema.rb | 3 ++- test/models/cat_test.rb | 19 +++++++++++++++++-- 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20200205190411_add_owner_to_cats.rb diff --git a/app/models/cat.rb b/app/models/cat.rb index 57790d6..628f8be 100644 --- a/app/models/cat.rb +++ b/app/models/cat.rb @@ -4,6 +4,8 @@ class Cat < ApplicationRecord validates :name, uniqueness: true validates :age, inclusion: { in: (1..10) } validates :color, inclusion: { in: ["brown", "tiger", "black", "white", "pink"] }, allow_nil: true + validates :owner, presence: true + validates :owner, length: { minimum: 3 } validates :food, exclusion: { in: ["pineapple", "beer", "chocolate", "wine", "butter"] } end diff --git a/db/migrate/20200205190411_add_owner_to_cats.rb b/db/migrate/20200205190411_add_owner_to_cats.rb new file mode 100644 index 0000000..9f2def4 --- /dev/null +++ b/db/migrate/20200205190411_add_owner_to_cats.rb @@ -0,0 +1,5 @@ +class AddOwnerToCats < ActiveRecord::Migration[5.2] + def change + add_column :cats, :owner, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 8863930..2b9ac0d 100644 --- a/db/schema.rb +++ b/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: 2020_01_14_183254) do +ActiveRecord::Schema.define(version: 2020_02_05_190411) do create_table "cats", force: :cascade do |t| t.string "name" @@ -19,6 +19,7 @@ t.text "food" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "owner" end end diff --git a/test/models/cat_test.rb b/test/models/cat_test.rb index 4f1f100..666bba8 100644 --- a/test/models/cat_test.rb +++ b/test/models/cat_test.rb @@ -4,12 +4,27 @@ class CatTest < ActiveSupport::TestCase test "should not save cat without name" do - cat = Cat.new + cat = Cat.new(owner:'gloria', age: 8) assert_not cat.save end test "should not save cat without age" do - cat = Cat.new(name:'m') + cat = Cat.new(name:'m', owner:'gloria') + assert_not cat.save + end + + test "should not save cat if the color is blue" do + cat = Cat.new(name:'n', age: 8, owner:'gloria', color:'blue') + assert_not cat.save + end + + test "should not save cat without owner" do + cat = Cat.new(name:'o', age: 8) + assert_not cat.save + end + + test "should not save cat with an owner without 3 charaters" do + cat = Cat.new(name:'p', age: 8, owner:'mi') assert_not cat.save end