This Week in Rails

March 31, 2023

March 31st 2023 edition 🌸

Hi, it’s zzak, writing in from Aomori, Japan, home of the great Jonan Scheffler.
2023 is officially 25% over, time to enjoy 🌸 and explore this week’s changes in the Rails codebase.

Add debug gem back to the Gemfile template
With the release of Ruby v3.2.2, which happened today, we can safely add the “debug” gem back to the Gemfile template for generated Rails apps.

Infer foreign_key when inverse_of is present
This PR makes has_one and has_many associations automatically infer the foreign_key option when the inverse_of option is present.

Action Cable: assert_broadcasts return the messages that were broadcast
This PR makes Action Cable’s assert_broadcasts return the messages that were broadcast.
This way you can more easily test them, for example:

messages = assert_broadcasts("test", 2) do
  ActionCable.server.broadcast "test", { message: "one" }
  ActionCable.server.broadcast "test", { message: "two" }
assert_equal 2, messages.length
assert_equal({ "message" => "one" }, messages.first)
assert_equal({ "message" => "two" }, messages.last)

This is helpful if you expect lots of messages to be broadcast or if you want to only match on some element of the data; assert_broadcast_on doesn’t work well in either of those scenarios.

Active Storage: Safe for Direct Uploads in js Libraries or Frameworks
This PR allows support for additional headers when using direct uploads from the client side, in order to support various types of authentication such as Bearer tokens, API keys, Basic auth, OAuth 1.0, Digest auth, etc.

Add zettabyte to number_to_human_size
Zettabytes (ZB) has been added to Active Support.

#=> "1 ZB"

Set charset in content type response headers (OWASP)
This PR ensures the charset is included in all Content-Type headers emitted from Rails.
OWASP recommends that every HTTP Response contains a Content-Type header with safe character set.

Active Storage: Remove mini_mime usage in favour of marcel
One less dependency to worry about upgrading!

Fix Rails generated index name being too long
Auto generated index names are now limited to 62 bytes, which fits within the default index name length limits for MySQL, Postgres and SQLite.

Implement marshal_dump and marshal_load on ActiveRecord::Base
A more stable and optimized Marshal serializer for Active Record models has been introduced.
You can opt in via:
config.active_record.marshalling_format_version = 7.1

Add ActiveSupport load hook for ActionText::EncryptedRichText
Previously ActionText::Record and ActionText::RichText have dedicated load hooks.
This PR adds an additional hook for ActionText::EncryptedRichText, so that external libraries have a similarly simple way to run code after the subclass is loaded.

Implement provider_job_id for Backburner jobs
This PR implements provider_job_id for the Beanstalk ActiveJob QueueAdapter.
For example, if you want to be able to cancel jobs:,
  {id: job.provider_job_id}

Load Rake tasks only once for command suggestions
After this PR the "bin/rails --help" command will now list only framework and plugin commands. Rake tasks defined in "lib/tasks/*.rake" files will no longer be included.
For a list of those tasks, use "rake -T".

Include READMEs in main framework pages of the API documentation
This PR teaches RDoc to include the Rails API docs to include each of the relevant libraries READMEs for the top-level constant.
Previously many of these libraries modules had empty descriptions in the API docs, so now they have all of the relevant information which is a nice improvement!

Alias ActionController::Parameters except as without
This PR adds support for without as an alias to except on StrongParameters.

For example, you can now do:

Which is the same as:

Rubocop markdown snippets
This PR adds rubocop-md for linting all markdown files.
Ensuring any code snippets in the guides also follow the Rails coding conventions, without having to manually review every change that comes in.

Add markdownlint to guides
A follow up to the previous PR, now all markdown content in the guides is linted using markdownlint.

Allow destroying active storage variants
When creating active storage variants, ActiveStorage::VariantRecord is inserted, then a file is uploaded.
If the upload fails, the file can be missing even though an ActiveStorage::VariantRecord exists in the database.

In this case, we can now delete the corresponding record of just one variant via this API:

attachable.variant(resize_to_limit: [100, 100]).destroy

Allow Attachables to override default template when attachment is missing
When rendering ActionText attachments, if the underlying attachable has been removed, currently Rails renders the "action_text/attachables/missing_attachable" partial for all attachable types. Consuming applications can override this partial, but it is global across all attachable models.

Now you can render model-specific partials when necessary, for example, you could render a placeholder image for a file attachment or the text “Deleted User” for a User attachment.

You can view the whole list of changes here.
We had 34 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.