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 ...@@ -16,6 +16,7 @@ class Projects::TriggersController < Projects::ApplicationController
if @trigger.valid? if @trigger.valid?
flash[:notice] = 'Trigger was created successfully.' flash[:notice] = 'Trigger was created successfully.'
else else
puts "@trigger.errors: #{@trigger.errors.inspect}"
flash[:alert] = 'You could not create a new trigger.' flash[:alert] = 'You could not create a new trigger.'
end end
...@@ -33,6 +34,7 @@ class Projects::TriggersController < Projects::ApplicationController ...@@ -33,6 +34,7 @@ class Projects::TriggersController < Projects::ApplicationController
end end
def edit def edit
@trigger.build_trigger_schedule unless @trigger.trigger_schedule.present?
end end
def update def update
...@@ -69,15 +71,15 @@ class Projects::TriggersController < Projects::ApplicationController ...@@ -69,15 +71,15 @@ class Projects::TriggersController < Projects::ApplicationController
def create_params def create_params
params.require(:trigger).permit( params.require(:trigger).permit(
:description, :ref, :trigger_schedule_on, :description, :ref,
trigger_schedule_attributes: [ :cron, :cron_timezone ] trigger_schedule_attributes: [ :cron, :cron_timezone, :_destroy ]
) )
end end
def update_params def update_params
params.require(:trigger).permit( params.require(:trigger).permit(
:description, :ref, :trigger_schedule_on, :description, :ref,
trigger_schedule_attributes: [ :cron, :cron_timezone ] trigger_schedule_attributes: [ :cron, :cron_timezone, :_destroy ]
) )
end end
end end
...@@ -8,27 +8,16 @@ module Ci ...@@ -8,27 +8,16 @@ module Ci
belongs_to :owner, class_name: "User" belongs_to :owner, class_name: "User"
has_many :trigger_requests, dependent: :destroy 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 validates :token, presence: true, uniqueness: true
before_validation :set_default_values before_validation :set_default_values
accepts_nested_attributes_for :trigger_schedule accepts_nested_attributes_for :trigger_schedule, allow_destroy: true
attr_accessor :trigger_schedule_on
def set_default_values def set_default_values
self.token = SecureRandom.hex(15) if self.token.blank? 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 end
def last_trigger_request def last_trigger_request
......
...@@ -6,11 +6,11 @@ module Ci ...@@ -6,11 +6,11 @@ module Ci
acts_as_paranoid acts_as_paranoid
belongs_to :project belongs_to :project
belongs_to :trigger belongs_to :trigger, inverse_of: :trigger_schedule
delegate :ref, to: :trigger, allow_nil: true 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, cron: true, presence: { unless: :importing? }
validates :cron_timezone, cron_timezone: true, presence: { unless: :importing? } validates :cron_timezone, cron_timezone: true, presence: { unless: :importing? }
validates :ref, presence: { unless: :importing? } validates :ref, presence: { unless: :importing? }
...@@ -19,6 +19,7 @@ module Ci ...@@ -19,6 +19,7 @@ module Ci
def set_next_run_at def set_next_run_at
self.next_run_at = Gitlab::Ci::CronParser.new(cron, cron_timezone).next_time_from(Time.now) self.next_run_at = Gitlab::Ci::CronParser.new(cron, cron_timezone).next_time_from(Time.now)
self.project = trigger.project
end end
def schedule_next_run! def schedule_next_run!
......
...@@ -18,13 +18,10 @@ ...@@ -18,13 +18,10 @@
.form-group .form-group
= f.label :ref, "Ref (For scheduled trigger)", class: "label-light" = 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" = 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 .form-group
.checkbox .checkbox
= f.label :trigger_schedule_on do = schedule_fields.label :_destroy do
= f.check_box :trigger_schedule_on = schedule_fields.check_box :_destroy, { checked: (@trigger.trigger_schedule.id.present?) }, 0, 1
%strong Register as scheduled trigger %strong Register as scheduled trigger
.help-block .help-block
If checked, this trigger will be executed periodically according to `cron`, `cron_timezone` and `ref` 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