summaryrefslogtreecommitdiff
path: root/app/models/commit_status.rb
diff options
context:
space:
mode:
authorJan Provaznik <jprovaznik@gitlab.com>2018-05-31 20:32:36 +0200
committerJan Provaznik <jprovaznik@gitlab.com>2018-06-06 20:46:54 +0200
commit0665a8f730e19e180f2b4d240ca7e93408d61b12 (patch)
treedaaaecc755f15629c9e28eeaeb0e15dd2a6654ec /app/models/commit_status.rb
parent41eab9a90787162ee338fd2e3a81827a9bc923c3 (diff)
downloadgitlab-ce-0665a8f730e19e180f2b4d240ca7e93408d61b12.tar.gz
Enable mapping to nil in enums
Enum in Rails 5 does not map nil values - IOW nil value remains nil, even if there is a key with nil value in the enum definition. This commit overrides the underlying Enum methods so nil value is still mapped. This solution is far from being ideal: it uses dynamic definition of methods which introduces more magic/confusion into the codebase. It would be better to get rid of the nil value in enums.
Diffstat (limited to 'app/models/commit_status.rb')
-rw-r--r--app/models/commit_status.rb10
1 files changed, 2 insertions, 8 deletions
diff --git a/app/models/commit_status.rb b/app/models/commit_status.rb
index a7d05722287..97516079b66 100644
--- a/app/models/commit_status.rb
+++ b/app/models/commit_status.rb
@@ -3,6 +3,7 @@ class CommitStatus < ActiveRecord::Base
include Importable
include AfterCommitQueue
include Presentable
+ include EnumWithNil
self.table_name = 'ci_builds'
@@ -39,7 +40,7 @@ class CommitStatus < ActiveRecord::Base
scope :retried_ordered, -> { retried.ordered.includes(project: :namespace) }
scope :after_stage, -> (index) { where('stage_idx > ?', index) }
- enum failure_reason: {
+ enum_with_nil failure_reason: {
unknown_failure: nil,
script_failure: 1,
api_failure: 2,
@@ -190,11 +191,4 @@ class CommitStatus < ActiveRecord::Base
v =~ /\d+/ ? v.to_i : v
end
end
-
- # Rails 5.0 autogenerated question mark enum methods return wrong result if enum value is nil.
- # They always return `false`.
- # This method overwrites the autogenerated one to return correct result.
- def unknown_failure?
- Gitlab.rails5? ? failure_reason.nil? : super
- end
end