diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 311b7b6..3581b31 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -1,27 +1,39 @@ /* * This is a manifest file that'll be compiled into application.css, which will include all the files * listed below. * * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path. * * You're free to add application-wide styles to this file and they'll appear at the bottom of the * compiled file so the styles you add here take precedence over styles defined in any styles * defined in the other CSS/SCSS files in this directory. It is generally better to create a new * file per style scope. * *= require bootstrap.min *= require_tree . *= require_self */ /* Make sure navbar does not overlay body */ body { padding-top: 70px; } .right { text-align: right; margin-right: 0px; float: right; } + +.graybox { + max-width: 500px; + margin-right: 0; + margin-left: 0; + background-color: #F7F7F9; + margin-bottom: 30px; +} + +.graybox form { + padding-top: 30px; +} diff --git a/app/controllers/jobs_controller.rb b/app/controllers/jobs_controller.rb index 388df9f..9d05729 100644 --- a/app/controllers/jobs_controller.rb +++ b/app/controllers/jobs_controller.rb @@ -1,36 +1,46 @@ class JobsController < ApplicationController before_action :fetch_job, only: [:show, :edit, :update, :destroy] + before_action :fetch_host, only: [:new, :create] # GET /jobs def new - @job_template = JobTemplate.new + @job = @host.job_templates.new end # POST /jobs def create - @job_template = JobTemplate.new(fetch_params) + @job = @host.job_templates.new(fetch_params) + if @job.save + redirect_to host_path(@host) + else + render :new + end end # GET /jobs/1 def show; end # GET /jobs/1/edit def edit;end # PUT /jobs/1 def update;end # DELETE /jobs/1 def destroy end private def fetch_job - @job_template = JobTemplate.find(params[:id]) + @job = JobTemplate.find(params[:id]) + end + + def fetch_host + @host = Host.find(params[:host_id]) end def fetch_params params.require(:job_template).permit(:name, :job_type, :fileset_id, :schedule_id) end end diff --git a/app/views/jobs/_form.html.erb b/app/views/jobs/_form.html.erb new file mode 100644 index 0000000..83f1467 --- /dev/null +++ b/app/views/jobs/_form.html.erb @@ -0,0 +1,56 @@ +<%= bootstrap_form_for(@job, url: host_jobs_path, layout: :horizontal, label_col: 'col-xs-4', control_col: 'col-xs-6') do |f| %> + <% if @job.errors.any? %> +
+

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

+ + +
+ <% end %> + + <%= f.text_field :name %> + + <%= f.select :job_type, options_for_select(JobTemplate.job_types.keys) %> + +
+ <%= label :job_template, :fileset_id, 'Fileset', class: 'control-label col-xs-4' %> +
+ <%= select_tag(:job_template_fileset_id, + options_for_select(Fileset.pluck(:name, :id)), + name: 'job_template[fileset_id]', + class: 'form-control' + ) %> +
+ +
+ <%= link_to 'Add', '#', class: 'btn btn-primary', role: 'button' %> +
+
+ +
+ <%= label :job_template, :schedule_id, 'Schedule', 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' + ) %> +
+ +
+ <%= link_to 'Add', '#', class: 'btn btn-primary', role: 'button' %> +
+
+ +
+
+ <%= f.submit class: "btn btn-success" %> +
+
+<% end %> diff --git a/app/views/jobs/new.html.erb b/app/views/jobs/new.html.erb new file mode 100644 index 0000000..357cb8c --- /dev/null +++ b/app/views/jobs/new.html.erb @@ -0,0 +1,18 @@ +

New Job Template

+ +
+
+
+

Job Attributes

+ <%= render 'form' %> +
+
+
+ +
+ +
+
+ <%= link_to 'Cancel', host_path(@job.host), class: 'btn btn-danger', role: 'button' %> +
+
diff --git a/spec/controllers/jobs_controller_spec.rb b/spec/controllers/jobs_controller_spec.rb new file mode 100644 index 0000000..152cdd6 --- /dev/null +++ b/spec/controllers/jobs_controller_spec.rb @@ -0,0 +1,64 @@ +require 'spec_helper' + +describe JobsController do + let!(:host) { FactoryGirl.create(:host) } + + describe 'GET #new' do + before { get :new, host_id: host.id } + + it 'initializes a job' do + expect(assigns(:job)).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_id: host.id, + job_template: FactoryGirl.build(:job_template).attributes.symbolize_keys. + slice(:name, :schedule_id, :fileset_id).merge(job_type: :backup) + } + end + + it 'creates the job' do + expect { post :create, params }. + to change { JobTemplate.count }.by(1) + end + + it 'redirects to root' do + post :create, params + expect(response).to redirect_to(host_path(host)) + end + end + + context 'with invalid params' do + let(:params) do + { + host_id: host.id, + job_template: FactoryGirl.build(:job_template).attributes.symbolize_keys. + slice(:name, :fileset_id) + } + end + + it 'initializes a job with errors' do + post :create, params + expect(assigns(:job)).to be + end + + it 'does not create the job' do + expect { post :create, params }. + to_not change { JobTemplate.count } + end + + it 'renders :new' do + post :create, params + expect(response).to render_template(:new) + end + end + end +end