Commit 5482bce3 authored by Patrick Derichs's avatar Patrick Derichs

Create base class for resource events

Derived ResourceMilestoneEvent,
ResourceWeightEvent and ResourceLabelEvent
from ResourceEvent

Also added missing specs for resource events.

Remove resource on ResourceMilestoneEvent
parent 63c48c56
# frozen_string_literal: true # frozen_string_literal: true
module ResourceEventTools class ResourceEvent < ApplicationRecord
extend ActiveSupport::Concern include Gitlab::Utils::StrongMemoize
include Importable
included do self.abstract_class = true
belongs_to :user
validates :user, presence: { unless: :importing? }, on: :create validates :user, presence: { unless: :importing? }, on: :create
validate :exactly_one_issuable belongs_to :user
scope :created_after, ->(time) { where('created_at > ?', time) } 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 end
def exactly_one_issuable def exactly_one_issuable
......
# frozen_string_literal: true # frozen_string_literal: true
class ResourceLabelEvent < ApplicationRecord class ResourceLabelEvent < ResourceEvent
include Importable
include Gitlab::Utils::StrongMemoize
include CacheMarkdownField include CacheMarkdownField
include ResourceEventTools
cache_markdown_field :reference cache_markdown_field :reference
...@@ -13,8 +10,11 @@ class ResourceLabelEvent < ApplicationRecord ...@@ -13,8 +10,11 @@ class ResourceLabelEvent < ApplicationRecord
belongs_to :label belongs_to :label
scope :inc_relations, -> { includes(:label, :user) } 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 validates :label, presence: { unless: :importing? }, on: :create
validate :exactly_one_issuable
after_save :expire_etag_cache after_save :expire_etag_cache
after_destroy :expire_etag_cache after_destroy :expire_etag_cache
...@@ -41,12 +41,6 @@ class ResourceLabelEvent < ApplicationRecord ...@@ -41,12 +41,6 @@ class ResourceLabelEvent < ApplicationRecord
issue || merge_request issue || merge_request
end end
def discussion_id(resource = nil)
strong_memoize(:discussion_id) do
Digest::SHA1.hexdigest(discussion_id_key.join("-"))
end
end
def project def project
issuable.project issuable.project
end end
...@@ -109,10 +103,6 @@ class ResourceLabelEvent < ApplicationRecord ...@@ -109,10 +103,6 @@ class ResourceLabelEvent < ApplicationRecord
def resource_parent def resource_parent
issuable.project || issuable.group issuable.project || issuable.group
end end
def discussion_id_key
[self.class.name, created_at, user_id]
end
end end
ResourceLabelEvent.prepend_if_ee('EE::ResourceLabelEvent') ResourceLabelEvent.prepend_if_ee('EE::ResourceLabelEvent')
# frozen_string_literal: true # frozen_string_literal: true
class ResourceMilestoneEvent < ApplicationRecord class ResourceMilestoneEvent < ResourceEvent
include Gitlab::Utils::StrongMemoize
include Importable
include ResourceEventTools
belongs_to :issue belongs_to :issue
belongs_to :merge_request belongs_to :merge_request
belongs_to :milestone belongs_to :milestone
...@@ -12,6 +8,8 @@ class ResourceMilestoneEvent < ApplicationRecord ...@@ -12,6 +8,8 @@ class ResourceMilestoneEvent < ApplicationRecord
scope :by_issue, ->(issue) { where(issue_id: issue.id) } scope :by_issue, ->(issue) { where(issue_id: issue.id) }
scope :by_merge_request, ->(merge_request) { where(merge_request_id: merge_request.id) } scope :by_merge_request, ->(merge_request) { where(merge_request_id: merge_request.id) }
validate :exactly_one_issuable
enum action: { enum action: {
add: 1, add: 1,
remove: 2 remove: 2
...@@ -23,8 +21,4 @@ class ResourceMilestoneEvent < ApplicationRecord ...@@ -23,8 +21,4 @@ class ResourceMilestoneEvent < ApplicationRecord
def self.issuable_attrs def self.issuable_attrs
%i(issue merge_request).freeze %i(issue merge_request).freeze
end end
def resource
issue || merge_request
end
end end
# frozen_string_literal: true # frozen_string_literal: true
class ResourceWeightEvent < ApplicationRecord class ResourceWeightEvent < ResourceEvent
include Gitlab::Utils::StrongMemoize
validates :user, presence: true
validates :issue, presence: true validates :issue, presence: true
belongs_to :user
belongs_to :issue belongs_to :issue
scope :by_issue, ->(issue) { where(issue_id: issue.id) } 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 end
...@@ -10,6 +10,10 @@ RSpec.describe ResourceLabelEvent, type: :model do ...@@ -10,6 +10,10 @@ RSpec.describe ResourceLabelEvent, type: :model do
it_behaves_like 'having unique enum values' 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 describe 'associations' do
it { is_expected.to belong_to(:user) } it { is_expected.to belong_to(:user) }
it { is_expected.to belong_to(:issue) } it { is_expected.to belong_to(:issue) }
......
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe ResourceWeightEvent, type: :model do 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(:user1) { create(:user) }
let_it_be(:user2) { create(:user) } let_it_be(:user2) { create(:user) }
...@@ -11,13 +14,11 @@ RSpec.describe ResourceWeightEvent, type: :model do ...@@ -11,13 +14,11 @@ RSpec.describe ResourceWeightEvent, type: :model do
let_it_be(:issue3) { create(:issue, author: user2) } let_it_be(:issue3) { create(:issue, author: user2) }
describe 'validations' do 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.not_to allow_value(nil).for(:issue) }
it { is_expected.to allow_value(nil).for(:weight) } it { is_expected.to allow_value(nil).for(:weight) }
end end
describe 'associations' do describe 'associations' do
it { is_expected.to belong_to(:user) }
it { is_expected.to belong_to(:issue) } it { is_expected.to belong_to(:issue) }
end end
......
...@@ -10,8 +10,21 @@ shared_examples 'a resource event' do ...@@ -10,8 +10,21 @@ shared_examples 'a resource event' do
let_it_be(:issue2) { create(:issue, author: user1) } let_it_be(:issue2) { create(:issue, author: user1) }
let_it_be(:issue3) { create(:issue, author: user2) } 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 describe 'validations' do
it { is_expected.not_to allow_value(nil).for(:user) } 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 end
describe 'associations' do 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