diff options
author | Rubén Dávila Santos <ruben@gitlab.com> | 2017-02-07 21:31:13 +0300 |
---|---|---|
committer | Rubén Dávila Santos <ruben@gitlab.com> | 2017-02-07 21:31:13 +0300 |
commit | d3aaa1a2a0313b7132479d4211e406fde5bc9324 (patch) | |
tree | 1aa16c5a84afbe0ad3c6d215ffe3fd285970c6fc /app | |
parent | b0767e82c27dfdb3847a61457815550adaebf028 (diff) | |
parent | 85a98fd40fbfbc4657ca910e8885015e50ca105d (diff) |
Merge branch '26908-add-foreign-keys-to-timelogs' into 'master'
Use normal associations instead of polymorphic
Closes #26908
See merge request !8769
Diffstat (limited to 'app')
-rw-r--r-- | app/models/concerns/time_trackable.rb | 2 | ||||
-rw-r--r-- | app/models/timelog.rb | 18 |
2 files changed, 18 insertions, 2 deletions
diff --git a/app/models/concerns/time_trackable.rb b/app/models/concerns/time_trackable.rb index 040e3a2884e..9cf83440784 100644 --- a/app/models/concerns/time_trackable.rb +++ b/app/models/concerns/time_trackable.rb @@ -18,7 +18,7 @@ module TimeTrackable validates :time_estimate, numericality: { message: 'has an invalid format' }, allow_nil: false validate :check_negative_time_spent - has_many :timelogs, as: :trackable, dependent: :destroy + has_many :timelogs, dependent: :destroy end def spend_time(options) diff --git a/app/models/timelog.rb b/app/models/timelog.rb index f768c4e3da5..e166cf69703 100644 --- a/app/models/timelog.rb +++ b/app/models/timelog.rb @@ -1,6 +1,22 @@ class Timelog < ActiveRecord::Base validates :time_spent, :user, presence: true + validate :issuable_id_is_present - belongs_to :trackable, polymorphic: true + belongs_to :issue + belongs_to :merge_request belongs_to :user + + def issuable + issue || merge_request + end + + private + + def issuable_id_is_present + if issue_id && merge_request_id + errors.add(:base, 'Only Issue ID or Merge Request ID is required') + elsif issuable.nil? + errors.add(:base, 'Issue or Merge Request ID is required') + end + end end |