class Stall::CartsCleaner

Attributes

cart_model[R]

Public Class Methods

new(cart_model) click to toggle source
# File lib/stall/carts_cleaner.rb, line 5
def initialize(cart_model)
  @cart_model = cart_model
end

Public Instance Methods

clean!() click to toggle source
# File lib/stall/carts_cleaner.rb, line 9
def clean!
  clean_empty_carts
  clean_aborted_carts
end

Private Instance Methods

clean_aborted_carts() click to toggle source

Unpaid carts have line items and other models related. Since empty carts are already cleaned, there should not be too many carts to destroy here and we can safely use .destroy_all to deeply clean carts.

Note : The given cart model should implement the `.unpaid` method

# File lib/stall/carts_cleaner.rb, line 34
def clean_aborted_carts
  carts = cart_model.aborted

  log "Cleaning #{ carts.count } aborted carts ..."
  carts.destroy_all
  log "Done."
end
clean_empty_carts() click to toggle source

Empty carts are cleaned with a .delete_all call for optimization.

Empty carts should not need to run destroy callbacks, not being related to any external model.

# File lib/stall/carts_cleaner.rb, line 21
def clean_empty_carts
  carts = cart_model.empty.older_than(Stall.config.empty_carts_expires_after.ago)

  log "Cleaning #{ carts.count } empty carts ..."
  carts.destroy_all
  log "Done."
end
log(*args) click to toggle source
# File lib/stall/carts_cleaner.rb, line 42
def log(*args)
  puts(*args) unless Rails.env.test?
end