diff --git a/app/controllers/checks_controller.rb b/app/controllers/checks_controller.rb new file mode 100644 index 0000000..9bd4f1d --- /dev/null +++ b/app/controllers/checks_controller.rb @@ -0,0 +1,70 @@ +class ChecksController < ApplicationController + before_action :set_check, only: %i[ show edit update destroy ] + + # GET /checks or /checks.json + def index + @checks = Check.all + end + + # GET /checks/1 or /checks/1.json + def show + end + + # GET /checks/new + def new + @check = Check.new + end + + # GET /checks/1/edit + def edit + end + + # POST /checks or /checks.json + def create + @check = Check.new(check_params) + + respond_to do |format| + if @check.save + format.html { redirect_to check_url(@check), notice: "Check was successfully created." } + format.json { render :show, status: :created, location: @check } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @check.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /checks/1 or /checks/1.json + def update + respond_to do |format| + if @check.update(check_params) + format.html { redirect_to check_url(@check), notice: "Check was successfully updated." } + format.json { render :show, status: :ok, location: @check } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @check.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /checks/1 or /checks/1.json + def destroy + @check.destroy + + respond_to do |format| + format.html { redirect_to checks_url, notice: "Check was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_check + @check = Check.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def check_params + params.require(:check).permit(:name, :description, :hostname) + end +end diff --git a/app/helpers/checks_helper.rb b/app/helpers/checks_helper.rb new file mode 100644 index 0000000..ec8edd7 --- /dev/null +++ b/app/helpers/checks_helper.rb @@ -0,0 +1,2 @@ +module ChecksHelper +end diff --git a/app/models/check.rb b/app/models/check.rb new file mode 100644 index 0000000..5e8a714 --- /dev/null +++ b/app/models/check.rb @@ -0,0 +1,4 @@ +class Check < ApplicationRecord + validates_presence_of :name + validates_presence_of :hostname +end diff --git a/app/views/checks/_check.html.erb b/app/views/checks/_check.html.erb new file mode 100644 index 0000000..be03310 --- /dev/null +++ b/app/views/checks/_check.html.erb @@ -0,0 +1,22 @@ +
+

+ Name: + <%= check.name %> +

+ +

+ Description: + <%= check.description %> +

+ +

+ Hostname: + <%= check.hostname %> +

+ + <% if action_name != "show" %> + <%= link_to "Show this check", check, class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> + <%= link_to 'Edit this check', edit_check_path(check), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %> +
+ <% end %> +
diff --git a/app/views/checks/_check.json.jbuilder b/app/views/checks/_check.json.jbuilder new file mode 100644 index 0000000..e4b0c14 --- /dev/null +++ b/app/views/checks/_check.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! check, :id, :name, :description, :hostname, :created_at, :updated_at +json.url check_url(check, format: :json) diff --git a/app/views/checks/_form.html.erb b/app/views/checks/_form.html.erb new file mode 100644 index 0000000..827dc18 --- /dev/null +++ b/app/views/checks/_form.html.erb @@ -0,0 +1,32 @@ +<%= form_with(model: check, class: "contents") do |form| %> + <% if check.errors.any? %> +
+

<%= pluralize(check.errors.count, "error") %> prohibited this check from being saved:

+ + +
+ <% end %> + +
+ <%= form.label :name %> + <%= form.text_field :name, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %> +
+ +
+ <%= form.label :description %> + <%= form.text_area :description, rows: 4, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %> +
+ +
+ <%= form.label :hostname %> + <%= form.text_field :hostname, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %> +
+ +
+ <%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %> +
+<% end %> diff --git a/app/views/checks/edit.html.erb b/app/views/checks/edit.html.erb new file mode 100644 index 0000000..acc169d --- /dev/null +++ b/app/views/checks/edit.html.erb @@ -0,0 +1,8 @@ +
+

Editing check

+ + <%= render "form", check: @check %> + + <%= link_to "Show this check", @check, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> + <%= link_to "Back to checks", checks_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> +
diff --git a/app/views/checks/index.html.erb b/app/views/checks/index.html.erb new file mode 100644 index 0000000..aea7905 --- /dev/null +++ b/app/views/checks/index.html.erb @@ -0,0 +1,14 @@ +
+ <% if notice.present? %> +

<%= notice %>

+ <% end %> + +
+

Checks

+ <%= link_to 'New check', new_check_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %> +
+ +
+ <%= render @checks %> +
+
diff --git a/app/views/checks/index.json.jbuilder b/app/views/checks/index.json.jbuilder new file mode 100644 index 0000000..fb8a917 --- /dev/null +++ b/app/views/checks/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @checks, partial: "checks/check", as: :check diff --git a/app/views/checks/new.html.erb b/app/views/checks/new.html.erb new file mode 100644 index 0000000..f3b7d9a --- /dev/null +++ b/app/views/checks/new.html.erb @@ -0,0 +1,7 @@ +
+

New check

+ + <%= render "form", check: @check %> + + <%= link_to 'Back to checks', checks_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> +
diff --git a/app/views/checks/show.html.erb b/app/views/checks/show.html.erb new file mode 100644 index 0000000..cf7fea3 --- /dev/null +++ b/app/views/checks/show.html.erb @@ -0,0 +1,15 @@ +
+
+ <% if notice.present? %> +

<%= notice %>

+ <% end %> + + <%= render @check %> + + <%= link_to 'Edit this check', edit_check_path(@check), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> +
+ <%= button_to 'Destroy this check', check_path(@check), method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %> +
+ <%= link_to 'Back to checks', checks_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> +
+
diff --git a/app/views/checks/show.json.jbuilder b/app/views/checks/show.json.jbuilder new file mode 100644 index 0000000..bd47a45 --- /dev/null +++ b/app/views/checks/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "checks/check", check: @check diff --git a/config/routes.rb b/config/routes.rb index 262ffd5..7e279b4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :checks # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Defines the root path route ("/") diff --git a/db/migrate/20211218150535_create_checks.rb b/db/migrate/20211218150535_create_checks.rb new file mode 100644 index 0000000..2e2b7b6 --- /dev/null +++ b/db/migrate/20211218150535_create_checks.rb @@ -0,0 +1,13 @@ +class CreateChecks < ActiveRecord::Migration[7.0] + def change + create_table :checks do |t| + t.string :name + t.text :description + t.string :hostname + + t.timestamps + end + add_index :checks, :name + add_index :checks, :hostname + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..6f3daa4 --- /dev/null +++ b/db/schema.rb @@ -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. +# +# 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_12_18_150535) do + + create_table "checks", force: :cascade do |t| + t.string "name" + t.text "description" + t.string "hostname" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["hostname"], name: "index_checks_on_hostname" + t.index ["name"], name: "index_checks_on_name" + end + +end diff --git a/test/controllers/checks_controller_test.rb b/test/controllers/checks_controller_test.rb new file mode 100644 index 0000000..a8eacd0 --- /dev/null +++ b/test/controllers/checks_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class ChecksControllerTest < ActionDispatch::IntegrationTest + setup do + @check = checks(:one) + end + + test "should get index" do + get checks_url + assert_response :success + end + + test "should get new" do + get new_check_url + assert_response :success + end + + test "should create check" do + assert_difference("Check.count") do + post checks_url, params: { check: { description: @check.description, hostname: @check.hostname, name: @check.name } } + end + + assert_redirected_to check_url(Check.last) + end + + test "should show check" do + get check_url(@check) + assert_response :success + end + + test "should get edit" do + get edit_check_url(@check) + assert_response :success + end + + test "should update check" do + patch check_url(@check), params: { check: { description: @check.description, hostname: @check.hostname, name: @check.name } } + assert_redirected_to check_url(@check) + end + + test "should destroy check" do + assert_difference("Check.count", -1) do + delete check_url(@check) + end + + assert_redirected_to checks_url + end +end diff --git a/test/fixtures/checks.yml b/test/fixtures/checks.yml new file mode 100644 index 0000000..f4be04e --- /dev/null +++ b/test/fixtures/checks.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + description: MyText + hostname: MyString + +two: + name: MyString + description: MyText + hostname: MyString diff --git a/test/models/check_test.rb b/test/models/check_test.rb new file mode 100644 index 0000000..5b7377c --- /dev/null +++ b/test/models/check_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class CheckTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/system/checks_test.rb b/test/system/checks_test.rb new file mode 100644 index 0000000..a7bb2d8 --- /dev/null +++ b/test/system/checks_test.rb @@ -0,0 +1,45 @@ +require "application_system_test_case" + +class ChecksTest < ApplicationSystemTestCase + setup do + @check = checks(:one) + end + + test "visiting the index" do + visit checks_url + assert_selector "h1", text: "Checks" + end + + test "should create check" do + visit checks_url + click_on "New check" + + fill_in "Description", with: @check.description + fill_in "Hostname", with: @check.hostname + fill_in "Name", with: @check.name + click_on "Create Check" + + assert_text "Check was successfully created" + click_on "Back" + end + + test "should update Check" do + visit check_url(@check) + click_on "Edit this check", match: :first + + fill_in "Description", with: @check.description + fill_in "Hostname", with: @check.hostname + fill_in "Name", with: @check.name + click_on "Update Check" + + assert_text "Check was successfully updated" + click_on "Back" + end + + test "should destroy Check" do + visit check_url(@check) + click_on "Destroy this check", match: :first + + assert_text "Check was successfully destroyed" + end +end