diff options
author | Ruben Davila <rdavila84@gmail.com> | 2017-01-26 04:16:09 +0300 |
---|---|---|
committer | Ruben Davila <rdavila84@gmail.com> | 2017-02-07 18:41:44 +0300 |
commit | bdc932245088b3a7ae5d633e81175352d5599083 (patch) | |
tree | f98c8d57c6f7d1f9e1d6a2a2eda8835fd9c2f0be /app | |
parent | 8abdabdb3ad9e4b87893f24042de077cd4a7d791 (diff) |
Use normal associations instead of polymorphic.
We can't properly use foreign keys on columns that are configured for
polymorphic associations which has disadvantages related to data
integrity and storage. Given we only use time tracking for Issues and
Merge Requests we're moving to the usage of regular associations.
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 |