check_type 追加
[from now] 2014/11/24 12:31:07

diff --git a/app/controllers/servers_controller.rb b/app/controllers/servers_controller.rb
index d1e9aa8..c2d3beb 100644
--- a/app/controllers/servers_controller.rb
+++ b/app/controllers/servers_controller.rb
@@ -17,7 +17,7 @@ class ServersController < ApplicationController

   # GET /servers/new
   def new
-    @server = Server.new(check_type: 1)
+    @server = Server.new
   end

   # GET /servers/1/edit
diff --git a/app/models/checker.rb b/app/models/checker.rb
new file mode 100644
index 0000000..674c7d1
--- /dev/null
+++ b/app/models/checker.rb
@@ -0,0 +1,3 @@
+class Checker < ActiveRecord::Base
+  validates :type, inclusion: { in: 1..2 }
+end
diff --git a/app/models/server.rb b/app/models/server.rb
index 562777d..84b3cb3 100644
--- a/app/models/server.rb
+++ b/app/models/server.rb
@@ -3,8 +3,8 @@ require 'net/http'
 # サーバ
 class Server < ActiveRecord::Base
   has_many :advisories
+  has_one :checker
   validates :name, presence: true
-  validates :check_type, inclusion: { in: 1..2 }

   # 状態を確認する
   def check_status
diff --git a/app/views/servers/_form.html.erb b/app/views/servers/_form.html.erb
index 4f8eb83..240aee1 100644
--- a/app/views/servers/_form.html.erb
+++ b/app/views/servers/_form.html.erb
@@ -22,6 +22,7 @@
       <%= f.text_field :name, class: 'form-control' %>
     </div>

+    <% if false %>
     <%= f.label :check_type, class: 'control-label' %>
     <div class="controls">
       <div class="col-sm-10">
@@ -39,6 +40,7 @@
 	</div>
       </div>
     </div>
+    <% end %>

     <%= error_span(@server[:name]) %>
   </div>
diff --git a/app/views/servers/show.html.erb b/app/views/servers/show.html.erb
index 2d8135d..6566320 100644
--- a/app/views/servers/show.html.erb
+++ b/app/views/servers/show.html.erb
@@ -8,8 +8,11 @@
   <dd><%= @server.name %></dd>
   <dt><strong><%= model_class.human_attribute_name(:status) %>:</strong></dt>
   <dd><%= @server.status.message %></dd>
+
+  <% if false %>
   <dt><strong><%= model_class.human_attribute_name(:check_type) %>:</strong></dt>
   <dd><%= @server.check_type %></dd>
+  <% end %>
 </dl>

 <%= link_to(t('.advisory', default: t('helpers.links.advisory')),
diff --git a/db/migrate/20141119112304_add_check_type_to_servers.rb b/db/migrate/20141119112304_add_check_type_to_servers.rb
deleted file mode 100644
index 059af3a..0000000
--- a/db/migrate/20141119112304_add_check_type_to_servers.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class AddCheckTypeToServers < ActiveRecord::Migration
-  def change
-    add_column :servers, :check_type, :integer
-  end
-end
diff --git a/db/migrate/20141124012006_create_checkers.rb b/db/migrate/20141124012006_create_checkers.rb
new file mode 100644
index 0000000..387292c
--- /dev/null
+++ b/db/migrate/20141124012006_create_checkers.rb
@@ -0,0 +1,9 @@
+class CreateCheckers < ActiveRecord::Migration
+  def change
+    create_table :checkers do |t|
+      t.integer :type
+
+      t.timestamps
+    end
+  end
+end
diff --git a/db/migrate/20141124014042_add_server_id_to_checkers.rb b/db/migrate/20141124014042_add_server_id_to_checkers.rb
new file mode 100644
index 0000000..deb2ba6
--- /dev/null
+++ b/db/migrate/20141124014042_add_server_id_to_checkers.rb
@@ -0,0 +1,5 @@
+class AddServerIdToCheckers < ActiveRecord::Migration
+  def change
+    add_column :checkers, :server_id, :integer
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 731200b..c0cbbc9 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.

-ActiveRecord::Schema.define(version: 20141119112304) do
+ActiveRecord::Schema.define(version: 20141124014042) do

   create_table "advisories", force: true do |t|
     t.integer  "server_id"
@@ -22,11 +22,18 @@ ActiveRecord::Schema.define(version: 20141119112304) do

   add_index "advisories", ["server_id"], name: "index_advisories_on_server_id"

+  create_table "checkers", force: true do |t|
+    t.integer  "type"
+    t.datetime "created_at"
+    t.datetime "updated_at"
+    t.integer  "server_id"
+  end
+
   create_table "servers", force: true do |t|
     t.string   "name"
     t.datetime "created_at"
     t.datetime "updated_at"
-    t.integer  "check_type"
+    t.integer  "checker_id"
   end

 end
diff --git a/spec/controllers/servers_controller_spec.rb b/spec/controllers/servers_controller_spec.rb
index 8063592..287cf85 100644
--- a/spec/controllers/servers_controller_spec.rb
+++ b/spec/controllers/servers_controller_spec.rb
@@ -24,11 +24,11 @@ RSpec.describe ServersController, type: :controller do
   # Server. As you add validations to Server, be sure to
   # adjust the attributes here as well.
   let(:valid_attributes) do
-    { name: 'www.example.com', check_type: 1 }
+    { name: 'www.example.com' }
   end

   let(:invalid_attributes) do
-    { name: ' ', check_type: 3 }
+    { name: ' ' }
   end

   # This should return the minimal set of values that should be in the session
diff --git a/spec/factories.rb b/spec/factories.rb
index 4913859..1e0e833 100644
--- a/spec/factories.rb
+++ b/spec/factories.rb
@@ -1,7 +1,11 @@
 FactoryGirl.define do
   factory :server do
     name 'www.example.com'
-    check_type 1
+    checker
+  end
+
+  factory :checker do
+    type 1
   end

   factory :advisory do
diff --git a/spec/models/checker_spec.rb b/spec/models/checker_spec.rb
new file mode 100644
index 0000000..94b24e8
--- /dev/null
+++ b/spec/models/checker_spec.rb
@@ -0,0 +1,5 @@
+require 'rails_helper'
+
+RSpec.describe Checker, :type => :model do
+  pending "add some examples to (or delete) #{__FILE__}"
+end
diff --git a/spec/models/server_spec.rb b/spec/models/server_spec.rb
index aa344c8..c34d79e 100644
--- a/spec/models/server_spec.rb
+++ b/spec/models/server_spec.rb
@@ -14,9 +14,4 @@ RSpec.describe Server, type: :model do
       expect(server.status.code).not_to eq '200'
     end
   end
-
-  describe 'check_type is nil' do
-    let(:server) { FactoryGirl.build(:server, check_type: nil) }
-    specify { expect(server).not_to be_valid }
-  end
 end
diff --git a/spec/requests/servers_spec.rb b/spec/requests/servers_spec.rb
index 671ebcc..cf06ccc 100644
--- a/spec/requests/servers_spec.rb
+++ b/spec/requests/servers_spec.rb
@@ -14,24 +14,8 @@ RSpec.describe 'Servers', type: :request do
     subject { @server }

     it { is_expected.to respond_to :name }
-    it { is_expected.to respond_to :check_type }

     it { is_expected.to be_valid }
-
-    describe 'Check Type が nil の際' do
-      before { @server.check_type = nil }
-      it { is_expected.not_to be_valid }
-    end
-
-    describe 'Check Type が 1 未満の際' do
-      before { @server.check_type = 0 }
-      it { is_expected.not_to be_valid }
-    end
-
-    describe 'Check Type が 2 より大きい際' do
-      before { @server.check_type = 3 }
-      it { is_expected.not_to be_valid }
-    end
   end

   describe '登録ページ' do