scaffold Check
This commit is contained in:
parent
22a246e0e7
commit
61e7605921
70
app/controllers/checks_controller.rb
Normal file
70
app/controllers/checks_controller.rb
Normal 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
|
2
app/helpers/checks_helper.rb
Normal file
2
app/helpers/checks_helper.rb
Normal file
|
@ -0,0 +1,2 @@
|
|||
module ChecksHelper
|
||||
end
|
4
app/models/check.rb
Normal file
4
app/models/check.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
class Check < ApplicationRecord
|
||||
validates_presence_of :name
|
||||
validates_presence_of :hostname
|
||||
end
|
22
app/views/checks/_check.html.erb
Normal file
22
app/views/checks/_check.html.erb
Normal 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>
|
2
app/views/checks/_check.json.jbuilder
Normal file
2
app/views/checks/_check.json.jbuilder
Normal file
|
@ -0,0 +1,2 @@
|
|||
json.extract! check, :id, :name, :description, :hostname, :created_at, :updated_at
|
||||
json.url check_url(check, format: :json)
|
32
app/views/checks/_form.html.erb
Normal file
32
app/views/checks/_form.html.erb
Normal 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 %>
|
8
app/views/checks/edit.html.erb
Normal file
8
app/views/checks/edit.html.erb
Normal 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>
|
14
app/views/checks/index.html.erb
Normal file
14
app/views/checks/index.html.erb
Normal 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>
|
1
app/views/checks/index.json.jbuilder
Normal file
1
app/views/checks/index.json.jbuilder
Normal file
|
@ -0,0 +1 @@
|
|||
json.array! @checks, partial: "checks/check", as: :check
|
7
app/views/checks/new.html.erb
Normal file
7
app/views/checks/new.html.erb
Normal 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>
|
15
app/views/checks/show.html.erb
Normal file
15
app/views/checks/show.html.erb
Normal 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>
|
1
app/views/checks/show.json.jbuilder
Normal file
1
app/views/checks/show.json.jbuilder
Normal file
|
@ -0,0 +1 @@
|
|||
json.partial! "checks/check", check: @check
|
|
@ -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 ("/")
|
||||
|
|
13
db/migrate/20211218150535_create_checks.rb
Normal file
13
db/migrate/20211218150535_create_checks.rb
Normal 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
25
db/schema.rb
generated
Normal 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
|
48
test/controllers/checks_controller_test.rb
Normal file
48
test/controllers/checks_controller_test.rb
Normal 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
11
test/fixtures/checks.yml
vendored
Normal 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
|
7
test/models/check_test.rb
Normal file
7
test/models/check_test.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
require "test_helper"
|
||||
|
||||
class CheckTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
45
test/system/checks_test.rb
Normal file
45
test/system/checks_test.rb
Normal 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
|
Loading…
Reference in a new issue