Skip to content

Commit 13d75c4

Browse files
Merge pull request rails#44485 from byroot/fix-pathname-blank
`Pathname.blank?` only returns true for `Pathname.new("")`
2 parents c01a744 + 0ac4c04 commit 13d75c4

File tree

5 files changed

+40
-0
lines changed

5 files changed

+40
-0
lines changed

activesupport/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
* `Pathname.blank?` only returns true for `Pathname.new("")`
2+
3+
Previously it would end up calling `Pathname#empty?` which returned true
4+
if the path existed and was an empty directory or file.
5+
6+
That behavior was unlikely to be expected.
7+
8+
*Jean Boussier*
9+
110
* Deprecate `Notification::Event`'s `#children` and `#parent_of?`
211

312
* Change default serialization format of `MessageEncryptor` from `Marshal` to `JSON` for Rails 7.1.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# frozen_string_literal: true
22

3+
require "active_support/core_ext/pathname/blank"
34
require "active_support/core_ext/pathname/existence"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
require "pathname"
4+
5+
class Pathname
6+
# An Pathname is blank if it's empty:
7+
#
8+
# Pathname.new("").blank? # => true
9+
# Pathname.new(" ").blank? # => false
10+
# Pathname.new("test).blank? # => false
11+
#
12+
# @return [true, false]
13+
def blank?
14+
to_s.empty?
15+
end
16+
end

activesupport/lib/active_support/core_ext/pathname/existence.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "pathname"
4+
35
class Pathname
46
# Returns the receiver if the named file exists otherwise returns +nil+.
57
# <tt>pathname.existence</tt> is equivalent to
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../../abstract_unit"
4+
require "active_support/core_ext/pathname/blank"
5+
6+
class PathnameBlankTest < ActiveSupport::TestCase
7+
def test_blank
8+
assert_predicate Pathname.new(""), :blank?
9+
assert_not_predicate Pathname.new("test"), :blank?
10+
assert_not_predicate Pathname.new(" "), :blank?
11+
end
12+
end

0 commit comments

Comments
 (0)