From 4a142f307b7cd66f754ed466ad835875369f1a37 Mon Sep 17 00:00:00 2001 From: Denny Bresch Date: Thu, 27 Jun 2019 12:01:40 +0200 Subject: [PATCH] Fixed #2624 - Concern 'CanLatestChange' returns wrong updated_at --- .../application_model/can_latest_change.rb | 8 ++--- .../can_latest_change_examples.rb | 32 +++++++++++++++++++ spec/models/application_model_examples.rb | 2 ++ 3 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 spec/models/application_model/can_latest_change_examples.rb diff --git a/app/models/application_model/can_latest_change.rb b/app/models/application_model/can_latest_change.rb index ff66837f5..ef441fb76 100644 --- a/app/models/application_model/can_latest_change.rb +++ b/app/models/application_model/can_latest_change.rb @@ -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? diff --git a/spec/models/application_model/can_latest_change_examples.rb b/spec/models/application_model/can_latest_change_examples.rb new file mode 100644 index 000000000..e9a5f9fc3 --- /dev/null +++ b/spec/models/application_model/can_latest_change_examples.rb @@ -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 diff --git a/spec/models/application_model_examples.rb b/spec/models/application_model_examples.rb index 91040caa3..537aaf1bd 100644 --- a/spec/models/application_model_examples.rb +++ b/spec/models/application_model_examples.rb @@ -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