Rails 8: The db:migrate task might not run all migrations in db/migrate/ anymore

In Rails 8 the behavior of the rails db:migrate command has changed for fresh databases (see PR #52830).

  • Before Rails 8: The command runs all migrations in the folder db/migrate/*
  • After Rails 8: The command loads the schema file (db/schema.rb or db/schema.sql) if existing and runs all pending migrations in the folder db/migrate/* afterwards

This speeds up the command. But e.g. migrations with data manipulations are missing.

The only way to run all pending migrations seems to...

How to reliably center (block) icons vertically with text

vertical-align is hard. Have you ever wanted to vertically center an icon with text? This usually means "vertically align with capital letters", as visually, a text line goes from baseline up to the capital top. (That's because descenders are far less frequent than ascenders.)

In this card we'll vertically center an icon (or any "blockish" inline element, really) with the capital letters of surrounding text. This works well with our [modern approach to SVG icons](/mak...

Ruby tempfiles

Tempfiles get deleted automatically

With the the ruby Tempfile class you can create temporary files. Those files only stick around as long as you have a reference to those. If no more variable points to them, the GC may finalize the object at some point and the file will be removed from the filesystem. If you would try to access your tempfile then using its path (which you stored previously), you would get an error because the file no longer exists.

Unlink your tempfiles when you're done with them
-...

How to use pessimistic row locks with ActiveRecord

When requests arrive at the application servers simultaneously, weird things can happen. Sometimes, this can also happen if a user double-clicks on a button, for example.

This often leads to problems, as two object instances are modified in parallel maybe by different code and one of the requests writes the results to the database.

In case you want to make sure that only one of the requests "wins", i.e. one of the requests is fully executed and completed while the other one at least has to wait for the first request to be completed, you ha...

Unpoly 3.12.0 released

This release adds asynchronous compilers and many other features requested by the community.
We also fixed a number of performance regressions introduced by Unpoly 3.11.

Breaking changes are marked with a ⚠️ emoji and polyfilled by unpoly-migrate.js.

Asynchronous compilers

Compiler functions can now be [async](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/...

Testing HTTPS with badssl.com

Website that offers lots of different kinds of HTTPS configurations, bad or good or complicated.

They also offer a dashboard to check if your browser's HTTPS handling works as expected (which might be compromised e.g. due to security products or enterprise proxy servers).

ActiveType 1.2 supports "change_association"

With ActiveType 1.2 you can modify associations (has_many etc.) after they have been defined.

One common use case for this is to change an association inside a form model, like this:

class Credential < ActiveRecord::Base
end

class User < ActiveRecord::Base
  has_many :credentials
end

class SignUpCredential < ActiveType::Record[Credential]
end

class SignUp < ActiveType::Record[User]
  change_association :credentials, class_name: 'SignUpCredential'
end

Now, if you load `credentials...

Speed up your sass compile with sass-embedded

Note

Compiling Sass is probably the slowest part of your build, so this is worth a try if you're using Sass.

If you're using the sass npm package to compile your SASS/SCSS, consider switching to sass-embedded. It should be a drop-in replacement in a large app and it's around 50% faster than using the deprecated sass package.

Geordi 12.6.0 was released

Changelog

  • geordi dump: Allow to forward the compression option to the underlying dumple command, e.g. geordi dump --compress=zstd:3 (for PostgreSQL) or geordi dump --compress (for MySQL).
  • dumple: Allow to specify a compression algorithm for PostgreSQL, e.g. dumple --compress=zstd:3. The already supported compression for MySQL dumple --compress is kept untouched.

In PostgreSQL you will notice a speedup of ~50%.

Example

Performance improvements during a deploy (e.g. lib/capistrano/tasks/db.rake)

namespace...

How to change the time for a Docker container from the outside

I had to modify the time for an application that I launch through Docker.
Here is an approach that worked for me without modifying or wrapping the container image.

Note

This is quite hacky and may not work for everyone. The "proper" way is probably to have a custom Dockerfile to wrap an existing image, or adjust a custom image's Dockerfile. I wanted to do neither.

So, I had an application that I ran through a pre-built image. Basically this:

docker run --rm -it example/image

The [faketime](https://manpages.ubuntu.c...

GoodJob: Ensure you get notified about background exceptions

GoodJob and ActiveJob rescue exceptions internally, preventing exception_notification from triggering. This can cause silent job failures.To get notified, subscribe to ActiveJob events and configure GoodJob's on_thread_error hook. This lets you manually call your exception notifier for every retry, discard, or internal GoodJob error.

# config/initializers/good_job.rb

# Manually notify on job failures, as they are handled internally by ActiveJob/GoodJob.
ActiveSupport::Noti...

Format your JavaScript with prettier

prettier calls itself an opinionated code formatter. I recommend using it for your JavaScript and TypeScript code.

prettier only concerns itself with the formatting of your JavaScript (and also some other file types like json or yaml). When you use it, it has an opinion on every single whitespace and linebreak, as well as a few other things. You renamed a variable and now your line is a bit longer than looks good? prettier will reformat your code.

This might not work for you if you have strong opinions yourself....

Ruby: Avoiding errors when casting user input to Integers

There's a method Integer() defined on Kernel, that typecasts everything into an Integer.

Integer("2")  # 2
Integer("foo")  # Invalid value for Integer() (ArgumentError)
Integer(nil) # Can't convert nil to Integer (TypeError)
Integer([]) # Can't convert Array into Integer (TypeError)
Integer(Object.new) # Can't convert Object into Integer (TypeError)
Integer(2) # 2
Integer("11", 2) # 3

This is very similar but not identical to to_i:

"2".to_i # 2
"foo".to_i #...

Automatic Log Rotation in Rails

Rails log files rotate automatically when they reach approx. 100MB:

$ ls -lh log/
-rw-r--r-- 1 user group  55M Sep 15 09:54 development.log
-rw-r--r-- 1 user group 101M Aug 22 13:45 development.log.0

This behavior is a built-in feature of Ruby's standard Logger class, which Rails uses by default.

To control the maximum file size, set config.log_file_size in yo...

Checklist: Rails Authentication

Authentication is a special part of web applications. On the one hand, it usually is a crucial security mechanism restrict access to certain people and roles. On the other hand, most users authenticate only once, so it is very unlikely to spot issues by accident.

So, here comes a quick checklist to help you verifying your authentication solution is all set.

  • This should be default: use HTTPS with HSTS. The HSTS part is important.
  • Use a reliable authentication solution, e.g. [Compose Rails authentication primitives](https://makandracards...

Documenting your project's Node.js version in .nvmrc

Not all versions of Node.js are compatible with each other. Also npm packages may require a minimum or maximum version of Node.js. We use nvm on our development PCs so we can operate multiple versions of Node.js in parallel.

To make sure that all developers use a compatible version of Node.js, your project should declare the required Node.js in a file called .nvmrc.

When a .nvmrc exists, developers can cd in your project directory and activate the p...

How to organize large I18n dictionaries in Ruby on Rails

If you're suffering from a huge de.yml or similiar file, cry no more. Rails lets you freely organize your dictionary files in config/locales.

My organization works like this:

  • config/locales/rails.de.yml modified Rails boilerplate
  • config/locales/faker.de.yml modified Faker boilerplate
  • config/locales/models.de.yml model names, attribute names, assignable_value labels
  • `config/locales/views.de.y...

Bookmarklet to generate a Pivotal Tracker story from Zammad Ticket

This is a bookmarklet you can add to Chrome or Firefox which will allow you to create a story in Pivotal Tracker from a Zammad ticket. This might come in handy when creating stories for SWAT Teams.

But first you will have to set two variables in the script below:

  • pt_project_id: the ID of the Pivotal Tracker Project you want to add stories to. This can be found as part of the URL of the project (https://www.pivotaltracker.com/n/projects/<pt_project_id>)
  • pt_token: the Pivotal Tracker token used for authentication. Can be found in y...

Timecop: reset after each test

Timecop is a great gem to set the current time in tests. However, it is easy to introduce flakyness to your test suite when you forget to reset the time after the test.
This might be the case if:

  • a test freezes time and a later test does not work for frozen time
  • a later test needs the real current date to work correctly

Often you only notice these kinds of errors in rare cases when tests are executed in a particular order.

A way to avoid this is by using block notation (`Timecop.travel(...) ...

Reliably sending a request when the user leaves the page

navigator.sendBeacon is a way to reliably send a POST request, even on unload.

Please note, however, that there are generally two ways to detect a "user leaving the page":

  1. The unload event, which fires after a page is actually gone (e.g. after tab close, page refresh, and navigation away).
  2. The visibilitychange event. It is much softer, and will fire after tab contents have been hidden by any means (e.g. when closing a tab, but also when switchin...

Stabilize integrations tests with flakyness introduced by Turbo / Stimulus / Hotwire

If you run a Rails app that is using Turbo, you might observe that your integration tests are unstable depending on the load of your machine. We have a card "Fixing flaky E2E tests" that explains various reasons for that in detail.

Turbo currently ships with three modules:

  • Turbo Drive accelerates links and form submissions by negating the need for full page reloads.
  • Turbo Frames decompose pages into independent contexts, which scope navigation and can be lazily loaded.
  • T...

What does 100% mean in CSS?

The attached article examines what the percent unit (%) is relative to in CSS

The article does a great job of visualizing the dependencies. The TLDR is:

Own property % of
height parent height
width parent width
top parent height
left parent width
margin-top parent width
margin-left parent width
padding-top parent width
padding-left parent width

How to avoid multiple versions of a package in yarn

To avoid multiple versions of a package, you can manually maintain a resolutions section in your package.json. We recommend you to do this for packages like jQuery. Otherwise the jQuery library attached to window might not include the functions of your packages that depend on jQuery.

Note: This is only an issue in case you want to use a package functionality from window e.g. $(...).datepicker() from your dev console or any other javascript within the application.

Background

By default yarn will create a folder node_modules ...

Processing GitLab Merge Requests within RubyMine

GitLab has a RubyMine plugin that enables you to review and process merge requests within RubyMine!

Setup

  1. Open RubyMine settings (Ctrl + Alt + S) > Plugins > Search for "GitLab" > Install
    • (You might need to re-open settings afterwards.)
  2. In the RubyMine settings > Version Control > GitLab > Connect your GitLab account with "+"

Working with merge requests

  1. From the Actions menu (Ctrl + Shift + A), choose "View merge...