diff --git a/app/controllers/api/v0/products_controller.rb b/app/controllers/api/v0/products_controller.rb index eead37ca49..1a72047d73 100644 --- a/app/controllers/api/v0/products_controller.rb +++ b/app/controllers/api/v0/products_controller.rb @@ -42,6 +42,7 @@ module Api authorize! :delete, Spree::Product @product = product_finder.find_product authorize! :delete, @product + @product.destroyed_by = current_api_user @product.destroy render json: @product, serializer: Api::Admin::ProductSerializer, status: :no_content end diff --git a/app/models/concerns/log_destroy_performer.rb b/app/models/concerns/log_destroy_performer.rb new file mode 100644 index 0000000000..d53d8ce3bd --- /dev/null +++ b/app/models/concerns/log_destroy_performer.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'active_support/concern' + +module LogDestroyPerformer + extend ActiveSupport::Concern + included do + attr_accessor :destroyed_by + + after_destroy :log_who_destroyed + + def log_who_destroyed + return if destroyed_by.nil? + + Rails.logger.info "#{self.class} #{id} deleted by #{destroyed_by.id}" + end + end +end diff --git a/app/models/spree/product.rb b/app/models/spree/product.rb index 4fd670f90e..d760c329ec 100755 --- a/app/models/spree/product.rb +++ b/app/models/spree/product.rb @@ -23,6 +23,7 @@ require 'open_food_network/property_merge' module Spree class Product < ApplicationRecord include ProductStock + include LogDestroyPerformer self.belongs_to_required_by_default = false