mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Merge pull request #12601 from cyrillefr/FixRailsRelativeDateConstant
Fixes Rails/RelativeDateConstant offense
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user