Skip to content

Commit 830957e

Browse files
committed
Document where support for AR normalizes
Also some other small changelog tweak: * Add normalize_value_for to the activerecord changelog * Fix a wrong method reference in the 7.1 release notes
1 parent e4350f6 commit 830957e

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,7 @@
856856
```ruby
857857
class User < ActiveRecord::Base
858858
normalizes :email, with: -> email { email.strip.downcase }
859+
normalizes :phone, with: -> phone { phone.delete("^0-9").delete_prefix("1") }
859860
end
860861
861862
user = User.create(email: " [email protected]\n")
@@ -865,8 +866,12 @@
865866
user.email # => "[email protected]"
866867
user.email_before_type_cast # => "[email protected]"
867868
869+
User.where(email: "\t[email protected] ").count # => 1
870+
868871
User.exists?(email: "\t[email protected] ") # => true
869872
User.exists?(["email = ?", "\t[email protected] "]) # => false
873+
874+
User.normalize_value_for(:phone, "+1 (555) 867-5309") # => "5558675309"
870875
```
871876

872877
*Jonathan Hefner*

activerecord/lib/active_record/normalization.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ module ClassMethods
6969
# user.email # => "[email protected]"
7070
# user.email_before_type_cast # => "[email protected]"
7171
#
72+
# User.where(email: "\[email protected] ").count # => 1
73+
#
7274
# User.exists?(email: "\[email protected] ") # => true
7375
# User.exists?(["email = ?", "\[email protected] "]) # => false
7476
#

activerecord/test/cases/normalized_attribute_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ class NormalizedAircraft < Aircraft
8383
assert_equal @aircraft, NormalizedAircraft.find_by(manufactured_at: @time.to_s)
8484
end
8585

86+
test "searches a record by normalized value" do
87+
from_database = NormalizedAircraft.where(name: "fly HIGH")
88+
assert_equal [@aircraft], from_database
89+
end
90+
91+
test "searches a record by normalized values" do
92+
from_database = NormalizedAircraft.where(name: ["fly LOW", "fly HIGH"])
93+
assert_equal [@aircraft], from_database
94+
end
95+
8696
test "can stack normalizations" do
8797
titlecase_then_reverse = Class.new(NormalizedAircraft) do
8898
normalizes :name, with: -> name { name.reverse }

guides/source/7_1_release_notes.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ user = User.find_by(email: "\[email protected] ")
7878
user.email # => "[email protected]"
7979
user.email_before_type_cast # => "[email protected]"
8080

81+
User.where(email: "\t[email protected] ").count # => 1
82+
8183
User.exists?(email: "\t[email protected] ") # => true
8284
User.exists?(["email = ?", "\t[email protected] "]) # => false
8385

84-
User.normalize(:phone, "+1 (555) 867-5309") # => "5558675309"
86+
User.normalize_value_for(:phone, "+1 (555) 867-5309") # => "5558675309"
8587
```
8688

8789
### Add `ActiveRecord::Base.generates_token_for`

0 commit comments

Comments
 (0)