diff --git a/app/models/schedule.rb b/app/models/schedule.rb index b9f85bc..9373621 100644 --- a/app/models/schedule.rb +++ b/app/models/schedule.rb @@ -1,39 +1,45 @@ class Schedule < ActiveRecord::Base DEFAULT_RUNS = [ 'Level=Full 1st sun at ', 'Level=Differential 2nd-5th sun at ', 'Level=Incremental mon-sat at ' ] attr_accessor :runtime serialize :runs, JSON belongs_to :host validates :name, :runs, presence: true + validates :name, uniqueness: { scope: :host } + before_validation :set_runs, if: Proc.new { |s| s.runtime.present? } def to_bacula_config_array ['Schedule {'] + - [" Name = \"#{name}\""] + + [" Name = \"#{name_for_config}\""] + runs.map {|r| " Run = #{r}" } + ['}'] end private + def name_for_config + [host.name, name].join(' ') + end + def set_runs if valid_runtime? self.runs = DEFAULT_RUNS.map { |r| r + runtime } else self.errors.add(:runtime, :not_valid_24h_time) false end end def valid_runtime? runtime && runtime[/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/] end end diff --git a/spec/models/schedule_spec.rb b/spec/models/schedule_spec.rb index 7e223ae..f8069fe 100644 --- a/spec/models/schedule_spec.rb +++ b/spec/models/schedule_spec.rb @@ -1,44 +1,58 @@ require 'spec_helper' describe Schedule do context 'validates' do it 'presence of name' do expect(Schedule.new).to have(1).errors_on(:name) end it 'presence of runs' do expect(Schedule.new).to have(1).errors_on(:runs) end + + context 'schedule name is unique in the host\'s scope' do + let!(:schedule_1) { FactoryGirl.create(:schedule, name: 'Schedule_1') } + let(:schedule_2) { FactoryGirl.build(:schedule, name: 'Schedule_1') } + let(:schedule_3) { FactoryGirl.build(:schedule, name: 'Schedule_1', host: schedule_1.host) } + + it 'two schedules of diferent hosts can have the same name' do + expect(schedule_2).to be_valid + end + + it 'two schedules of the same host can NOT have the same name' do + expect(schedule_3).to_not be_valid + end + end end describe '#to_bacula_config_array' do let(:runs) do [ 'Full 1st sun at 23:05', 'Differential 2nd-5th sun at 23:50', 'Incremental mon-sat at 23:50' ] end let(:schedule) do FactoryGirl.create(:schedule, name: 'Test Schedule', runs: runs) end subject { schedule.to_bacula_config_array } it 'is a schedule type resource' do expect(subject.first).to eq('Schedule {') expect(subject.last).to eq('}') end it 'contains the name' do - expect(subject).to include(" Name = \"#{schedule.name}\"") + expect(subject).to include(" Name = \"#{[schedule.host.name, schedule.name].join(' ')}\"") end it 'contains the runs' do runs.each do |r| expect(subject).to include(" Run = #{r}") end end end end