Friday, August 5, 2022

Strict template locals, detecting unused routes and an improved find_or_create_by

Posted by p8

Hi, Petrik here with some updates on activities in Rails and associated projects over the last week.

Allow templates to set strict locals

By default, templates will accept any locals as keyword arguments. To define what locals a template accepts, add a locals magic comment:

<%# locals: (message:) -%>
<%= message %>

Try to find a second time if find_or_create_by hits a uniqueness constraint For the case where most of the time the record is expected to exist, find_or_create_by can be made race-condition free by re-trying the find if the create failed with ActiveRecord::RecordNotUnique. This assumes that the table has the proper uniqueness constraints, if not, find_or_create_by will still lead to duplicated records.

Add support for if_exists option when removing a check constraint 

The remove_check_constraint method now accepts an if_exists option. If set to true an error won’t be raised if the check constraint doesn’t exist.

Add linter for CHANGELOG formatting A PR to Rails should include a CHANGELOG entry if you’re adding or removing a feature, committing a bug fix, or adding deprecation notices. To prevent inconsistencies in the CHANGELOG a linter has been added.

Add drop_enum migration command for PostgreSQL This does the inverse of create_enum. Before dropping an enum, ensure you have dropped columns that depend on it. Update the existing Kindle version of the guide from .mobi to .epub As Kindle is discontinuing support for the .mobi format, this makes the guides available as .epub.

Add –unused option to the routes command to detect extraneous routes

Over time, a Rails app can become slow to boot simply because of how many routes it has. This new option for the routes command can be used to detect routes that are drawn, but aren’t actually valid.

24 people contributed to Rails since the last time.

Lots of activity in the Hotwired world this week as well.

Release Turbo v7.2.0-beta.2 This builds on the v7.2.0-beta.1 release that saw substantial new features and bugs fixes.

Introduce_ turbo:frame-missing_ event

When a response from within a frame is missing a matching frame, the turbo:frame-missing event is fired. For example, if the reason the frame is missing is access (an expired session, for example), the call to visit() can be made with { action: “replace” } to remove the current page from Turbo’s page history.

Activate <script> in Turbo Streams

When a <turbo-stream> element connects to the document, all included <script> elements will be activated.

Preserve <select> values in cache

This change will save the values of <select> tags when creating a snapshot so that when you navigate back or forward in history, form fields will not be reset.

That’s it for this week. Until next time!