Expire ETag cache on note when award emoji changes

This commit is contained in:
Sean McGivern 2017-07-17 19:36:29 +01:00 committed by Fatih Acet
parent aed5632ca4
commit db0b7fb39e
3 changed files with 56 additions and 11 deletions

View file

@ -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

View file

@ -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

View file

@ -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