aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_storage/filename.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2017-07-06 11:33:29 +0200
committerDavid Heinemeier Hansson <david@loudthinking.com>2017-07-06 11:33:29 +0200
commitc624df326a4ef36919a5195a3c5509fab97dcba3 (patch)
treea8e07aabde7548d5bd4a322a9898ad123cfa40f7 /lib/active_storage/filename.rb
parent5869045f2e71f0abdf3add19629d23a46b9fff0d (diff)
downloadrails-c624df326a4ef36919a5195a3c5509fab97dcba3.tar.gz
rails-c624df326a4ef36919a5195a3c5509fab97dcba3.tar.bz2
rails-c624df326a4ef36919a5195a3c5509fab97dcba3.zip
ActiveVault -> ActiveStorage
Yaroslav agreed to hand over the gem name ❤️
Diffstat (limited to 'lib/active_storage/filename.rb')
-rw-r--r--lib/active_storage/filename.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/active_storage/filename.rb b/lib/active_storage/filename.rb
new file mode 100644
index 0000000000..71614b5113
--- /dev/null
+++ b/lib/active_storage/filename.rb
@@ -0,0 +1,31 @@
+class ActiveStorage::Filename
+ include Comparable
+
+ def initialize(filename)
+ @filename = filename
+ end
+
+ def extname
+ File.extname(@filename)
+ end
+
+ def extension
+ extname.from(1)
+ end
+
+ def base
+ File.basename(@filename, extname)
+ end
+
+ def sanitized
+ @filename.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "�").strip.tr("\u{202E}%$|:;/\t\r\n\\", "-")
+ end
+
+ def to_s
+ sanitized.to_s
+ end
+
+ def <=>(other)
+ to_s.downcase <=> other.to_s.downcase
+ end
+end