Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/assets/javascripts/cats.coffee
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/cats.scss
Original file line number Diff line number Diff line change
@@ -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/
5 changes: 5 additions & 0 deletions app/controllers/cats_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class CatsController < ApplicationController
def index
@cats = Cat.all
end
end
2 changes: 2 additions & 0 deletions app/helpers/cats_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CatsHelper
end
11 changes: 11 additions & 0 deletions app/models/cat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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 :owner, presence: true
validates :owner, length: { minimum: 3 }
validates :food, exclusion: { in: ["pineapple", "beer", "chocolate", "wine", "butter"] }

end
10 changes: 10 additions & 0 deletions app/views/cats/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<ol>
<%- @cats.each do |cat| %>
<li>
<strong><%= cat.name %>,</strong>
<span><%= cat.age %>,</span>
<span><%= cat.color %>,</span>
<span><%= cat.food %></span>
</li>
<%- end %>
</ol>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions db/migrate/20200114183254_create_cats.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions db/migrate/20200205190411_add_owner_to_cats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddOwnerToCats < ActiveRecord::Migration[5.2]
def change
add_column :cats, :owner, :string
end
end
25 changes: 25 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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_02_05_190411) 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
t.string "owner"
end

end
7 changes: 7 additions & 0 deletions test/controllers/cats_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class CatsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
31 changes: 31 additions & 0 deletions test/models/cat_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

require 'test_helper'

class CatTest < ActiveSupport::TestCase

test "should not save cat without name" do
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', 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

end