1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Rename ActiveStorage::Filename#extname to extension_with_delimiter

This commit is contained in:
George Claghorn 2017-08-20 16:30:15 -04:00
parent 81ca1556b6
commit 8bd14971b9
2 changed files with 31 additions and 11 deletions

View file

@ -9,21 +9,23 @@ class ActiveStorage::Filename
@filename = filename
end
# Filename.new("racecar.jpg").extname # => ".jpg"
def extname
File.extname(@filename)
end
# Filename.new("racecar.jpg").extension # => "jpg"
def extension
extname.from(1)
end
# Filename.new("racecar.jpg").base # => "racecar"
def base
File.basename(@filename, extname)
File.basename @filename, extension_with_delimiter
end
# Filename.new("racecar.jpg").extension_with_delimiter # => ".jpg"
def extension_with_delimiter
File.extname @filename
end
# Filename.new("racecar.jpg").extension_without_delimiter # => "jpg"
def extension_without_delimiter
extension_with_delimiter.from(1).to_s
end
alias_method :extension, :extension_without_delimiter
# Filename.new("foo:bar.jpg").sanitized # => "foo-bar.jpg"
# Filename.new("foo/bar.jpg").sanitized # => "foo-bar.jpg"
#

View file

@ -3,6 +3,24 @@
require "test_helper"
class ActiveStorage::FilenameTest < ActiveSupport::TestCase
test "base" do
assert_equal "racecar", ActiveStorage::Filename.new("racecar.jpg").base
assert_equal "race.car", ActiveStorage::Filename.new("race.car.jpg").base
assert_equal "racecar", ActiveStorage::Filename.new("racecar").base
end
test "extension with delimiter" do
assert_equal ".jpg", ActiveStorage::Filename.new("racecar.jpg").extension_with_delimiter
assert_equal ".jpg", ActiveStorage::Filename.new("race.car.jpg").extension_with_delimiter
assert_equal "", ActiveStorage::Filename.new("racecar").extension_with_delimiter
end
test "extension without delimiter" do
assert_equal "jpg", ActiveStorage::Filename.new("racecar.jpg").extension_without_delimiter
assert_equal "jpg", ActiveStorage::Filename.new("race.car.jpg").extension_without_delimiter
assert_equal "", ActiveStorage::Filename.new("racecar").extension_without_delimiter
end
test "sanitize" do
"%$|:;/\t\r\n\\".each_char do |character|
filename = ActiveStorage::Filename.new("foo#{character}bar.pdf")