class MaterialSyncSuppressionRegistrar def self.create! attributes, created_by_user: new(attributes, created_by_user:).create! end def initialize attributes, created_by_user: @attributes = attributes @created_by_user = created_by_user end def create! suppression = nil MaterialSyncSuppression.transaction do suppression = MaterialSyncSuppression.create!( @attributes.merge(created_by_user: @created_by_user)) discard_existing_materials!(suppression) end suppression end private def discard_existing_materials! suppression Material.unscoped .kept .where(normalized_source_key: suppression.normalized_source_key) .find_each do |material| MaterialVersionRecorder.ensure_snapshot!(material, created_by_user: @created_by_user) material.discard! MaterialVersionRecorder.record!(material:, event_type: :discard, created_by_user: @created_by_user) end end end