diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb new file mode 100644 index 0000000..84b9fc1 --- /dev/null +++ b/app/controllers/hosts_controller.rb @@ -0,0 +1,22 @@ +class HostsController < ApplicationController +# before_action :fetch_params, only: :create + + def new + @host = Host.new + end + + def create + @host = Host.new(fetch_params) + if @host.save + redirect_to root_path + else + render :new + end + end + + private + + def fetch_params + params.require(:host).permit(:name, :fqdn, :port, :password) + end +end diff --git a/app/views/clients/index.html.erb b/app/views/clients/index.html.erb index 3dc1fb8..ff6dbba 100644 --- a/app/views/clients/index.html.erb +++ b/app/views/clients/index.html.erb @@ -1,23 +1,27 @@ +
+ <%= link_to 'New Client', new_host_path, class: 'btn btn-primary', role: 'button' %> +
+

My Hosts

<%= render partial: 'client', collection: @clients %>
Name Uname Active Jobs Last Backup FileRetention (days) JobRetention (days) Space Used File count AutoPrune
diff --git a/app/views/hosts/_form.html.erb b/app/views/hosts/_form.html.erb new file mode 100644 index 0000000..9558c57 --- /dev/null +++ b/app/views/hosts/_form.html.erb @@ -0,0 +1,13 @@ +<%= bootstrap_form_for(@host) do |f| %> +
+ <%= f.text_field :name %> + <%= f.text_field :password %> + <%= f.number_field :port %> + <%= f.text_field :fqdn %> +
+ + +
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/hosts/new.html.erb b/app/views/hosts/new.html.erb new file mode 100644 index 0000000..b372286 --- /dev/null +++ b/app/views/hosts/new.html.erb @@ -0,0 +1,5 @@ +

New Host

+ +<%= render 'form' %> + +<%= link_to 'Back', clients_path %> diff --git a/config/routes.rb b/config/routes.rb index d56c812..c624f02 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,6 @@ Rails.application.routes.draw do resources :clients, only: [:index, :show] + resources :hosts, only: [:new, :create] root 'clients#index' end diff --git a/spec/controllers/hosts_controller_spec.rb b/spec/controllers/hosts_controller_spec.rb new file mode 100644 index 0000000..b3580ff --- /dev/null +++ b/spec/controllers/hosts_controller_spec.rb @@ -0,0 +1,55 @@ +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 'POST #create' do + context 'with valid params' do + let(:params) do + { + host: FactoryGirl.build(:host).attributes.symbolize_keys. + slice(:password, :name, :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(:name, :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 diff --git a/spec/routing/host_routing_spec.rb b/spec/routing/host_routing_spec.rb new file mode 100644 index 0000000..eee61ba --- /dev/null +++ b/spec/routing/host_routing_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe HostsController do + it 'routes GET /hosts/new' do + expect(get('/hosts/new')).to route_to( { controller: 'hosts', action: 'new' }) + end + + it 'routes POST /hosts' do + expect(post('/hosts')).to route_to( { controller: 'hosts', action: 'create' }) + end +end