Recent open source contributions in the Turbo ecosystem

I’ve been pushing Turbo hard the past couple of months. That resulted in running into some edge case scenarios, which have not yet been resolved in the library.

One is an annoying race condition, related to incoming turbo refreshes stealing the navigation to another page. At the time of writing still waiting for merge.

The other one, just merged to main, is an improvement to turbo-rails documentation, which came out of the above work too.

Configuring SSHKit in Automated Tests

When using Ruby’s SSHKit gem in your application, you might want to test code that uses it. Most likely, you don’t want to connect to real servers when automated tests are run. Luckily SSHKit includes a backend just for that, called Printer.

You can easily configure your test suite to use it:

SSHKit.configure do |kit|
  kit.backend = SSHKit::Backend::Printer
end

In a Rails application for example, simply add this to your test_helper.rb. Now all the execute statements will be printer to the standard output.

You can also implement your own SSHKit backend, based on the Printer class, if you need something more involved, like saving all the commands into a Thread.current variable and running assertions on them within your test.

MacOS Ventura firewall repeatedly asks to accept incoming connections when reinstalling a Ruby version with rbenv

On my Intel iMac on macOS Ventura 13.4 I ran into this annoying issue when reinstalling the same version of Ruby (3.2.2 for example) with rbenv. When running rails test:system the firewall’s “Accept incoming connections” dialog would pop every time, no matter whether you Denied or Allowed the connection to go through previously, making testing quite painful.

I couldn’t find a working solution through googling, but ChatGPT was able to help. It’s as simple as:

sudo codesign --force --sign - $HOME/.rbenv/versions/3.2.2/bin/ruby

Simply find the ruby bin version you’re having problems with inside your .rbenv folder and instruct macOS to re-sign it. The expected output should be something like:

/Users/klevo/.rbenv/versions/3.2.2/bin/ruby: replacing existing signature

After that, you’ll need to confirm or deny the “Accept incoming connections” Firewall dialog when running something like system test suite, but afterwards the Firewall should remember your choice.

Lastly, this only seems to affect Intel Macs. At least for me, no such issue occurred when reinstalling ruby with rbenv on an ARM MacBook.

Thanks AI 🙂

navigator.clipboard does not work under plain HTTP in latest Safari (on .test domains)

I just noticed that on the latest MacOS Ventura 13.3.1 with Safari 16.4, the navigator.clipboard API is not accessible under plain HTTP .test domains, used with puma-dev for example. It returns “property is undefined” type of error.

The solution is to switch to HTTPS. With puma-dev this is simple, as it comes out of box with support for secure connections.

I haven’t had a chance to test it with plain old localhost yet, but I figured to post this, in case it trips you too.

Solution: Some Mac OS Mail Favourites are grayed out and cannot be deleted

I encountered the issue of a grayed out folder that can’t be deleted, in MacOS Mail app today. The discussion on Apple’s forums does not provide any actionable answers. After a while I figured out a solution myself and it’s very simple:

  1. Create a new mailbox (Mailbox -> New Mailbox…). For Location, choose “On My Mac” and name it the same as the grayed out folder in Favourites that you’re struggling with.
  2. Once you add this new mailbox, the one in Favourites should become accessible again. You can now right click on it and hit “Remove from Favourites”.
  3. You can now also delete the empty local (On My Mac) mailbox folder that you created in step 1.

Hope that helps.

How I run puma-dev alongside Rails’ bin/dev

The bin/dev script Ruby on Rails ships with, if you for example init the app with --css=tailwind or --css=bootstrap is great. It comes with auto-generated Procfile that will launch the web server and the Tailwind CSS (or other) preprocessors. In addition bin/dev will install the foreman gem if it does not exist on your system.

At some point however, you’ll want to run multiple Rails applications on your developer machine. Doing that with localhost:3000 will get annoying at some point (cookie sharing, changing ports, everyone in the team handling it differently, no HTTPS to test with…).

puma-dev has been around for a long time to solve this. However while it takes care of running the web server part for you (in the background), you still want to see the logs, run your CSS (or JS) preprocessor or compiler, job workers, etc. I figured out it’s simply about adjusting the Procfile and bin/dev slightly and one can use the familiar approach in conjunction with all the puma-dev features.

This is what I do:

My Procfile.dev looks like this:

web: tail -f log/development.log
css: bin/rails tailwindcss:watch

The change is in the web: stanza. Instead of launching puma server, we simply tail the logs, as puma will be run by puma-dev in the background for you, as soon as you visit your local .test domain.

My bin/dev looks like this:

#!/usr/bin/env bash

if ! command -v foreman &> /dev/null
then
  echo "Installing foreman..."
  gem install foreman
fi

if ! command -v puma-dev &> /dev/null
then
  echo "Installing puma-dev..."
  brew install puma/puma/puma-dev
  echo ""
  echo "To finish puma-dev setup run:"
  echo "sudo puma-dev -setup"
  echo "puma-dev -install"
  echo "puma-dev link -n myapp ."
  echo ""
fi

foreman start -f Procfile.dev

I’ve added a section that installs puma-dev (if your team is using Homebrew) and prints out instructions on how to complete the setup.

For your use case, simple replace “myapp” above with the name of your app.

This feels like sticking to the Rails way and is easily extensible if you also want to launch other services in development, like job workers, by simply adding a stanza in the Procfile.dev. Neat.

Scaffolding in Rails 7 is amazing

I’m continuously being impressed by the productivity and ease of use enhancements Rails keeps making after all these years it has been around.

Today I discovered that scaffolds generated in brand new --css=tailwind enabled Rails 7 codebase, is generating basic, beautiful Tailwind markup out of the box.

rails g scaffold_controller User username:string

Produces:

Index of a freshly generated model.
Edit view. Notice the Tailwind markup.

Further I have this in my config/application.rb to prevent generating files that I don’t use for every single resource until they are needed:

config.generators do |g|
  g.assets false
  g.helper false
  g.jbuilder false
end

That’s a damn good job Rails community. ❤️

Easily switch or experiment with different databases in Rails

I just learned about rake db:system:change task that Rails provides, to speed up switching between different database engines. If you’re experimenting with something, or benchmarking things, this is super useful.

I had a simple PostgreSQL app and running rake db:system:change --to=sqlite3 and afterwards rake db:setup got me going with my task in seconds.

Small detail I noticed, rake -T which should list all available tasks, does not show anything about db:system for some reason.

Starting new Rails applications

It’s been a while since I reviewed the documentation for rails new console command. There are a few useful features that I didn’t know about:

Most of the times, for database engine, PostgreSQL is my choice. This is easily accomplished with the -d switch, but even more convenient is adding this as a preset to your environment, using the .railsrc dotfile. Configuring such on your system will allow you to just stick to rails new appname without having to add switches.

The second useful thing I didn’t know exists is the Rails Application Templates which makes it faster to start new Rails applications with the same set of gems and configuration that you might be reusing across your apps.