Class: FileNamingHelper
- Inherits:
-
Object
- Object
- FileNamingHelper
- Defined in:
- lib/file_naming_helper.rb
Overview
Normalises arbitrary user-supplied filenames into safe, ASCII-friendly
slugs while preserving the original extension.
Class Method Summary collapse
Class Method Details
.sanitize_file_name(filename) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/file_naming_helper.rb', line 5 def self.sanitize_file_name(filename) base = File.basename(filename, File.extname(filename)) ext = File.extname(filename) # Replace any non-word characters with a dash, strip extra dashes/spaces sanitized = base .gsub(/[^\w\s-]/, '') # Remove anything not word, space, or dash .gsub(/\s+/, '-').squeeze('-') # Squeeze repeated dashes .downcase # Optional: downcase "#{sanitized}#{ext}" end |