Skip to content
Snippets Groups Projects
Commit f29c3047 authored by James Lopez's avatar James Lopez
Browse files

use has_many relationship with events

parent d6d0a355
No related branches found
No related tags found
1 merge request!4987Fixing problems with events for import/export
Pipeline #
module Eventable
extend ActiveSupport::Concern
def events
Event.where(target_id: id, target_type: self.class.to_s)
end
def events=(events)
events.each do |event|
event.target_id = id
event.data.deep_symbolize_keys! if event.data
event.save!
end
end
end
Loading
@@ -6,7 +6,6 @@ class Issue < ActiveRecord::Base
Loading
@@ -6,7 +6,6 @@ class Issue < ActiveRecord::Base
include Referable include Referable
include Sortable include Sortable
include Taskable include Taskable
include Eventable
   
DueDateStruct = Struct.new(:title, :name).freeze DueDateStruct = Struct.new(:title, :name).freeze
NoDueDate = DueDateStruct.new('No Due Date', '0').freeze NoDueDate = DueDateStruct.new('No Due Date', '0').freeze
Loading
@@ -20,6 +19,8 @@ class Issue < ActiveRecord::Base
Loading
@@ -20,6 +19,8 @@ class Issue < ActiveRecord::Base
belongs_to :project belongs_to :project
belongs_to :moved_to, class_name: 'Issue' belongs_to :moved_to, class_name: 'Issue'
   
has_many :events, as: :target, dependent: :destroy
validates :project, presence: true validates :project, presence: true
   
scope :cared, ->(user) { where(assignee_id: user) } scope :cared, ->(user) { where(assignee_id: user) }
Loading
Loading
Loading
@@ -5,7 +5,6 @@ class MergeRequest < ActiveRecord::Base
Loading
@@ -5,7 +5,6 @@ class MergeRequest < ActiveRecord::Base
include Sortable include Sortable
include Taskable include Taskable
include Importable include Importable
include Eventable
   
belongs_to :target_project, foreign_key: :target_project_id, class_name: "Project" belongs_to :target_project, foreign_key: :target_project_id, class_name: "Project"
belongs_to :source_project, foreign_key: :source_project_id, class_name: "Project" belongs_to :source_project, foreign_key: :source_project_id, class_name: "Project"
Loading
@@ -13,6 +12,8 @@ class MergeRequest < ActiveRecord::Base
Loading
@@ -13,6 +12,8 @@ class MergeRequest < ActiveRecord::Base
   
has_one :merge_request_diff, dependent: :destroy has_one :merge_request_diff, dependent: :destroy
   
has_many :events, as: :target, dependent: :destroy
serialize :merge_params, Hash serialize :merge_params, Hash
   
after_create :create_merge_request_diff, unless: :importing after_create :create_merge_request_diff, unless: :importing
Loading
Loading
Loading
@@ -11,13 +11,13 @@ class Milestone < ActiveRecord::Base
Loading
@@ -11,13 +11,13 @@ class Milestone < ActiveRecord::Base
include Referable include Referable
include StripAttribute include StripAttribute
include Milestoneish include Milestoneish
include Eventable
   
belongs_to :project belongs_to :project
has_many :issues has_many :issues
has_many :labels, -> { distinct.reorder('labels.title') }, through: :issues has_many :labels, -> { distinct.reorder('labels.title') }, through: :issues
has_many :merge_requests has_many :merge_requests
has_many :participants, -> { distinct.reorder('users.name') }, through: :issues, source: :assignee has_many :participants, -> { distinct.reorder('users.name') }, through: :issues, source: :assignee
has_many :events, as: :target, dependent: :destroy
   
scope :active, -> { with_state(:active) } scope :active, -> { with_state(:active) }
scope :closed, -> { with_state(:closed) } scope :closed, -> { with_state(:closed) }
Loading
Loading
Loading
@@ -5,7 +5,6 @@ class Note < ActiveRecord::Base
Loading
@@ -5,7 +5,6 @@ class Note < ActiveRecord::Base
include Mentionable include Mentionable
include Awardable include Awardable
include Importable include Importable
include Eventable
   
# Attribute containing rendered and redacted Markdown as generated by # Attribute containing rendered and redacted Markdown as generated by
# Banzai::ObjectRenderer. # Banzai::ObjectRenderer.
Loading
@@ -22,6 +21,7 @@ class Note < ActiveRecord::Base
Loading
@@ -22,6 +21,7 @@ class Note < ActiveRecord::Base
belongs_to :updated_by, class_name: "User" belongs_to :updated_by, class_name: "User"
   
has_many :todos, dependent: :destroy has_many :todos, dependent: :destroy
has_many :events, as: :target, dependent: :destroy
   
delegate :gfm_reference, :local_reference, to: :noteable delegate :gfm_reference, :local_reference, to: :noteable
delegate :name, to: :project, prefix: true delegate :name, to: :project, prefix: true
Loading
Loading
Loading
@@ -33,6 +33,7 @@ module Gitlab
Loading
@@ -33,6 +33,7 @@ module Gitlab
update_user_references update_user_references
update_project_references update_project_references
reset_ci_tokens if @relation_name == 'Ci::Trigger' reset_ci_tokens if @relation_name == 'Ci::Trigger'
@relation_hash['data'].deep_symbolize_keys! if @relation_name == :events && @relation_hash['data']
   
generate_imported_object generate_imported_object
end end
Loading
Loading
Loading
@@ -24,6 +24,12 @@ describe Gitlab::ImportExport::ProjectTreeRestorer, services: true do
Loading
@@ -24,6 +24,12 @@ describe Gitlab::ImportExport::ProjectTreeRestorer, services: true do
   
expect(Ci::Pipeline.first.notes).not_to be_empty expect(Ci::Pipeline.first.notes).not_to be_empty
end end
it 'restores the correct event' do
restored_project_json
expect(Event.where.not(data: nil).first.data[:ref]).not_to be_empty
end
end end
end end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment