diff --git a/app/controllers/filesets_controller.rb b/app/controllers/filesets_controller.rb index e8ec205..0c4f248 100644 --- a/app/controllers/filesets_controller.rb +++ b/app/controllers/filesets_controller.rb @@ -1,27 +1,33 @@ class FilesetsController < ApplicationController + before_action :fetch_host, only: [:new, :create] + def new - @fileset = Fileset.new + @fileset = @host.filesets.new end def show end def create - @fileset = Fileset.new(fetch_params) + @fileset = @host.filesets.new(fetch_params) if @fileset.save - redirect_to root_path + redirect_to host_path(@host) else render :new end end def destroy end private + def fetch_host + @host = Host.find(params[:host_id]) + end + def fetch_params params.require(:fileset).permit(:name, exclude_directions: [], include_files: []) end end diff --git a/app/views/filesets/_form.html.erb b/app/views/filesets/_form.html.erb index f35e602..bd2a384 100644 --- a/app/views/filesets/_form.html.erb +++ b/app/views/filesets/_form.html.erb @@ -1,29 +1,29 @@ -<%= bootstrap_form_for(@fileset, layout: :horizontal, +<%= bootstrap_form_for(@fileset, url: host_filesets_path(@host, @fileset), layout: :horizontal, label_col: 'col-xs-3', control_col: 'col-xs-8') do |f| %> <% if @fileset.errors.any? %>

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

<% end %>
<%= f.text_field :name %> <%= f.text_field :include_files, label: 'Files', placeholder: '/', multiple: true %> <%= f.text_field :include_files, label: 'Files', placeholder: '/media', multiple: true %> <%= f.text_field :exclude_directions, label: 'Exclude', placeholder: '/proc', multiple: true %> <%= f.text_field :exclude_directions, label: 'Exclude', placeholder: '/bacula', multiple: true %>
<%= f.submit class: 'btn btn-success' %>
<% end %> diff --git a/app/views/jobs/_form.html.erb b/app/views/jobs/_form.html.erb index 57432c3..a268222 100644 --- a/app/views/jobs/_form.html.erb +++ b/app/views/jobs/_form.html.erb @@ -1,53 +1,53 @@ <%= bootstrap_form_for(@job, url: @job.new_record? ? host_jobs_path : host_job_path(@host, @job), layout: :horizontal, label_col: 'col-xs-4', control_col: 'col-xs-6') do |f| %> <%= f.text_field :name %>
<%= label :job_template, :fileset_id, 'Fileset', class: 'control-label col-xs-4 required' %>
<%= select_tag(:job_template_fileset_id, - options_for_select(Fileset.pluck(:name, :id)), + options_for_select(@host.filesets.pluck(:name, :id)), name: 'job_template[fileset_id]', class: 'form-control' ) %> <% if @job.errors[:fileset_id].present? %> <%= @job.errors[:fileset_id].first %> <% end %>
- <%= link_to 'Add', '#', class: 'btn btn-primary', role: 'button' %> + <%= link_to 'Add', new_host_fileset_path(@host), class: 'btn btn-primary', role: 'button' %>
<% if !@job.restore? %>
<%= label :job_template, :schedule_id, 'Schedule Time', class: 'control-label col-xs-4' %>
<%= select_tag(:job_template_schedule_id, options_for_select(Schedule.pluck(:name, :id)), name: 'job_template[schedule_id]', class: 'form-control' ) %> <% if @job.errors[:schedule_id].present? %> <%= @job.errors[:schedule_id].first %> <% end %>
<%= link_to 'Add', new_schedule_path, class: 'btn btn-primary', role: 'button' %>
<% end %> <%= f.text_field :restore_location, label: 'Restore Location', placeholder: '/tmp/bacula' %>
<%= f.submit class: "btn btn-success" %>
<% end %> diff --git a/config/routes.rb b/config/routes.rb index b5b2595..7328271 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,16 +1,17 @@ Rails.application.routes.draw do resources :clients, only: [:index, :show] resources :hosts, only: [:new, :create, :show, :edit, :update, :destroy] do resources :jobs, only: [:new, :create, :show, :edit, :update, :destroy] do member do patch :toggle_enable end end + + resources :filesets, only: [:show, :new, :create, :destroy] end resources :schedules, only: [:show, :new, :edit, :create, :update, :destroy] - resources :filesets, only: [:show, :new, :create, :destroy] root 'clients#index' end diff --git a/spec/controllers/filesets_controller_spec.rb b/spec/controllers/filesets_controller_spec.rb index e3399f4..18bfa32 100644 --- a/spec/controllers/filesets_controller_spec.rb +++ b/spec/controllers/filesets_controller_spec.rb @@ -1,57 +1,75 @@ require 'spec_helper' describe FilesetsController do + let(:host) { FactoryGirl.create(:host) } + describe 'GET #new' do - before { get :new } + before { get :new, host_id: host.id } it 'initializes a fileset' do expect(assigns(:fileset)).to be end + it 'sets host the fileset\'s host' do + expect(assigns(:fileset).host).to eq(host) + end + it 'renders' do expect(response).to render_template(:new) end end describe 'POST #create' do context 'with valid params' do let(:params) do { - fileset: { name: FactoryGirl.build(:fileset).name, - exclude_directions: ['/proc', '/tmp'], - include_files: ['/home', '/media'] - } + host_id: host.id, + fileset: { + name: FactoryGirl.build(:fileset).name, + exclude_directions: ['/proc', '/tmp'], + include_files: ['/home', '/media'] + } } end it 'creates the fileset' do expect { post :create, params }. - to change { Fileset.count }.by(1) + to change { host.filesets(true).count }.by(1) end - it 'redirects to root' do + it 'redirects to host' do post :create, params - expect(response).to redirect_to(root_path) + expect(response).to redirect_to(host_path(host)) end end context 'with invalid params' do - let(:params) { { fileset: { invalid: :foo } } } + let(:params) do + { + host_id: host.id, + fileset: { invalid: :foo } + } + end it 'initializes a fileset with errors' do post :create, params expect(assigns(:fileset)).to be end + it 'sets the host' do + post :create, params + expect(assigns(:fileset).host).to eq(host) + end + it 'does not create the fileset' do expect { post :create, params }. to_not change { Fileset.count } end it 'renders :new' do post :create, params expect(response).to render_template(:new) end end end end diff --git a/spec/routing/fileset_routing_spec.rb b/spec/routing/fileset_routing_spec.rb index b4b2f97..2c60272 100644 --- a/spec/routing/fileset_routing_spec.rb +++ b/spec/routing/fileset_routing_spec.rb @@ -1,21 +1,23 @@ require 'spec_helper' describe FilesetsController do - it 'routes GET /filesets/new' do - expect(get('/filesets/new')).to route_to( { controller: 'filesets', action: 'new'}) + it 'routes GET /hosts/:host_id/filesets/new' do + expect(get('/hosts/1/filesets/new')). + to route_to(controller: 'filesets', action: 'new', host_id: '1') end - it 'routes POST /filesets' do - expect(post('/filesets')).to route_to( { controller: 'filesets', action: 'create'}) + it 'routes POST /hosts/:host_id/filesets' do + expect(post('/hosts/1/filesets')). + to route_to(controller: 'filesets', action: 'create', host_id: '1') end - it 'routes GET /filesets/1' do - expect(get('/filesets/1')). - to route_to( { controller: 'filesets', action: 'show', id: '1' }) + it 'routes GET /hosts/:host_id/filesets/:id' do + expect(get('/hosts/1/filesets/2')). + to route_to(controller: 'filesets', action: 'show', host_id: '1', id: '2') end - it 'routes DELETE /filesets/1' do - expect(delete('/filesets/1')). - to route_to( { controller: 'filesets', action: 'destroy', id: '1' }) + it 'routes DELETE /hosts/:host_id/filesets/:id' do + expect(delete('/hosts/1/filesets/2')). + to route_to(controller: 'filesets', action: 'destroy', host_id: '1', id: '2') end end