Skip to content

Commit 7ec1531

Browse files
authored
Merge pull request rails#54185 from seanpdoyle/active-storage-filename-equality
Delegate `ActiveStorage::Filename#to_str` to `#to_s`
2 parents 9f8d180 + 73133fa commit 7ec1531

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

activestorage/CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
* Delegate `ActiveStorage::Filename#to_str` to `#to_s`
2+
3+
Supports checking String equality:
4+
5+
```ruby
6+
filename = ActiveStorage::Filename.new("file.txt")
7+
filename == "file.txt" # => true
8+
filename in "file.txt" # => true
9+
"file.txt" == filename # => true
10+
```
11+
12+
*Sean Doyle*
13+
114
* Add support for alternative MD5 implementation through `config.active_storage.checksum_implementation`.
215

316
Also automatically degrade to using the slower `Digest::MD5` implementation if `OpenSSL::Digest::MD5`

activestorage/app/models/active_storage/filename.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def sanitized
6464
def to_s
6565
sanitized.to_s
6666
end
67+
alias_method :to_str, :to_s
6768

6869
def as_json(*)
6970
to_s

activestorage/test/models/filename_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ class ActiveStorage::FilenameTest < ActiveSupport::TestCase
5454
assert_operator ActiveStorage::Filename.new("foo-bar.pdf"), :==, ActiveStorage::Filename.new("foo\tbar.pdf")
5555
end
5656

57+
test "String equality" do
58+
assert_operator "foo-bar.pdf", :===, ActiveStorage::Filename.new("foo-bar.pdf")
59+
assert_equal "foo-bar.pdf", ActiveStorage::Filename.new("foo-bar.pdf")
60+
assert_pattern { ActiveStorage::Filename.new("foo-bar.pdf") => "foo-bar.pdf" }
61+
end
62+
5763
test "encoding to json" do
5864
assert_equal '"foo.pdf"', ActiveStorage::Filename.new("foo.pdf").to_json
5965
assert_equal '{"filename":"foo.pdf"}', { filename: ActiveStorage::Filename.new("foo.pdf") }.to_json

0 commit comments

Comments
 (0)