diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-12-05 15:09:43 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-12-05 15:09:43 +0300 |
commit | 519d1054bcd894f514cc5c607a42be336d05df48 (patch) | |
tree | 3c3e78f47ab9ea95d0d2ff08aeeae5a6b3f405ea | |
parent | c6c21dd4566899ca85fa3754833dfd0c70f1768a (diff) |
Add custom validation message for chronic duration attribute
-rw-r--r-- | app/models/ci/runner.rb | 3 | ||||
-rw-r--r-- | app/models/concerns/chronic_duration_attribute.rb | 2 | ||||
-rw-r--r-- | app/models/project.rb | 3 | ||||
-rw-r--r-- | app/validators/duration_validator.rb | 6 | ||||
-rw-r--r-- | spec/models/concerns/chronic_duration_attribute_spec.rb | 3 |
5 files changed, 12 insertions, 5 deletions
diff --git a/app/models/ci/runner.rb b/app/models/ci/runner.rb index 260348c97b2..2693386443a 100644 --- a/app/models/ci/runner.rb +++ b/app/models/ci/runner.rb @@ -114,7 +114,8 @@ module Ci cached_attr_reader :version, :revision, :platform, :architecture, :ip_address, :contacted_at - chronic_duration_attr :maximum_timeout_human_readable, :maximum_timeout + chronic_duration_attr :maximum_timeout_human_readable, :maximum_timeout, + error_message: 'Maximum job timeout has a value which could not be accepted' validates :maximum_timeout, allow_nil: true, numericality: { greater_than_or_equal_to: 600, diff --git a/app/models/concerns/chronic_duration_attribute.rb b/app/models/concerns/chronic_duration_attribute.rb index edf6ac96730..af4905115b1 100644 --- a/app/models/concerns/chronic_duration_attribute.rb +++ b/app/models/concerns/chronic_duration_attribute.rb @@ -24,7 +24,7 @@ module ChronicDurationAttribute end end - validates virtual_attribute, allow_nil: true, duration: true + validates virtual_attribute, allow_nil: true, duration: { message: parameters[:error_message] } end alias_method :chronic_duration_attr, :chronic_duration_attr_writer diff --git a/app/models/project.rb b/app/models/project.rb index 6ec323b58c2..d8ce8038e92 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -384,7 +384,8 @@ class Project < ActiveRecord::Base enum auto_cancel_pending_pipelines: { disabled: 0, enabled: 1 } - chronic_duration_attr :build_timeout_human_readable, :build_timeout, default: 3600 + chronic_duration_attr :build_timeout_human_readable, :build_timeout, + default: 3600, error_message: 'Maximum job timeout has a value which could not be accepted' validates :build_timeout, allow_nil: true, numericality: { greater_than_or_equal_to: 10.minutes, diff --git a/app/validators/duration_validator.rb b/app/validators/duration_validator.rb index 811828169ca..defd28d7d3b 100644 --- a/app/validators/duration_validator.rb +++ b/app/validators/duration_validator.rb @@ -14,6 +14,10 @@ class DurationValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) ChronicDuration.parse(value) rescue ChronicDuration::DurationParseError - record.errors.add(attribute, "is not a correct duration") + if options[:message] + record.errors.add(:base, options[:message]) + else + record.errors.add(attribute, "is not a correct duration") + end end end diff --git a/spec/models/concerns/chronic_duration_attribute_spec.rb b/spec/models/concerns/chronic_duration_attribute_spec.rb index 8847623f705..b14b773b653 100644 --- a/spec/models/concerns/chronic_duration_attribute_spec.rb +++ b/spec/models/concerns/chronic_duration_attribute_spec.rb @@ -54,7 +54,8 @@ shared_examples 'ChronicDurationAttribute writer' do subject.send("#{virtual_field}=", '-10m') expect(subject.valid?).to be_falsey - expect(subject.errors&.messages).to include(virtual_field => ['is not a correct duration']) + expect(subject.errors&.messages) + .to include(base: ['Maximum job timeout has a value which could not be accepted']) end end |