scaffold Check

This commit is contained in:
Jérémy Lecour 2021-12-18 16:14:50 +01:00 committed by Jérémy Lecour
parent 22a246e0e7
commit 61e7605921
19 changed files with 328 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,2 @@
module ChecksHelper
end

4
app/models/check.rb Normal file
View File

@ -0,0 +1,4 @@
class Check < ApplicationRecord
validates_presence_of :name
validates_presence_of :hostname
end

View File

@ -0,0 +1,22 @@
<div id="<%= dom_id check %>">
<p class="my-5">
<strong class="block font-medium mb-1">Name:</strong>
<%= check.name %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Description:</strong>
<%= check.description %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Hostname:</strong>
<%= check.hostname %>
</p>
<% 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" %>
<hr class="mt-6">
<% end %>
</div>

View File

@ -0,0 +1,2 @@
json.extract! check, :id, :name, :description, :hostname, :created_at, :updated_at
json.url check_url(check, format: :json)

View File

@ -0,0 +1,32 @@
<%= form_with(model: check, class: "contents") do |form| %>
<% if check.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<h2><%= pluralize(check.errors.count, "error") %> prohibited this check from being saved:</h2>
<ul>
<% check.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="my-5">
<%= 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" %>
</div>
<div class="my-5">
<%= 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" %>
</div>
<div class="my-5">
<%= 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" %>
</div>
<div class="inline">
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
</div>
<% end %>

View File

@ -0,0 +1,8 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">Editing check</h1>
<%= 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" %>
</div>

View File

@ -0,0 +1,14 @@
<div class="w-full">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<div class="flex justify-between items-center">
<h1 class="text-lg font-bold text-4xl">Checks</h1>
<%= link_to 'New check', new_check_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
</div>
<div id="checks" class="min-w-full">
<%= render @checks %>
</div>
</div>

View File

@ -0,0 +1 @@
json.array! @checks, partial: "checks/check", as: :check

View File

@ -0,0 +1,7 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="text-lg font-bold text-4xl">New check</h1>
<%= 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" %>
</div>

View File

@ -0,0 +1,15 @@
<div class="mx-auto md:w-2/3 w-full flex">
<div class="mx-auto">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% 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" %>
<div class="inline-block ml-2">
<%= button_to 'Destroy this check', check_path(@check), method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
</div>
<%= link_to 'Back to checks', checks_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>
</div>

View File

@ -0,0 +1 @@
json.partial! "checks/check", check: @check

View File

@ -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 ("/")

View File

@ -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

25
db/schema.rb generated Normal file
View File

@ -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

View File

@ -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

11
test/fixtures/checks.yml vendored Normal file
View File

@ -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

View File

@ -0,0 +1,7 @@
require "test_helper"
class CheckTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@ -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