From 659111932c24fd9ea8f1c4a1edc330199275df9e Mon Sep 17 00:00:00 2001 From: cyrillefr Date: Fri, 21 Jun 2024 23:04:33 +0200 Subject: [PATCH 1/2] Fixes Rails/RelativeDateConstant offense - Cop: Rails/RelativeDateConstant - raises offense if Constant is relative data (ie: since, ago) - Reason: relative data will be evaluated only once - BUT here, Date should not be evaluated in a class method, and have a different - value for each call. But the data should be the same for an instance - Therefore: move the ago in init method - Cf. https://docs.rubocop.org/rubocop-rails/cops_rails.html#railsrelativedateconstant - Since there is no constant to be called form a class, but a date from an instance, the spec has been modified accordingly. The RemoveTransientData.new.call had to be splitted. --- .rubocop_todo.yml | 6 ---- lib/tasks/data/remove_transient_data.rb | 16 ++++++--- .../tasks/data/remove_transient_data_spec.rb | 35 +++++++++---------- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 04deca0de9..f9e352aef2 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -654,12 +654,6 @@ Rails/PluckInWhere: Exclude: - 'app/models/spree/variant.rb' -# Offense count: 1 -# This cop supports unsafe autocorrection (--autocorrect-all). -Rails/RelativeDateConstant: - Exclude: - - 'lib/tasks/data/remove_transient_data.rb' - # Offense count: 56 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: Include. diff --git a/lib/tasks/data/remove_transient_data.rb b/lib/tasks/data/remove_transient_data.rb index 1a519fae8f..a5a330642e 100644 --- a/lib/tasks/data/remove_transient_data.rb +++ b/lib/tasks/data/remove_transient_data.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class RemoveTransientData - RETENTION_PERIOD = 3.months.ago.to_date + RETENTION_PERIOD = 3.months # This model lets us operate on the sessions DB table using ActiveRecord's # methods within the scope of this service. This relies on the AR's @@ -9,12 +9,18 @@ class RemoveTransientData class Session < ApplicationRecord end + attr_reader :expiration_date + + def initialize + @expiration_date = RETENTION_PERIOD.ago.to_date + end + def call Rails.logger.info("#{self.class.name}: processing") - Spree::StateChange.where("created_at < ?", RETENTION_PERIOD).delete_all - Spree::LogEntry.where("created_at < ?", RETENTION_PERIOD).delete_all - Session.where("updated_at < ?", RETENTION_PERIOD).delete_all + Spree::StateChange.where("created_at < ?", @expiration_date).delete_all + Spree::LogEntry.where("created_at < ?", @expiration_date).delete_all + Session.where("updated_at < ?", @expiration_date).delete_all clear_old_cart_data! end @@ -23,7 +29,7 @@ class RemoveTransientData def clear_old_cart_data! old_carts = Spree::Order. - where("spree_orders.state = 'cart' AND spree_orders.updated_at < ?", RETENTION_PERIOD). + where("spree_orders.state = 'cart' AND spree_orders.updated_at < ?", @expiration_date). merge(orders_without_payments) old_cart_line_items = Spree::LineItem.where(order_id: old_carts) diff --git a/spec/lib/tasks/data/remove_transient_data_spec.rb b/spec/lib/tasks/data/remove_transient_data_spec.rb index ff2667d749..e568453da4 100644 --- a/spec/lib/tasks/data/remove_transient_data_spec.rb +++ b/spec/lib/tasks/data/remove_transient_data_spec.rb @@ -5,8 +5,6 @@ require 'tasks/data/remove_transient_data' RSpec.describe RemoveTransientData do describe '#call' do - let(:retention_period) { RemoveTransientData::RETENTION_PERIOD } - before do allow(Spree::StateChange).to receive(:delete_all) allow(Spree::LogEntry).to receive(:delete_all) @@ -14,24 +12,28 @@ RSpec.describe RemoveTransientData do allow(Rails.logger).to receive(:info) end - it 'deletes state changes older than rentention_period' do - Spree::StateChange.create(created_at: retention_period - 1.day) + it 'deletes state changes older than retention period' do + remover = RemoveTransientData.new + Spree::StateChange.create(created_at: remover.expiration_date - 1.day) + remover.call - RemoveTransientData.new.call expect(Spree::StateChange.all).to be_empty end - it 'deletes log entries older than retention_period' do - Spree::LogEntry.create(created_at: retention_period - 1.day) + it 'deletes log entries older than retention period' do + remover = RemoveTransientData.new + Spree::LogEntry.create(created_at: remover.expiration_date - 1.day) - expect { RemoveTransientData.new.call } + expect { remover.call } .to change { Spree::LogEntry.count }.by(-1) end - it 'deletes sessions older than retention_period' do - RemoveTransientData::Session.create(session_id: 1, updated_at: retention_period - 1.day) + it 'deletes sessions older than retention period' do + remover = RemoveTransientData.new + RemoveTransientData::Session.create(session_id: 1, + updated_at: remover.expiration_date - 1.day) - RemoveTransientData.new.call + remover.call expect(RemoveTransientData::Session.all).to be_empty end @@ -44,16 +46,13 @@ RSpec.describe RemoveTransientData do let!(:line_item) { create(:line_item, order: cart, variant:) } let!(:adjustment) { create(:adjustment, order: cart) } - let!(:old_cart) { create(:order, state: 'cart', updated_at: retention_period - 1.day) } + let!(:remover) { RemoveTransientData.new } + let!(:old_cart) { create(:order, state: 'cart', updated_at: remover.expiration_date - 1.day) } let!(:old_line_item) { create(:line_item, order: old_cart, variant:) } let!(:old_adjustment) { create(:adjustment, order: old_cart) } - before do - old_cart.update_columns(updated_at: retention_period - 1.day) - end - - it 'deletes cart orders and related objects older than retention_period' do - RemoveTransientData.new.call + it 'deletes cart orders and related objects older than retention period' do + remover.call expect{ cart.reload }.not_to raise_error expect{ line_item.reload }.not_to raise_error From 980ce95ab208a3a478c906d35ecf668acb230143 Mon Sep 17 00:00:00 2001 From: cyrillefr Date: Mon, 24 Jun 2024 10:37:43 +0200 Subject: [PATCH 2/2] Requested changes - replace direct instance variable use by instance getter use --- lib/tasks/data/remove_transient_data.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/tasks/data/remove_transient_data.rb b/lib/tasks/data/remove_transient_data.rb index a5a330642e..ffe136cd08 100644 --- a/lib/tasks/data/remove_transient_data.rb +++ b/lib/tasks/data/remove_transient_data.rb @@ -18,9 +18,9 @@ class RemoveTransientData def call Rails.logger.info("#{self.class.name}: processing") - Spree::StateChange.where("created_at < ?", @expiration_date).delete_all - Spree::LogEntry.where("created_at < ?", @expiration_date).delete_all - Session.where("updated_at < ?", @expiration_date).delete_all + Spree::StateChange.where("created_at < ?", expiration_date).delete_all + Spree::LogEntry.where("created_at < ?", expiration_date).delete_all + Session.where("updated_at < ?", expiration_date).delete_all clear_old_cart_data! end @@ -29,7 +29,7 @@ class RemoveTransientData def clear_old_cart_data! old_carts = Spree::Order. - where("spree_orders.state = 'cart' AND spree_orders.updated_at < ?", @expiration_date). + where("spree_orders.state = 'cart' AND spree_orders.updated_at < ?", expiration_date). merge(orders_without_payments) old_cart_line_items = Spree::LineItem.where(order_id: old_carts)