Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Davila <rdavila84@gmail.com>2017-01-26 04:16:09 +0300
committerRuben Davila <rdavila84@gmail.com>2017-02-07 18:41:44 +0300
commitbdc932245088b3a7ae5d633e81175352d5599083 (patch)
treef98c8d57c6f7d1f9e1d6a2a2eda8835fd9c2f0be /app/models/timelog.rb
parent8abdabdb3ad9e4b87893f24042de077cd4a7d791 (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/models/timelog.rb')
-rw-r--r--app/models/timelog.rb18
1 files changed, 17 insertions, 1 deletions
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