diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb
index 383ee4c..9e80c8e 100644
--- a/app/controllers/hosts_controller.rb
+++ b/app/controllers/hosts_controller.rb
@@ -1,44 +1,51 @@
class HostsController < ApplicationController
-# before_action :fetch_params, only: :create
before_action :fetch_host, only: [:show, :edit, :update, :destroy]
# GET /hosts
def new
@host = Host.new
end
# POST /hosts
def create
@host = Host.new(fetch_params)
if @host.save
redirect_to host_path @host
else
render :new
end
end
# GET /hosts/1
def show; end
# GET /hosts/1/edit
def edit; end
- # PUT /hosts/1
- def update;end
+ # PATCH /hosts/1
+ def update
+ updates = fetch_params.slice(:port, :password)
+ if updates.present? && @host.update_attributes(updates)
+ @host.recalculate
+ redirect_to host_path @host
+ else
+ render :edit
+ end
+ end
# DELETE /hosts/1
def destroy
@host.destroy
redirect_to root_path
end
private
def fetch_host
@host = Host.includes(job_templates: [:fileset, :schedule]).find(params[:id])
end
def fetch_params
params.require(:host).permit(:fqdn, :port, :password)
end
end
diff --git a/app/views/hosts/_form.html.erb b/app/views/hosts/_form.html.erb
index 4d8aba5..629becc 100644
--- a/app/views/hosts/_form.html.erb
+++ b/app/views/hosts/_form.html.erb
@@ -1,15 +1,16 @@
<%= bootstrap_form_for(@host, layout: :horizontal,
label_col: 'col-xs-3', control_col: 'col-xs-8') do |f| %>
- <%= f.text_field :fqdn %>
- <%= f.text_field :password %>
- <%= f.number_field :port, value: 9102 %>
+
+ <%= f.text_field :fqdn, disabled: @host.persisted? %>
+ <%= f.text_field :password %>
+ <%= f.number_field :port, value: 9102 %>
<% end %>
diff --git a/app/views/hosts/edit.html.erb b/app/views/hosts/edit.html.erb
new file mode 100644
index 0000000..11bc55e
--- /dev/null
+++ b/app/views/hosts/edit.html.erb
@@ -0,0 +1,7 @@
+Edit Host <%= @host.name %>
+
+
+ <%= render 'form' %>
+
+
+<%= link_to 'Back', host_path(@host) %>
diff --git a/spec/controllers/hosts_controller_spec.rb b/spec/controllers/hosts_controller_spec.rb
index e284881..aa72b3f 100644
--- a/spec/controllers/hosts_controller_spec.rb
+++ b/spec/controllers/hosts_controller_spec.rb
@@ -1,55 +1,98 @@
require 'spec_helper'
describe HostsController do
describe 'GET #new' do
before { get :new }
it 'initializes a host' do
expect(assigns(:host)).to be
end
it 'renders' do
expect(response).to render_template(:new)
end
end
+ describe 'PATCH #update' do
+ let!(:host) { FactoryGirl.create(:host) }
+
+ context 'with valid params' do
+ let(:params) do
+ {
+ id: host.id,
+ host: { port: 9999, password: 'wrong_pass' }
+ }
+ end
+
+ it 'updates the host' do
+ expect { patch :update, params }.
+ to change { [host.reload.port, host.reload.password] }.
+ to([9999, 'wrong_pass'])
+ end
+
+ it 'redirects to host_show' do
+ patch :update, params
+ expect(response).to redirect_to(host_path(host))
+ end
+ end
+
+ context 'with fqdn in params' do
+ let(:params) do
+ {
+ id: host.id,
+ host: { fqdn: 'another.host.gr' }
+ }
+ end
+
+ it 'does not update the host' do
+ expect { patch :update, params }.
+ to_not change { host.reload.fqdn }
+ end
+
+ it 'renders the edit page' do
+ patch :update, params
+ expect(response).to render_template(:edit)
+ end
+ end
+ end
+
describe 'POST #create' do
context 'with valid params' do
let(:params) do
{
host: FactoryGirl.build(:host).attributes.symbolize_keys.
slice(:password, :fqdn, :port)
}
end
it 'creates the host' do
expect { post :create, params }.
to change { Host.count }.by(1)
end
it 'redirects to root' do
post :create, params
expect(response).to redirect_to(host_path(Host.last))
end
end
context 'with invalid params' do
let(:params) do
{
host: FactoryGirl.build(:host).attributes.symbolize_keys.
slice(:fqdn, :port)
}
end
before { post :create, params }
it 'initializes a host with errors' do
expect(assigns(:host)).to be
end
it 'renders :new' do
expect(response).to render_template(:new)
end
end
end
end