diff --git a/app/controllers/jobs_controller.rb b/app/controllers/jobs_controller.rb index 02c85ef..510676f 100644 --- a/app/controllers/jobs_controller.rb +++ b/app/controllers/jobs_controller.rb @@ -1,59 +1,59 @@ class JobsController < ApplicationController before_action :fetch_job, only: [:show, :edit, :update, :destroy, :toggle_enable] before_action :fetch_host, only: [:new, :edit, :show, :create, :update, :toggle_enable] # GET /jobs def new @job = @host.job_templates.new end # POST /jobs def create @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 if @job.update_attributes(fetch_params) redirect_to host_job_path(@host, @job) else render :edit end end # DELETE /jobs/1 def destroy end # PATCH /hosts/1/jobs/1/enable def toggle_enable @job.enabled = !@job.enabled @job.save redirect_to host_path(@host) end private def fetch_job @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) + params.require(:job_template).permit(:name, :fileset_id, :schedule_id) end end diff --git a/app/models/job_template.rb b/app/models/job_template.rb index 3bac6fa..d440906 100644 --- a/app/models/job_template.rb +++ b/app/models/job_template.rb @@ -1,50 +1,57 @@ class JobTemplate < ActiveRecord::Base establish_connection Baas::settings[:local_db] enum job_type: { backup: 0, restore: 1, verify: 2, admin: 3 } belongs_to :host belongs_to :fileset belongs_to :schedule validates :name, :schedule_id, :fileset_id, presence: true + before_save :set_job_type + scope :enabled, -> { where(enabled: true) } # configurable DEFAULT_OPTIONS = { storage: :File, pool: :Default, messages: :Standard, priority: 10, :'Write Bootstrap' => '"/var/lib/bacula/%c.bsr"' } def to_bacula_config_array ['Job {'] + DEFAULT_OPTIONS.map { |k,v| " #{k.capitalize} = #{v}" } + options_array.map { |x| " #{x}" } + ['}'] end def priority DEFAULT_OPTIONS[:priority] end def enabled_human enabled? ? 'yes' : 'no' end private + # Sets the default job_type as backup + def set_job_type + self.job_type = :backup if job_type.nil? + end + def options_array result = restore? ? ['Where = "/tmp/bacula-restores"'] : [] result += [ "Name = \"#{name}\"", "FileSet = \"#{fileset.name}\"", "Client = \"#{host.name}\"", "Type = \"#{job_type.capitalize}\"", "Schedule = \"#{schedule.name}\"" ] end end diff --git a/app/views/jobs/_form.html.erb b/app/views/jobs/_form.html.erb index dc0e1e3..c082ba3 100644 --- a/app/views/jobs/_form.html.erb +++ b/app/views/jobs/_form.html.erb @@ -1,56 +1,54 @@ <%= 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| %> <% 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' %> + <%= 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' ) %>
<%= link_to 'Add', new_schedule_path, class: 'btn btn-primary', role: 'button' %>
<%= f.submit class: "btn btn-success" %>
<% end %> diff --git a/spec/controllers/jobs_controller_spec.rb b/spec/controllers/jobs_controller_spec.rb index aec26e8..876a3de 100644 --- a/spec/controllers/jobs_controller_spec.rb +++ b/spec/controllers/jobs_controller_spec.rb @@ -1,64 +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) + slice(:name, :schedule_id, :fileset_id) } end it 'creates the job' do expect { post :create, params }. to change { JobTemplate.count }.by(1) end it 'redirects to host' 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 diff --git a/spec/factories/job_template.rb b/spec/factories/job_template.rb index 6bd6f2b..16df7ed 100644 --- a/spec/factories/job_template.rb +++ b/spec/factories/job_template.rb @@ -1,9 +1,8 @@ FactoryGirl.define do factory :job_template do host fileset schedule sequence(:name) { |n| "Job #{n}" } - job_type :backup end end diff --git a/spec/models/job_template_spec.rb b/spec/models/job_template_spec.rb index b21f400..c417f38 100644 --- a/spec/models/job_template_spec.rb +++ b/spec/models/job_template_spec.rb @@ -1,20 +1,28 @@ require 'spec_helper' describe JobTemplate do describe '#to_bacula_config_array' do let(:job_template) { FactoryGirl.create(:job_template) } subject { job_template.to_bacula_config_array } it 'has a Job structure' do expect(subject.first).to eq('Job {') expect(subject.last).to eq('}') end JobTemplate::DEFAULT_OPTIONS.each do |k, v| it "assigns #{k.capitalize} param" do expect(subject).to include(" #{k.capitalize} = #{v}") end end end + + context 'when no job_type is given' do + let(:job_template) { FactoryGirl.create(:job_template) } + + it 'sets the job_type to :backup' do + expect(job_template).to be_backup + end + end end