From 45552da94f56dda83fc3bf68c6278cf88da07743 Mon Sep 17 00:00:00 2001 From: Masaki Suketa Date: Sat, 6 Jul 2019 15:54:27 +0900 Subject: [PATCH] try051 Support default expression and expression indexes for MySQL https://github.com/rails/rails/pull/34307 --- app/assets/stylesheets/hosts.scss | 3 + app/assets/stylesheets/scaffolds.scss | 84 +++++++++++++++++++++++ app/controllers/hosts_controller.rb | 75 ++++++++++++++++++++ app/helpers/hosts_helper.rb | 2 + app/models/host.rb | 2 + app/views/hosts/_form.html.erb | 27 ++++++++ app/views/hosts/_host.json.jbuilder | 2 + app/views/hosts/edit.html.erb | 6 ++ app/views/hosts/index.html.erb | 29 ++++++++ app/views/hosts/index.json.jbuilder | 1 + app/views/hosts/new.html.erb | 5 ++ app/views/hosts/show.html.erb | 14 ++++ app/views/hosts/show.json.jbuilder | 1 + config/routes.rb | 1 + db/migrate/20190706063437_create_hosts.rb | 10 +++ db/schema.rb | 22 ++++++ db/seeds.rb | 18 +++-- test/controllers/hosts_controller_test.rb | 48 +++++++++++++ test/fixtures/hosts.yml | 9 +++ test/models/host_test.rb | 7 ++ test/system/hosts_test.rb | 45 ++++++++++++ 21 files changed, 404 insertions(+), 7 deletions(-) create mode 100644 app/assets/stylesheets/hosts.scss create mode 100644 app/assets/stylesheets/scaffolds.scss create mode 100644 app/controllers/hosts_controller.rb create mode 100644 app/helpers/hosts_helper.rb create mode 100644 app/models/host.rb create mode 100644 app/views/hosts/_form.html.erb create mode 100644 app/views/hosts/_host.json.jbuilder create mode 100644 app/views/hosts/edit.html.erb create mode 100644 app/views/hosts/index.html.erb create mode 100644 app/views/hosts/index.json.jbuilder create mode 100644 app/views/hosts/new.html.erb create mode 100644 app/views/hosts/show.html.erb create mode 100644 app/views/hosts/show.json.jbuilder create mode 100644 db/migrate/20190706063437_create_hosts.rb create mode 100644 db/schema.rb create mode 100644 test/controllers/hosts_controller_test.rb create mode 100644 test/fixtures/hosts.yml create mode 100644 test/models/host_test.rb create mode 100644 test/system/hosts_test.rb diff --git a/app/assets/stylesheets/hosts.scss b/app/assets/stylesheets/hosts.scss new file mode 100644 index 0000000..90133d4 --- /dev/null +++ b/app/assets/stylesheets/hosts.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the hosts controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/scaffolds.scss b/app/assets/stylesheets/scaffolds.scss new file mode 100644 index 0000000..6045188 --- /dev/null +++ b/app/assets/stylesheets/scaffolds.scss @@ -0,0 +1,84 @@ +body { + background-color: #fff; + color: #333; + margin: 33px; + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { + color: #000; + + &:visited { + color: #666; + } + + &:hover { + color: #fff; + background-color: #000; + } +} + +th { + padding-bottom: 5px; +} + +td { + padding: 0 5px 7px; +} + +div { + &.field, &.actions { + margin-bottom: 10px; + } +} + +#notice { + color: green; +} + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; +} + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px 7px 0; + margin-bottom: 20px; + background-color: #f0f0f0; + + h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px -7px 0; + background-color: #c00; + color: #fff; + } + + ul li { + font-size: 12px; + list-style: square; + } +} + +label { + display: block; +} diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb new file mode 100644 index 0000000..89ae0fb --- /dev/null +++ b/app/controllers/hosts_controller.rb @@ -0,0 +1,75 @@ +class HostsController < ApplicationController + before_action :set_host, only: [:show, :edit, :update, :destroy] + + # GET /hosts + # GET /hosts.json + def index + @hosts = Host.all.order(:aton) + # @hosts = Host.all.order(:ip) + end + + # GET /hosts/1 + # GET /hosts/1.json + def show + end + + # GET /hosts/new + def new + @host = Host.new + end + + # GET /hosts/1/edit + def edit + end + + # POST /hosts + # POST /hosts.json + def create + @host = Host.new(host_params) + + respond_to do |format| + if @host.save + format.html { redirect_to @host, notice: 'Host was successfully created.' } + format.json { render :show, status: :created, location: @host } + else + format.html { render :new } + format.json { render json: @host.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /hosts/1 + # PATCH/PUT /hosts/1.json + def update + respond_to do |format| + if @host.update(host_params) + format.html { redirect_to @host, notice: 'Host was successfully updated.' } + format.json { render :show, status: :ok, location: @host } + else + format.html { render :edit } + format.json { render json: @host.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /hosts/1 + # DELETE /hosts/1.json + def destroy + @host.destroy + respond_to do |format| + format.html { redirect_to hosts_url, notice: 'Host was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_host + @host = Host.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def host_params + params.require(:host).permit(:ip, :aton) + end +end diff --git a/app/helpers/hosts_helper.rb b/app/helpers/hosts_helper.rb new file mode 100644 index 0000000..998691e --- /dev/null +++ b/app/helpers/hosts_helper.rb @@ -0,0 +1,2 @@ +module HostsHelper +end diff --git a/app/models/host.rb b/app/models/host.rb new file mode 100644 index 0000000..da76317 --- /dev/null +++ b/app/models/host.rb @@ -0,0 +1,2 @@ +class Host < ApplicationRecord +end diff --git a/app/views/hosts/_form.html.erb b/app/views/hosts/_form.html.erb new file mode 100644 index 0000000..d028b14 --- /dev/null +++ b/app/views/hosts/_form.html.erb @@ -0,0 +1,27 @@ +<%= form_with(model: host, local: true) do |form| %> + <% if host.errors.any? %> +
+

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

+ + +
+ <% end %> + +
+ <%= form.label :ip %> + <%= form.text_field :ip %> +
+ +
+ <%= form.label :aton %> + <%= form.number_field :aton %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/hosts/_host.json.jbuilder b/app/views/hosts/_host.json.jbuilder new file mode 100644 index 0000000..6171205 --- /dev/null +++ b/app/views/hosts/_host.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! host, :id, :ip, :aton, :created_at, :updated_at +json.url host_url(host, format: :json) diff --git a/app/views/hosts/edit.html.erb b/app/views/hosts/edit.html.erb new file mode 100644 index 0000000..36ab20a --- /dev/null +++ b/app/views/hosts/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Host

+ +<%= render 'form', host: @host %> + +<%= link_to 'Show', @host %> | +<%= link_to 'Back', hosts_path %> diff --git a/app/views/hosts/index.html.erb b/app/views/hosts/index.html.erb new file mode 100644 index 0000000..dc89265 --- /dev/null +++ b/app/views/hosts/index.html.erb @@ -0,0 +1,29 @@ +

<%= notice %>

+ +

Hosts

+ + + + + + + + + + + + <% @hosts.each do |host| %> + + + + + + + + <% end %> + +
IpAton
<%= host.ip %><%= host.aton %><%= link_to 'Show', host %><%= link_to 'Edit', edit_host_path(host) %><%= link_to 'Destroy', host, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Host', new_host_path %> diff --git a/app/views/hosts/index.json.jbuilder b/app/views/hosts/index.json.jbuilder new file mode 100644 index 0000000..1f8c29a --- /dev/null +++ b/app/views/hosts/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @hosts, partial: "hosts/host", as: :host diff --git a/app/views/hosts/new.html.erb b/app/views/hosts/new.html.erb new file mode 100644 index 0000000..1a509f0 --- /dev/null +++ b/app/views/hosts/new.html.erb @@ -0,0 +1,5 @@ +

New Host

+ +<%= render 'form', host: @host %> + +<%= link_to 'Back', hosts_path %> diff --git a/app/views/hosts/show.html.erb b/app/views/hosts/show.html.erb new file mode 100644 index 0000000..459079e --- /dev/null +++ b/app/views/hosts/show.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

+ Ip: + <%= @host.ip %> +

+ +

+ Aton: + <%= @host.aton %> +

+ +<%= link_to 'Edit', edit_host_path(@host) %> | +<%= link_to 'Back', hosts_path %> diff --git a/app/views/hosts/show.json.jbuilder b/app/views/hosts/show.json.jbuilder new file mode 100644 index 0000000..ce1aca0 --- /dev/null +++ b/app/views/hosts/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "hosts/host", host: @host diff --git a/config/routes.rb b/config/routes.rb index c06383a..aa52704 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,4 @@ Rails.application.routes.draw do + resources :hosts # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end diff --git a/db/migrate/20190706063437_create_hosts.rb b/db/migrate/20190706063437_create_hosts.rb new file mode 100644 index 0000000..8ebd43e --- /dev/null +++ b/db/migrate/20190706063437_create_hosts.rb @@ -0,0 +1,10 @@ +class CreateHosts < ActiveRecord::Migration[6.0] + def change + create_table :hosts do |t| + t.string :ip, null: false + t.integer :aton, limit: 5, default: -> { '(inet_aton(ip))' } + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..d275753 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,22 @@ +# 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 `rails +# db:schema:load`. When creating a new database, `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: 2019_07_06_063437) do + + create_table "hosts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t| + t.string "ip", null: false + t.bigint "aton", default: -> { "(inet_aton(`ip`))" } + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + +end diff --git a/db/seeds.rb b/db/seeds.rb index 1beea2a..12691ea 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,7 +1,11 @@ -# This file should contain all the record creation needed to seed the database with its default values. -# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup). -# -# Examples: -# -# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) -# Character.create(name: 'Luke', movie: movies.first) +Host.create( + [ + { ip: '192.168.1.1' }, + { ip: '192.168.2.1' }, + { ip: '192.168.9.1' }, + { ip: '192.168.10.1' }, + { ip: '192.168.20.1' }, + { ip: '192.168.99.1' }, + { ip: '192.168.120.1' } + ] +) diff --git a/test/controllers/hosts_controller_test.rb b/test/controllers/hosts_controller_test.rb new file mode 100644 index 0000000..7326888 --- /dev/null +++ b/test/controllers/hosts_controller_test.rb @@ -0,0 +1,48 @@ +require 'test_helper' + +class HostsControllerTest < ActionDispatch::IntegrationTest + setup do + @host = hosts(:one) + end + + test "should get index" do + get hosts_url + assert_response :success + end + + test "should get new" do + get new_host_url + assert_response :success + end + + test "should create host" do + assert_difference('Host.count') do + post hosts_url, params: { host: { aton: @host.aton, ip: @host.ip } } + end + + assert_redirected_to host_url(Host.last) + end + + test "should show host" do + get host_url(@host) + assert_response :success + end + + test "should get edit" do + get edit_host_url(@host) + assert_response :success + end + + test "should update host" do + patch host_url(@host), params: { host: { aton: @host.aton, ip: @host.ip } } + assert_redirected_to host_url(@host) + end + + test "should destroy host" do + assert_difference('Host.count', -1) do + delete host_url(@host) + end + + assert_redirected_to hosts_url + end +end diff --git a/test/fixtures/hosts.yml b/test/fixtures/hosts.yml new file mode 100644 index 0000000..8410e4d --- /dev/null +++ b/test/fixtures/hosts.yml @@ -0,0 +1,9 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + ip: MyString + aton: 1 + +two: + ip: MyString + aton: 1 diff --git a/test/models/host_test.rb b/test/models/host_test.rb new file mode 100644 index 0000000..e4bb6df --- /dev/null +++ b/test/models/host_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class HostTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/system/hosts_test.rb b/test/system/hosts_test.rb new file mode 100644 index 0000000..c9d3a9c --- /dev/null +++ b/test/system/hosts_test.rb @@ -0,0 +1,45 @@ +require "application_system_test_case" + +class HostsTest < ApplicationSystemTestCase + setup do + @host = hosts(:one) + end + + test "visiting the index" do + visit hosts_url + assert_selector "h1", text: "Hosts" + end + + test "creating a Host" do + visit hosts_url + click_on "New Host" + + fill_in "Aton", with: @host.aton + fill_in "Ip", with: @host.ip + click_on "Create Host" + + assert_text "Host was successfully created" + click_on "Back" + end + + test "updating a Host" do + visit hosts_url + click_on "Edit", match: :first + + fill_in "Aton", with: @host.aton + fill_in "Ip", with: @host.ip + click_on "Update Host" + + assert_text "Host was successfully updated" + click_on "Back" + end + + test "destroying a Host" do + visit hosts_url + page.accept_confirm do + click_on "Destroy", match: :first + end + + assert_text "Host was successfully destroyed" + end +end