diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index d4087f7..31d3872 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -1,40 +1,67 @@ // This is a manifest file that'll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. // // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // compiled file. // // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details // about supported directives. // //= require jquery //= require jquery_ujs //= require bootstrap.min //= require_tree . //= require highcharts $(document).ready(function() { $(".include_files-plus-sign").click(function() { addIncludedFileTextArea(); }); $(".exclude_directions-plus-sign").click(function() { addExcludeDirectionsTextArea(); }); + $(".schedule_run_form_plus").click(function() { + addScheduleRun(); + }); + $(".schedule_run_form_remove").click(function() { + removeScheduleRun(); + }); }); function addIncludedFileTextArea() { var textArrea = $('.include_files:last').clone(true).val(""); $('.include_files-plus-sign:first').parent().remove(); textArrea.insertAfter('.include_files:last'); $('.include_files:last input').val(""); } function addExcludeDirectionsTextArea() { var textArrea = $('.exclude_directions:last').clone(true).val(""); $('.exclude_directions-plus-sign:first').parent().remove(); textArrea.insertAfter('.exclude_directions:last'); $('.exclude_directions:last input').val(""); } + +function addScheduleRun() { + var scheduleRun = $('.schedule_run_form:last').clone(); + scheduleRun.insertAfter('.schedule_run_form:last'); + $('.schedule_run_form:last input').val(''); + if ($('.schedule_run_form').size() > 1) { + $(".schedule_run_form_remove").show(); + }; +} + +function removeScheduleRun() { + if ($('.schedule_run_form').size() > 1) { + $('.schedule_run_form:last').remove(); + if ($('.schedule_run_form').size() == 1) { + $(".schedule_run_form_remove").hide(); + }; + } + else { + alert('nothing to remove'); + }; +} diff --git a/app/controllers/schedules_controller.rb b/app/controllers/schedules_controller.rb index c5ef177..4824a49 100644 --- a/app/controllers/schedules_controller.rb +++ b/app/controllers/schedules_controller.rb @@ -1,51 +1,52 @@ class SchedulesController < ApplicationController before_action :fetch_host, only: [:new, :create] before_action :fetch_job_id, only: [:new, :create] def new @schedule = @host.schedules.new end def show end def edit end def update end def create @schedule = @host.schedules.new(fetch_params) @schedule.runtime = params[:schedule][:runtime] if params[:schedule][:runtime] if @schedule.save flash[:success] = 'Schedule created successfully' if @job_id.present? redirect_to edit_host_job_path(@host, @job_id, schedule_id: @schedule.id) else redirect_to new_host_job_path(@host, schedule_id: @schedule.id) end else render :new end end def destroy end private def fetch_host @host = current_user.hosts.find(params[:host_id]) end def fetch_job_id @job_id = @host.job_templates.find(params[:job_id]).id if params[:job_id].present? end def fetch_params - params.require(:schedule).permit(:name) + params.require(:schedule). + permit(:name, { schedule_runs_attributes: [[:level, :month, :day, :time]] }) end end diff --git a/app/models/schedule.rb b/app/models/schedule.rb index a026cee..ccad7ab 100644 --- a/app/models/schedule.rb +++ b/app/models/schedule.rb @@ -1,45 +1,22 @@ 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 + has_many :schedule_runs belongs_to :host - validates :name, :runs, presence: true + validates :name, presence: true validates :name, uniqueness: { scope: :host } validates_with NameValidator - before_validation :set_runs, if: Proc.new { |s| s.runtime.present? } + accepts_nested_attributes_for :schedule_runs def to_bacula_config_array ['Schedule {'] + [" Name = \"#{name_for_config}\""] + - runs.map {|r| " Run = #{r}" } + + schedule_runs.map {|r| " Run = #{r.schedule_line}" } + ['}'] end def name_for_config [host.name, name].join(' ') 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/app/views/schedules/_form.html.erb b/app/views/schedules/_form.html.erb index 4ebd63e..f53d299 100644 --- a/app/views/schedules/_form.html.erb +++ b/app/views/schedules/_form.html.erb @@ -1,27 +1,49 @@ <%= bootstrap_form_for(@schedule, url: host_schedules_path(@host), layout: :horizontal, label_col: 'col-xs-3', control_col: 'col-xs-8') do |f| %> - <% if @schedule.errors.any? %> -
eg: jan-mar, feb, monthly
+ <%= r.text_field :day, placeholder: '[week | week-range] day | day-range', + required: true %> ++eg: first sun, second-fifth mon, mon-sat +
+ <%= r.time_field :time, placeholder: 'HH:MM', required: true %> +