diff --git a/app/models/schedule.rb b/app/models/schedule.rb index 1f55686..60283f0 100644 --- a/app/models/schedule.rb +++ b/app/models/schedule.rb @@ -1,12 +1,37 @@ 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 validates :name, :runs, presence: true + before_validation :set_runs, if: Proc.new { |s| s.runtime.present? } + def to_bacula_config_array ['Schedule {'] + [" Name = \"#{name}\""] + runs.map {|r| " Run = #{r}" } + ['}'] end + + private + + 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/config/locales/en.yml b/config/locales/en.yml index 0653957..8305a70 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,23 +1,31 @@ # Files in the config/locales directory are used for internationalization # and are automatically loaded by Rails. If you want to use locales other # than English, add the necessary files in this directory. # # To use the locales, use `I18n.t`: # # I18n.t 'hello' # # In views, this is aliased to just `t`: # # <%= t('hello') %> # # To use a different locale, set it with `I18n.locale`: # # I18n.locale = :es # # This would use the information in config/locales/es.yml. # # To learn more, please read the Rails Internationalization guide # available at http://guides.rubyonrails.org/i18n.html. en: hello: "Hello world" + + activerecord: + errors: + models: + schedule: + attributes: + runtime: + not_valid_24h_time: 'Not a valid time format, use HH:MM'