Fixed #2624 - Concern 'CanLatestChange' returns wrong updated_at

This commit is contained in:
Denny Bresch 2019-06-27 12:01:40 +02:00 committed by Thorsten Eckel
parent 812266b80e
commit 4a142f307b
3 changed files with 38 additions and 4 deletions

View file

@ -9,7 +9,7 @@ module ApplicationModel::CanLatestChange
get latest updated_at object timestamp
latest_change = Ticket.latest_change
latest_change = object.latest_change
returns
@ -18,13 +18,13 @@ returns
=end
def latest_change
key = "#{new.class.name}_latest_change"
key = "#{name}_latest_change"
updated_at = Cache.get(key)
return updated_at if updated_at
# if we do not have it cached, do lookup
updated_at = Ticket.order(updated_at: :desc).limit(1).pluck(:updated_at).first
updated_at = order(updated_at: :desc).limit(1).pluck(:updated_at).first
return if !updated_at
@ -33,7 +33,7 @@ returns
end
def latest_change_set(updated_at)
key = "#{new.class.name}_latest_change"
key = "#{name}_latest_change"
expires_in = 86_400 # 1 day
if updated_at.nil?

View file

@ -0,0 +1,32 @@
RSpec.shared_examples 'ApplicationModel::CanLatestChange' do
subject { create(described_class.name.underscore) }
describe '#latest_change' do
describe 'caching updated_at' do
context 'with empty cache' do
it 'stores updated_at in the cache and returns it' do
expect(subject.updated_at).to eq(described_class.latest_change)
end
end
context 'with valid cache' do
before { described_class.latest_change_set(subject.updated_at) }
it 'return updated_at from cache' do
expect(subject.updated_at).to eq(described_class.latest_change)
end
end
context 'delete valid cache' do
before do
subject.touch
described_class.latest_change_set(nil)
end
it 'stores new updated_at in the cache and returns it' do
expect(subject.updated_at).to eq(described_class.latest_change)
end
end
end
end
end

View file

@ -1,9 +1,11 @@
require 'models/application_model/can_assets_examples'
require 'models/application_model/can_associations_examples'
require 'models/application_model/can_latest_change_examples'
require 'models/application_model/checks_import_examples'
RSpec.shared_examples 'ApplicationModel' do |options = {}|
include_examples 'ApplicationModel::CanAssets', options[:can_assets]
include_examples 'ApplicationModel::CanAssociations'
include_examples 'ApplicationModel::CanLatestChange'
include_examples 'ApplicationModel::ChecksImport'
end