Commit 76c0364c authored by Shinya Maeda's avatar Shinya Maeda Committed by Kamil Trzcinski

Use allow_destroy. Remove condtion from form.haml.

parent 2548c155
......@@ -16,6 +16,7 @@ class Projects::TriggersController < Projects::ApplicationController
if @trigger.valid?
flash[:notice] = 'Trigger was created successfully.'
else
puts "@trigger.errors: #{@trigger.errors.inspect}"
flash[:alert] = 'You could not create a new trigger.'
end
......@@ -33,6 +34,7 @@ class Projects::TriggersController < Projects::ApplicationController
end
def edit
@trigger.build_trigger_schedule unless @trigger.trigger_schedule.present?
end
def update
......@@ -69,15 +71,15 @@ class Projects::TriggersController < Projects::ApplicationController
def create_params
params.require(:trigger).permit(
:description, :ref, :trigger_schedule_on,
trigger_schedule_attributes: [ :cron, :cron_timezone ]
:description, :ref,
trigger_schedule_attributes: [ :cron, :cron_timezone, :_destroy ]
)
end
def update_params
params.require(:trigger).permit(
:description, :ref, :trigger_schedule_on,
trigger_schedule_attributes: [ :cron, :cron_timezone ]
:description, :ref,
trigger_schedule_attributes: [ :cron, :cron_timezone, :_destroy ]
)
end
end
......@@ -8,27 +8,16 @@ module Ci
belongs_to :owner, class_name: "User"
has_many :trigger_requests, dependent: :destroy
has_one :trigger_schedule, dependent: :destroy
has_one :trigger_schedule, dependent: :destroy, inverse_of: :trigger
validates :token, presence: true, uniqueness: true
before_validation :set_default_values
accepts_nested_attributes_for :trigger_schedule
attr_accessor :trigger_schedule_on
accepts_nested_attributes_for :trigger_schedule, allow_destroy: true
def set_default_values
self.token = SecureRandom.hex(15) if self.token.blank?
if trigger_schedule_on.present?
if trigger_schedule_on.to_i == 1
self.trigger_schedule.project = project
self.trigger_schedule.trigger = self
else
self.trigger_schedule = nil
end
end
end
def last_trigger_request
......
......@@ -6,11 +6,11 @@ module Ci
acts_as_paranoid
belongs_to :project
belongs_to :trigger
belongs_to :trigger, inverse_of: :trigger_schedule
delegate :ref, to: :trigger, allow_nil: true
validates :trigger, presence: { unless: :importing? }
validates_presence_of :trigger
validates :cron, cron: true, presence: { unless: :importing? }
validates :cron_timezone, cron_timezone: true, presence: { unless: :importing? }
validates :ref, presence: { unless: :importing? }
......@@ -19,6 +19,7 @@ module Ci
def set_next_run_at
self.next_run_at = Gitlab::Ci::CronParser.new(cron, cron_timezone).next_time_from(Time.now)
self.project = trigger.project
end
def schedule_next_run!
......
......@@ -18,13 +18,10 @@
.form-group
= f.label :ref, "Ref (For scheduled trigger)", class: "label-light"
= f.text_field :ref, class: "form-control", title: 'Trigger Schedule Ref is required.', placeholder: "master"
- if action_name == 'edit'
= f.hidden_field :trigger_schedule_on, :value => @trigger.trigger_schedule.present? ? 1 : 0
- else
.form-group
.checkbox
= f.label :trigger_schedule_on do
= f.check_box :trigger_schedule_on
= schedule_fields.label :_destroy do
= schedule_fields.check_box :_destroy, { checked: (@trigger.trigger_schedule.id.present?) }, 0, 1
%strong Register as scheduled trigger
.help-block
If checked, this trigger will be executed periodically according to `cron`, `cron_timezone` and `ref`
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment