Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename ::normalize to ::normalize_value_for #47034

Conversation

jonathanhefner
Copy link
Member

The ::normalize method accepts an attribute name and a value, and type casts the value, applying any declared normalizations for the attribute. Because of the way type casting works for most types, the type of the given value can be very different than the attribute type. This allows for potential confusion between the ::normalize method and the ::normalizes method. For example:

class Post < ActiveRecord::Base
  normalize :title, with: -> { _1.titlecase }
end

In the above code, the user meant to call ::normalizes, but called ::normalize instead. The :with kwarg is treated as a Hash value which is then type cast to a String. The type cast silently succeeds, so the user does not realize why their normalization is not applied.

To prevent such confusion, this commit renames ::normalize to ::normalize_value_for.

The `::normalize` method accepts an attribute name and a value, and type
casts the value, applying any declared normalizations for the attribute.
Because of the way type casting works for most types, the type of the
given value can be very different than the attribute type.  This allows
for potential confusion between the `::normalize` method and the
`::normalizes` method.  For example:

  ```ruby
  class Post < ActiveRecord::Base
    normalize :title, with: -> { _1.titlecase }
  end
  ```

In the above code, the user meant to call `::normalizes`, but called
`::normalize` instead.  The `:with` kwarg is treated as a `Hash` value
which is then type cast to a `String`.  The type cast silently succeeds,
so the user does not realize why their normalization is not applied.

To prevent such confusion, this commit renames `::normalize` to
`::normalize_value_for`.
Copy link
Member

@ghiculescu ghiculescu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍I think this solves the typo problem well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants