Commit 7322437f authored by James Fargher's avatar James Fargher

Merge branch '38096-extract-resource-events-base-class-pd' into 'master'

Create base class for resource events

Closes #38096

See merge request gitlab-org/gitlab!25791
parents 6ab2ee64 5482bce3
# frozen_string_literal: true
module ResourceEventTools
extend ActiveSupport::Concern
class ResourceEvent < ApplicationRecord
include Gitlab::Utils::StrongMemoize
include Importable
included do
belongs_to :user
self.abstract_class = true
validates :user, presence: { unless: :importing? }, on: :create
validate :exactly_one_issuable
belongs_to :user
scope :created_after, ->(time) { where('created_at > ?', time) }
def discussion_id
strong_memoize(:discussion_id) do
Digest::SHA1.hexdigest(discussion_id_key.join("-"))
end
end
private
def discussion_id_key
[self.class.name, created_at, user_id]
end
def exactly_one_issuable
......
# frozen_string_literal: true
class ResourceLabelEvent < ApplicationRecord
include Importable
include Gitlab::Utils::StrongMemoize
class ResourceLabelEvent < ResourceEvent
include CacheMarkdownField
include ResourceEventTools
cache_markdown_field :reference
......@@ -13,8 +10,11 @@ class ResourceLabelEvent < ApplicationRecord
belongs_to :label
scope :inc_relations, -> { includes(:label, :user) }
scope :by_issue, ->(issue) { where(issue_id: issue.id) }
scope :by_merge_request, ->(merge_request) { where(merge_request_id: merge_request.id) }
validates :label, presence: { unless: :importing? }, on: :create
validate :exactly_one_issuable
after_save :expire_etag_cache
after_destroy :expire_etag_cache
......@@ -41,12 +41,6 @@ class ResourceLabelEvent < ApplicationRecord
issue || merge_request
end
def discussion_id(resource = nil)
strong_memoize(:discussion_id) do
Digest::SHA1.hexdigest(discussion_id_key.join("-"))
end
end
def project
issuable.project
end
......@@ -109,10 +103,6 @@ class ResourceLabelEvent < ApplicationRecord
def resource_parent
issuable.project || issuable.group
end
def discussion_id_key
[self.class.name, created_at, user_id]
end
end
ResourceLabelEvent.prepend_if_ee('EE::ResourceLabelEvent')
# frozen_string_literal: true
class ResourceMilestoneEvent < ApplicationRecord
include Gitlab::Utils::StrongMemoize
include Importable
include ResourceEventTools
class ResourceMilestoneEvent < ResourceEvent
belongs_to :issue
belongs_to :merge_request
belongs_to :milestone
......@@ -12,6 +8,8 @@ class ResourceMilestoneEvent < ApplicationRecord
scope :by_issue, ->(issue) { where(issue_id: issue.id) }
scope :by_merge_request, ->(merge_request) { where(merge_request_id: merge_request.id) }
validate :exactly_one_issuable
enum action: {
add: 1,
remove: 2
......@@ -23,8 +21,4 @@ class ResourceMilestoneEvent < ApplicationRecord
def self.issuable_attrs
%i(issue merge_request).freeze
end
def resource
issue || merge_request
end
end
# frozen_string_literal: true
class ResourceWeightEvent < ApplicationRecord
include Gitlab::Utils::StrongMemoize
validates :user, presence: true
class ResourceWeightEvent < ResourceEvent
validates :issue, presence: true
belongs_to :user
belongs_to :issue
scope :by_issue, ->(issue) { where(issue_id: issue.id) }
scope :created_after, ->(time) { where('created_at > ?', time) }
def discussion_id(resource = nil)
strong_memoize(:discussion_id) do
Digest::SHA1.hexdigest(discussion_id_key.join("-"))
end
end
private
def discussion_id_key
[self.class.name, created_at, user_id]
end
end
......@@ -10,6 +10,10 @@ RSpec.describe ResourceLabelEvent, type: :model do
it_behaves_like 'having unique enum values'
it_behaves_like 'a resource event'
it_behaves_like 'a resource event for issues'
it_behaves_like 'a resource event for merge requests'
describe 'associations' do
it { is_expected.to belong_to(:user) }
it { is_expected.to belong_to(:issue) }
......
......@@ -3,6 +3,9 @@
require 'spec_helper'
RSpec.describe ResourceWeightEvent, type: :model do
it_behaves_like 'a resource event'
it_behaves_like 'a resource event for issues'
let_it_be(:user1) { create(:user) }
let_it_be(:user2) { create(:user) }
......@@ -11,13 +14,11 @@ RSpec.describe ResourceWeightEvent, type: :model do
let_it_be(:issue3) { create(:issue, author: user2) }
describe 'validations' do
it { is_expected.not_to allow_value(nil).for(:user) }
it { is_expected.not_to allow_value(nil).for(:issue) }
it { is_expected.to allow_value(nil).for(:weight) }
end
describe 'associations' do
it { is_expected.to belong_to(:user) }
it { is_expected.to belong_to(:issue) }
end
......
......@@ -10,8 +10,21 @@ shared_examples 'a resource event' do
let_it_be(:issue2) { create(:issue, author: user1) }
let_it_be(:issue3) { create(:issue, author: user2) }
describe 'importable' do
it { is_expected.to respond_to(:importing?) }
it { is_expected.to respond_to(:imported?) }
end
describe 'validations' do
it { is_expected.not_to allow_value(nil).for(:user) }
context 'when importing' do
before do
allow(subject).to receive(:importing?).and_return(true)
end
it { is_expected.to allow_value(nil).for(:user) }
end
end
describe 'associations' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment