aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/app
diff options
context:
space:
mode:
authorGeorge Claghorn <george@basecamp.com>2017-09-20 15:34:04 -0400
committerGeorge Claghorn <george@basecamp.com>2017-09-20 15:34:04 -0400
commit91edf754c4ccdb55bfebb2fcb1458ca0a4d769d9 (patch)
tree958a9961550b650abbb0c8b0bf8b0501a895e322 /activestorage/app
parenteddb9baf39bce0608b3364339ec171332a315d9f (diff)
downloadrails-91edf754c4ccdb55bfebb2fcb1458ca0a4d769d9.tar.gz
rails-91edf754c4ccdb55bfebb2fcb1458ca0a4d769d9.tar.bz2
rails-91edf754c4ccdb55bfebb2fcb1458ca0a4d769d9.zip
Flesh out ActiveStorage::Filename docs
Diffstat (limited to 'activestorage/app')
-rw-r--r--activestorage/app/models/active_storage/filename.rb20
1 files changed, 14 insertions, 6 deletions
diff --git a/activestorage/app/models/active_storage/filename.rb b/activestorage/app/models/active_storage/filename.rb
index dead6b6d33..8e3cd488a4 100644
--- a/activestorage/app/models/active_storage/filename.rb
+++ b/activestorage/app/models/active_storage/filename.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
-# Encapsulates a string representing a filename to provide convenience access to parts of it and a sanitized version.
-# This is what's returned by ActiveStorage::Blob#filename. A Filename instance is comparable so it can be used for sorting.
+# Encapsulates a string representing a filename to provide convenient access to parts of it sanitization.
+# A Filename instance is returned by ActiveStorage::Blob#filename, and is comparable so it can be used for sorting.
class ActiveStorage::Filename
include Comparable
@@ -9,23 +9,31 @@ class ActiveStorage::Filename
@filename = filename
end
- # Returns the basename of the filename.
+ # Returns the part of the filename preceding any extension.
#
# ActiveStorage::Filename.new("racecar.jpg").base # => "racecar"
+ # ActiveStorage::Filename.new("racecar").base # => "racecar"
+ # ActiveStorage::Filename.new(".gitignore").base # => ".gitignore"
def base
File.basename @filename, extension_with_delimiter
end
- # Returns the extension with delimiter of the filename.
+ # Returns the extension of the filename (i.e. the substring following the last dot, excluding a dot at the
+ # beginning) with the dot that precedes it. If the filename has no extension, an empty string is returned.
#
# ActiveStorage::Filename.new("racecar.jpg").extension_with_delimiter # => ".jpg"
+ # ActiveStorage::Filename.new("racecar").extension_with_delimiter # => ""
+ # ActiveStorage::Filename.new(".gitignore").extension_with_delimiter # => ""
def extension_with_delimiter
File.extname @filename
end
- # Returns the extension without delimiter of the filename.
+ # Returns the extension of the filename (i.e. the substring following the last dot, excluding a dot at
+ # the beginning). If the filename has no extension, an empty string is returned.
#
# ActiveStorage::Filename.new("racecar.jpg").extension_without_delimiter # => "jpg"
+ # ActiveStorage::Filename.new("racecar").extension_without_delimiter # => ""
+ # ActiveStorage::Filename.new(".gitignore").extension_without_delimiter # => ""
def extension_without_delimiter
extension_with_delimiter.from(1).to_s
end
@@ -37,7 +45,7 @@ class ActiveStorage::Filename
# ActiveStorage::Filename.new("foo:bar.jpg").sanitized # => "foo-bar.jpg"
# ActiveStorage::Filename.new("foo/bar.jpg").sanitized # => "foo-bar.jpg"
#
- # ...and any other character unsafe for URLs or storage is converted or stripped.
+ # Characters considered unsafe for storage (e.g. \, $, and the RTL override character) are replaced with a dash.
def sanitized
@filename.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "�").strip.tr("\u{202E}%$|:;/\t\r\n\\", "-")
end