Skip to content

Commit

Permalink
Show error messages adding invalid suspend schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
eisuke committed Jul 5, 2017
1 parent 97d2e52 commit 7cb7676
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 6 deletions.
9 changes: 9 additions & 0 deletions app/assets/javascripts/kuroko2/suspend_schedule_handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$(function(){
$("#new_job_suspend_schedule").
on("ajax:success", function(e, data, status, xhr){
$("#suspend-cron-error").hide();
}).
on("ajax:error", function(e, xhr, status, error) {
$("#suspend-cron-error").show().html("<p>Error: " + xhr.responseJSON.join(' ') + "</p>");
});
});
5 changes: 5 additions & 0 deletions app/assets/stylesheets/kuroko2/job_definitions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@
color: #888;
}
}

#suspend-cron-error {
display: none;
color: #843534;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def create
if suspend_schedule.valid?
render json: suspend_schedule, status: :created
else
render json: suspend_schedule, status: :bad_request
render json: suspend_schedule.errors.full_messages, status: :bad_request
end
end

Expand Down
4 changes: 1 addition & 3 deletions app/models/kuroko2/job_suspend_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ def validate_cron_schedule
end

if schedule.values.all?(&:empty?)
errors.add(:cron, "suspends all schedules")
errors.add(:cron, "suspends all launched schedules")
end
end
else
errors.add(:cron, "has invalid format")
end
rescue Chrono::Fields::Base::InvalidField => e
errors.add(:cron, "has invalid field: #{e.message}")
Expand Down
3 changes: 3 additions & 0 deletions app/views/kuroko2/job_suspend_schedules/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
= form_for([@definition, @suspend_schedule], remote: true) do |form|
.box-body
#suspend-cron-field.row.form-group
.col-md-12
div id="suspend-cron-error"
.col-md-8
= form.text_field :cron, class: 'form-control script-input', placeholder: '0 9 * * *'
= javascript_include_tag 'kuroko2/suspend_schedule_handler'
.col-md-4
= form.submit 'Add Suspend Schedule', class: 'btn btn-default btn-block'
2 changes: 1 addition & 1 deletion config/initializers/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# Rails.application.config.assets.precompile += %w( search.js )
Rails.application.config.assets.precompile += %w( kuroko2/instance_linker.js timeline/* network/* )
Rails.application.config.assets.precompile += %w( kuroko2/instance_linker.js kuroko2/suspend_schedule_handler.js timeline/* network/* )
10 changes: 9 additions & 1 deletion spec/features/job_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,17 @@
end
expect(page).to have_selector('#schedules table tbody tr .log', text: '* * * * *', count: 0)

fill_in 'job_schedule_cron', with: '* * * * *'
click_on 'Add Schedule'

fill_in 'job_suspend_schedule_cron', with: '* * * * *'
click_on 'Add Suspend Schedule'
expect(page).to have_selector('#suspend-schedules table tbody tr .log', text: '* * * * *', count: 1)
expect(page).to have_content('Cron suspends all launched schedules')
expect(page).to have_selector('#suspend-schedules table tbody tr .log', text: '* * * * *', count: 0)

fill_in 'job_suspend_schedule_cron', with: '* 10 * * *'
click_on 'Add Suspend Schedule'
expect(page).to have_selector('#suspend-schedules table tbody tr .log', text: '* 10 * * *', count: 1)

within '#suspend-schedules' do
click_on 'Delete'
Expand Down

0 comments on commit 7cb7676

Please sign in to comment.