Friday, July 7, 2023

Preprocessed Active Storage variants and more

Posted by wojtek

Hi, Wojtek here. Let’s see what changed in the Rails codebase.

Add an option to preprocess Active Storage variants in background job
Active Storage variants are processed on the fly when they are needed but sometimes we’re sure that they are accessed and want to process them upfront.

class User < ApplicationRecord
  has_one_attached :avatar do |attachable|
    attachable.variant :thumb, resize_to_limit: [100, 100], preprocessed: true
  end
end

Introduce sanitizer_vendor config for Action Text
Use Rails::HTML5::SafeListSanitizer by default in the Rails 7.1 configuration if it is supported. Action Text’s sanitizer can be configured by setting config.action_text.sanitizer_vendor.

Improve performance of ActiveSupport::JSON.encode
This about doubles the performance of .to_json/ActiveSupport::JSON.encode. The benchmarks are included in the pull request.

Allow opting in/out of Link preload headers when calling stylesheet_link_tag or javascript_include_tag

# will exclude header, even if setting is enabled:
javascript_include_tag("http://example.com/all.js", preload_links_header: false)

Disable database prepared statements when query logs are enabled
Prepared statements and query logs are incompatible features due to query logs making every query unique so Active Record will create a new prepared statement for each, potentially exhausting system resources.

Fix has_one through singular building with inverse
Allows building of records from an association with a has_one through a singular association with inverse. For belongs_to through associations, linking the foreign key to the primary key model isn’t needed.

Don’t double-encode nested field_id and field_name index
This fixes the bug with adding an extra index parameter in form builder for deeply nested associations.

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

Until next time!

Subscribe to get these updates mailed to you.