Skip to content

Commit

Permalink
try051
Browse files Browse the repository at this point in the history
Support default expression and expression indexes for MySQL
rails/rails#34307
  • Loading branch information
suketa committed Jul 6, 2019
1 parent d9eae5e commit 45552da
Show file tree
Hide file tree
Showing 21 changed files with 404 additions and 7 deletions.
3 changes: 3 additions & 0 deletions 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/
84 changes: 84 additions & 0 deletions 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;
}
75 changes: 75 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions app/helpers/hosts_helper.rb
@@ -0,0 +1,2 @@
module HostsHelper
end
2 changes: 2 additions & 0 deletions app/models/host.rb
@@ -0,0 +1,2 @@
class Host < ApplicationRecord
end
27 changes: 27 additions & 0 deletions app/views/hosts/_form.html.erb
@@ -0,0 +1,27 @@
<%= form_with(model: host, local: true) do |form| %>
<% if host.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(host.errors.count, "error") %> prohibited this host from being saved:</h2>

<ul>
<% host.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :ip %>
<%= form.text_field :ip %>
</div>

<div class="field">
<%= form.label :aton %>
<%= form.number_field :aton %>
</div>

<div class="actions">
<%= form.submit %>
</div>
<% end %>
2 changes: 2 additions & 0 deletions 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)
6 changes: 6 additions & 0 deletions app/views/hosts/edit.html.erb
@@ -0,0 +1,6 @@
<h1>Editing Host</h1>

<%= render 'form', host: @host %>
<%= link_to 'Show', @host %> |
<%= link_to 'Back', hosts_path %>
29 changes: 29 additions & 0 deletions app/views/hosts/index.html.erb
@@ -0,0 +1,29 @@
<p id="notice"><%= notice %></p>

<h1>Hosts</h1>

<table>
<thead>
<tr>
<th>Ip</th>
<th>Aton</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @hosts.each do |host| %>
<tr>
<td><%= host.ip %></td>
<td><%= host.aton %></td>
<td><%= link_to 'Show', host %></td>
<td><%= link_to 'Edit', edit_host_path(host) %></td>
<td><%= link_to 'Destroy', host, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Host', new_host_path %>
1 change: 1 addition & 0 deletions app/views/hosts/index.json.jbuilder
@@ -0,0 +1 @@
json.array! @hosts, partial: "hosts/host", as: :host
5 changes: 5 additions & 0 deletions app/views/hosts/new.html.erb
@@ -0,0 +1,5 @@
<h1>New Host</h1>

<%= render 'form', host: @host %>
<%= link_to 'Back', hosts_path %>
14 changes: 14 additions & 0 deletions app/views/hosts/show.html.erb
@@ -0,0 +1,14 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Ip:</strong>
<%= @host.ip %>
</p>

<p>
<strong>Aton:</strong>
<%= @host.aton %>
</p>

<%= link_to 'Edit', edit_host_path(@host) %> |
<%= link_to 'Back', hosts_path %>
1 change: 1 addition & 0 deletions app/views/hosts/show.json.jbuilder
@@ -0,0 +1 @@
json.partial! "hosts/host", host: @host
1 change: 1 addition & 0 deletions 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
10 changes: 10 additions & 0 deletions 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
22 changes: 22 additions & 0 deletions 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
18 changes: 11 additions & 7 deletions 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' }
]
)
48 changes: 48 additions & 0 deletions 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
9 changes: 9 additions & 0 deletions 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
7 changes: 7 additions & 0 deletions test/models/host_test.rb
@@ -0,0 +1,7 @@
require 'test_helper'

class HostTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 45552da

Please sign in to comment.