Skip to content

Commit

Permalink
Add negative where examples for normalizes
Browse files Browse the repository at this point in the history
In #49105, `where` examples were added to the `normalizes` documentation
to demonstrate that normalizations are also applied for `where`.
However, as with the `exists?` examples, we should also demonstrate that
normalizations are only applied to keyword arguments, not positional
arguments.  We can also address the original source of the confusion by
changing the wording of "finder methods" to "query methods".

This commit also removes the tests added in #49105.  `normalizes` works
at the level of attribute types, so there is no need to test every query
method.  Testing `find_by` is sufficient.  (And, in point of fact,
`find_by` is implemented in terms of `where`.)
  • Loading branch information
jonathanhefner committed Sep 10, 2023
1 parent b1ac22f commit 843283b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 16 deletions.
5 changes: 3 additions & 2 deletions activerecord/CHANGELOG.md
Expand Up @@ -944,7 +944,7 @@

A normalization is applied when the attribute is assigned or updated, and
the normalized value will be persisted to the database. The normalization
is also applied to the corresponding keyword argument of finder methods.
is also applied to the corresponding keyword argument of query methods.
This allows a record to be created and later queried using unnormalized
values. For example:

Expand All @@ -961,7 +961,8 @@
user.email # => "cruise-control@example.com"
user.email_before_type_cast # => "cruise-control@example.com"

User.where(email: "\tCRUISE-CONTROL@EXAMPLE.COM ").count # => 1
User.where(email: "\tCRUISE-CONTROL@EXAMPLE.COM ").count # => 1
User.where(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]).count # => 0

User.exists?(email: "\tCRUISE-CONTROL@EXAMPLE.COM ") # => true
User.exists?(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]) # => false
Expand Down
5 changes: 3 additions & 2 deletions activerecord/lib/active_record/normalization.rb
Expand Up @@ -32,7 +32,7 @@ module ClassMethods
# Declares a normalization for one or more attributes. The normalization
# is applied when the attribute is assigned or updated, and the normalized
# value will be persisted to the database. The normalization is also
# applied to the corresponding keyword argument of finder methods. This
# applied to the corresponding keyword argument of query methods. This
# allows a record to be created and later queried using unnormalized
# values.
#
Expand Down Expand Up @@ -69,7 +69,8 @@ module ClassMethods
# user.email # => "cruise-control@example.com"
# user.email_before_type_cast # => "cruise-control@example.com"
#
# User.where(email: "\tCRUISE-CONTROL@EXAMPLE.COM ").count # => 1
# User.where(email: "\tCRUISE-CONTROL@EXAMPLE.COM ").count # => 1
# User.where(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]).count # => 0
#
# User.exists?(email: "\tCRUISE-CONTROL@EXAMPLE.COM ") # => true
# User.exists?(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]) # => false
Expand Down
10 changes: 0 additions & 10 deletions activerecord/test/cases/normalized_attribute_test.rb
Expand Up @@ -83,16 +83,6 @@ class NormalizedAircraft < Aircraft
assert_equal @aircraft, NormalizedAircraft.find_by(manufactured_at: @time.to_s)
end

test "searches a record by normalized value" do
from_database = NormalizedAircraft.where(name: "fly HIGH")
assert_equal [@aircraft], from_database
end

test "searches a record by normalized values" do
from_database = NormalizedAircraft.where(name: ["fly LOW", "fly HIGH"])
assert_equal [@aircraft], from_database
end

test "can stack normalizations" do
titlecase_then_reverse = Class.new(NormalizedAircraft) do
normalizes :name, with: -> name { name.reverse }
Expand Down
5 changes: 3 additions & 2 deletions guides/source/7_1_release_notes.md
Expand Up @@ -60,7 +60,7 @@ getting your Rails application up and running in a production environment.

Normalizations can be declared for attribute values. The normalization
takes place when the attribute is assigned or updated, and will be persisted to the database.
Normalization is also applied to corresponding keyword arguments in finder methods,
Normalization is also applied to corresponding keyword arguments in query methods,
allowing records to be queried using unnormalized values.

For example:
Expand All @@ -78,7 +78,8 @@ user = User.find_by(email: "\tCRUISE-CONTROL@EXAMPLE.COM ")
user.email # => "cruise-control@example.com"
user.email_before_type_cast # => "cruise-control@example.com"

User.where(email: "\tCRUISE-CONTROL@EXAMPLE.COM ").count # => 1
User.where(email: "\tCRUISE-CONTROL@EXAMPLE.COM ").count # => 1
User.where(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]).count # => 0

User.exists?(email: "\tCRUISE-CONTROL@EXAMPLE.COM ") # => true
User.exists?(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]) # => false
Expand Down

0 comments on commit 843283b

Please sign in to comment.