提交 db0b7fb3 编写于 作者: S Sean McGivern 提交者: Fatih Acet

Expire ETag cache on note when award emoji changes

上级 aed5632c
......@@ -17,6 +17,9 @@ class AwardEmoji < ActiveRecord::Base
scope :downvotes, -> { where(name: DOWNVOTE_NAME) }
scope :upvotes, -> { where(name: UPVOTE_NAME) }
after_save :expire_etag_cache
after_destroy :expire_etag_cache
class << self
def votes_for_collection(ids, type)
select('name', 'awardable_id', 'COUNT(*) as count')
......@@ -32,4 +35,10 @@ class AwardEmoji < ActiveRecord::Base
def upvote?
self.name == UPVOTE_NAME
end
def expire_etag_cache
return unless awardable.is_a?(Note)
awardable.expire_etag_cache
end
end
......@@ -299,6 +299,17 @@ class Note < ActiveRecord::Base
end
end
def expire_etag_cache
return unless for_issue?
key = Gitlab::Routing.url_helpers.project_noteable_notes_path(
noteable.project,
target_type: noteable_type.underscore,
target_id: noteable.id
)
Gitlab::EtagCaching::Store.new.touch(key)
end
private
def keep_around_commit
......@@ -326,15 +337,4 @@ class Note < ActiveRecord::Base
def set_discussion_id
self.discussion_id ||= discussion_class.discussion_id(self)
end
def expire_etag_cache
return unless for_issue?
key = Gitlab::Routing.url_helpers.project_noteable_notes_path(
noteable.project,
target_type: noteable_type.underscore,
target_id: noteable.id
)
Gitlab::EtagCaching::Store.new.touch(key)
end
end
......@@ -41,4 +41,40 @@ describe AwardEmoji, models: true do
end
end
end
describe 'expiring ETag cache' do
context 'on a note' do
let(:note) { create(:note_on_issue) }
let(:award_emoji) { build(:award_emoji, user: build(:user), awardable: note) }
it 'calls expire_etag_cache on the note when saved' do
expect(note).to receive(:expire_etag_cache)
award_emoji.save!
end
it 'calls expire_etag_cache on the note when destroyed' do
expect(note).to receive(:expire_etag_cache)
award_emoji.destroy!
end
end
context 'on another awardable' do
let(:issue) { create(:issue) }
let(:award_emoji) { build(:award_emoji, user: build(:user), awardable: issue) }
it 'does not call expire_etag_cache on the issue when saved' do
expect(issue).not_to receive(:expire_etag_cache)
award_emoji.save!
end
it 'does not call expire_etag_cache on the issue when destroyed' do
expect(issue).not_to receive(:expire_etag_cache)
award_emoji.destroy!
end
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册