This Week in Rails

May 12, 2023

Rails World CFP, picture_tag helper and more!

Hi, this is Greg, bringing you this week’s changes in the Rails codebase.

Rails World Call for Papers now open
This CFP is open until June 16, with space in the agenda for 26 talks on two tracks, and up to six workshops. Read the full announcement for more details and submit your proposal!

Prevent duplicate filters for encrypted attributes
When an Active Record encrypted attribute is declared, a filter for it is automatically added to config.filter_parameters. Prior to this pull request, the filter would be re-added every time the model was reloaded. This change ensures filters are only added once so that config.filter_parameters does not grow unbounded.

Prevent non-anonymous modules from becoming frozen
In 48106, Module#deep_dup was changed to return the module itself (not a copy) when the module is not anonymous. However, that causes non-anonymous modules to become frozen via value.deep_dup.freeze when passed to ActiveModel::Type::Helpers::Mutable#immutable_value. So, for example, class attributes can no longer be set on the module.
To prevent such issues, this pull request removes the freeze from immutable_value. immutable_value is only called by ActiveRecord::PredicateBuilder#build_bind_attribute, which only cares that other code cannot mutate the value, not that the value is actually frozen.

Allow PredicateBuilder to recognize schema namespaced table names
Before this change, ActiveRecord::PredicateBuilder assumed that a column name will only be specified in dot notation with a single period. If a column is specified in dot notation for a table that is namespaced in a schema, it used the schema name as the table name. This pull request allows columns to specified in the format schema.table.column as well as table.column.

Support :message_pack as cookies serializer
This commit adds support for :message_pack and :message_pack_allow_marshal as serializers for config.action_dispatch.cookies_serializer, just like config.active_support.message_serializer.

Support :message_pack as message serializer
This change adds support for :message_pack as a serializer for MessageEncryptor and MessageVerifier, and, consequently, as an option for config.active_support.message_serializer.

Default message serializer to :json_allow_marshal
This pull request changes the default message serializer set by config.load_defaults 7.1 from :json to :json_allow_marshal so that upgraded apps can continue to read old messages without additional configuration.
The intention is to eventually change the default to :json (with no Marshal fallback) in Rails 7.2, after some time has passed with apps generating JSON-serialized messages.
Apps can opt in to JSON-only serialization before Rails 7.2 by manually setting config.active_support.message_serializer = :json.

Handle empty list of cache keys
With this change, read_multi, write_multi and fetch_multi will bail out early if somehow called with an empty list.
The same change was done for delete_multi in a follow-up pull request.

Make increment_counter/decrement_counter accept an amount argument
This pull request introduces the by option to increment_counter and decrement_counter to make it easier to change counters by an arbitrary value. Example usage:
Company.increment_counter(:users_count, 5, by: 3)

Make Active Record’s query cache an LRU
Active Record query cache is now evicts least recently used entries, with 100 entries as a default. The cache size can be configured via database.yml

development:
  adapter: mysql2
  query_cache: 100


It can also be entirely disabled:

development:
  adapter: mysql2
  query_cache: false


Add a picture_tag helper
This pull request adds support for the HTML picture tag. It supports passing a String, an Array or a Block and passing properties directly to the img tag via the :image key.
Since the picture tag requires an img tag, the last element you provide will be used for the img tag.
For complete control over the picture tag, a block can be passed, which will populate the contents of the tag accordingly. See the pull request for usage examples.

Add matcher support to assert_enqueued_email_with
Currently, assert_enqueued_with allows you to pass procs for matching args. However, assert_enqueued_email_with doesn’t let you do that for args and params.
This pull request made assert_enqueued_email_with accept procs for args and params:
assert_enqueued_email_with DeliveryJob, params: -> p { p[:token] =~ /\w+/ } do
  UserMailer.with(token: user.generate_token).email_verification.deliver_later
end

You can view the whole list of changes here.
We had 28 contributors to the Rails codebase this past week!
Until next time!

About This Week in Rails

Your weekly inside scoop of interesting commits, pull requests and more from Rails.