⚡️ Hotwire dev newsletter - December 2022 edition

Turbo (Native), Stimulus, and Strada articles, code, courses, and more. Plus exclusive insights into how I build Turbo Native apps. Delivered monthly.

Welcome back to the December edition of the ⚡️ Hotwire dev newsletter!

Highlights from this month include new releases of Ruby, Stimulus, and Nate Hopkins’s “TurboBoost” libraries; the first video from the Rails SaaS Conference; and a tutorial on rolling your own authentication. There’s also a sneak peek of a new feature being released in Rails 7.1 that I am very excited about.

I’m also excited to share that I hosted the first ever Turbo Native workshop last week! Attendees learned how to use the iOS wrapper for navigation, authentication, the JavaScript bridge, and integration with SwiftUI.

I’m planning on tweaking the content and pacing a bit to make it easier to follow for those new to Swift and Xcode. If you’d like to attend future sessions, you can sign up to get notified when the next session goes on sale.

I hope you can enjoy some rest and recovery as we go into the new year. See you in 2023!

📰 News

Ruby 3.2.0 released

As is tradition, the latest version of Ruby was released on Christmas day again this year. The update adds tons of new features and performance improvements. I’m most excited about:

  • Production-ready YJIT - 41% faster than the existing Ruby interpretor
  • Data - Leaner and stricter Struct-like API for immutable value objects
  • Struct - Defaults to keyword initialization without keyword_init: true
  • SyntaxSuggest - Helps find missing ends in your code
  • ErrorHighlight - TypeError and ArgumentError now point to the relevant argument(s)

Stimulus 3.2.0 released

This release adds a way to call Stimulus controller methods from key presses. This removes a lot of boilerplate by baking it into the library.

From the documentation, here’s how you can use the new KeyboardEvent Filter to close a modal when the escape key is pressed.

<div data-controller="modal"
      data-action="keydown.esc->modal#close"
      tabindex="0">
</div>

This release also includes official support for the Outlets API I mentioned last month.

New versions of TurboReady, TurboReflex, and Reflex Behaviors

by Nate Hopkins @hopsoft

TurboReady and TurboReflex have been consolidated under the “TurboBoost” namespace and renamed to TurboBoost Streams and TurboBoost Commands, respectively. These libraries build on top of Turbo to provide more control over the DOM with custom actions. Reflex Behaviors adds pre-built reactive behaviors to Hotwire apps.

Check out Nate’s video to see how magical they are.

Normalize records in Rails 7.1

by Jonathan Hefner @jonathanhefner

Coming in Rails 7.1 is a feature I’ve been wanting for a while – Active Record normalizations. This new API applies custom changes to attributes on a record before it is saved or updated.

For example, stripping and downcasing an email on a User model.

class User < ActiveRecord::Base
  normalizes :email, with: -> email { email.strip.downcase }
end

The Rails and Hotwire Codex book

by Ayush Newatia @ayushn21

Learn how to build a marketplace app and deploy it to web, iOS, and Android with Rails and Hotwire. This 924-page book covers authentication, authorization, hybrid mobile apps, internationalization and localization, PostgreSQL powered search, and every single Rails sub-framework (Action Mailer, Active Storage, Action Cable, etc.).

📝 Articles

Rails’ safety mechanisms

by Jason Charnes @jmcharnes

Jason outlines 5 common Rails pitfalls and provides concrete examples on how to fix or avoid them. They range from mass assignment to N+1 queries to keeping your credentials secure.

My favorite tip is applying strict loading in development. This configuration will raise an error if you hit an N+1 on Rails 6.1+.

config.active_record.strict_loading_by_default = true

👩‍🎓 Tutorials

Directing Turbo Native apps from the server

by Ayush Newatia @ayushn21

Turbo Native apps require special care when navigating between screens. Redirecting to a new page, like on the web, doesn’t always make sense in an iOS or Android app.

Ayush dives into some navigation routes built into the turbo-rails gem to keep more custom logic on the server. With these routes (and their helpers) we gain a bit more control in directing how our native apps should navigate.

Reddit-like Nested Comments

by Alessandra Tirnavan

A step-by-step and, dare I say, fun guide on building a Rails + Hotwire app with nested comments. Alessandra takes us from rails new and incrementally adds commenting, editing, and nesting. Then brings it all together with dynamic updates via Turbo Streams and Frames. And not a single line of custom JavaScript is needed.

Turbo Stream responses that spark joy

by Spinal CMS @SpinalCms

This article covers how to animate the appearance of views from View Component or custom partials. Examples include fading in a new icon, animating content rows on appear, and animating table cells individually.

The magic? Leveraging the class_names() helper to add Animate.css classes to the new elements. Because the classes are applied to the new elements on the screen, the content animates when they are rendered from the server.

A better way to handle user authentication in Rails

by Simon Chiu @geetfun

Forget Devise. Simon pushes for owning all the code in your authentication stack.

He uses Authentication Zero to generate a bunch of scaffold code directly into your codebase. This is different than installing a gem because you manage the code yourself. The logic isn’t hidden away somewhere, so you can customize it as much, or as little, as you want.

How to validate the presence of a boolean field in a Rails model

by Stefanni Brasil @stefannibrasil

Let’s say you are validating the presence of a boolean and reach for the following validation.

validates :required, presence: true

Turns out this will reject a value that is false. Not what you want! Read on for how to correctly validate the presence of a boolean with Active Record validations.

🎥 Videos

Getting to One Million Users as a One-Woman Dev

by Nadia Odunayo @nodunayo

The first Rails SaaS Conference talk was published! And by golly it’s a good one.

Nadia takes us through her amazing journey building and bootstrapping The StoryGraph. We get a peek into the early beta buzz on Twitter, bringing on a co-founder, and going viral with her mobile apps powered by Turbo Native.

Realtime Analytics With Ahoy And Chartkick

by Dean DeHart @Deaniocom

This video takes us from rails new to a live-updating analytics chart. It leverages Ahoy to persist analytics data from visitors and Chartkick to render the views.

The last few minutes kick things up a notch by adding a Turbo Frame to make the charts update in real time. No more refreshing your analytics page!

Stimulus Outlets API

by David Kimura @kobaltz

Excited to try the new Outlets API in Stimulus 3.2? This video walks through a quick example of how to get started.

David creates two Stimulus controllers - one to alert the user and another to trigger the event. Previously, this would have required one controller wrapping the other in the DOM. But now we can keep our controllers self-contained and reference the other with the Outlets API.

👀 One more thing…

I’d like to ask a small favor: can you please share this newsletter with a Rails or Hotwire friend who could benefit from my content? It would mean a lot to me.

I’m going to publish more content in places I own: this newsletter and my blog. It brings me pure joy to hear feedback and replies from everyone who reads this newsletter. It’s an honor and a privilege to hit send every month.

I want to make this newsletter even better for you next year!

Also, to be honest, I’m not so sure about a certain social network. And I’m contemplating its role in my life moving forward. The bird site helped me launch my independent business. More importantly, it’s connected me with amazing people — both online and in real life.

But my time there may look different. In the meantime, the best place to keep up with me will be this newsletter. Thank you for being here!

Thanks,
Joe Masilotti
@joemasilotti joe@masilotti.com