AAAAAwesome company name
I've got nothing but respect for this rando Amazon vendor's alpha-sort-hacking game.
I've got nothing but respect for this rando Amazon vendor's alpha-sort-hacking game.
Just me or is universal <video preloading=none>
support a lie? macOS Safari is downloading the entirety of 36 (hidden!) videos in the DOM on page load.
Having to resort to waiting to set the <source src>
with JavaScript after user action. imagekit.io/blog/lazy-loading-html-videos/
A Breaking Change listener replied to v16 asking to see my new pool drain hose, and since it happened to rain today, here you go, my dude: youtube.com/shorts/Pq2NK-8hNqs
For iOS 16, Apple overhauled the iPhone lock screen and the one feature they shipped that I really, really wanted was the ability to shuffle depth-effect photos of my spouse. It's called "Photo Shuffle", and you get there by adding a new lock screen, tapping "Photo Shuffle", and selecting "People". The Big Idea is that your phone would use machine learning to select great photos and then apply a depth effect (i.e. clipping the subject in front of the time). However, instead of having users select "People & Pets" from a standard iCloud Photos picker, you get an arbitrary smattering of a couple dozen randos in a bare bones custom UI.
So what's my beef with this feature? Over the course of 2 years and 7 devices, my wife has never been among the options presented to me. Can't select her. Doesn't matter that I've named her in the Photos app. Or favorited her. My library has over 25,000 photos of her for crissakes. Who can I pick from instead? Well, there are least 3 kids whose names I never knew and for whom Becky appears to have had as Spanish students for a single semester in 2009. Great job, everyone.
As it turns out, I am not alone.
I first encountered this bug in iOS 16 developer beta 1 on June 6, 2022. It has persisted across four iPhones and three iPads, even when set up fresh, not-from-backup. Not only that, I always see the exact same list of people I don't care about. Most of whom I never even bothered to name in Photos, which suggests the bug lives in the cloud, which is just great.
Jason Snell reported on this feature's problematic design last year (during iOS 17 beta season), for MacWorld:
Photo Shuffle's method of offering people to display appears utterly broken. It offered my wife a small number of faces, most of whom were completely random and fairly uncommon. She's got hundreds, if not thousands, of pictures of me and our kids on her phone, and yet we weren't among the faces offered. And if the faces you're looking for aren't in Photo Shuffle's very small list of options, there's no recourse. You're stuck.
Well, here we are, one year later, and I'm unhappy to report: Photo Shuffle is still broken in iOS 18. It doesn't seem to have been touched at all.
When people talk about the inscrutability of machine-learning and AI as being problematic, this is as practical an example as I can think of. All I want to do is shuffle photos of my wife on my lock screen, but there's no action I can take as a user—no amount of hardware purchases, software updates, or device factory resets—to make that happen. Apple Support can't do anything either. I doubt the engineers who worked on it could. Whenever anyone says "AI", everyone involved quickly absolves themselves of responsibility—it's a black box.
I wasn't happy with the Faker gem's Lorem and Markdown modules, so—for anyone who might need it—I wrote a silly function to generate more realistically sized paragraphs with Markdown bits gist.github.com/searls/2859ad7e8941872edb9561eb965b7c76
Big day: now with transition music! 🎶
There's one topic that's been dominating headlines for the last two weeks, but if you want to hear about it you'll have to look elsewhere! That's because Breaking Change is a safe space. So listen up and let's find other things to get mad about.
Do you have opinions about politics? Do you want to share them with an Internet friend? Now's your chance: podcast@searls.co. (Other topics also welcome.)
I heard you like links:
I really enjoyed this discussion with host Tim Chaten about the state of Apple Vision Pro. It was recorded a couple weeks after WWDC, which meant the memory was fresh enough to keep all of Apple's announcements top of mind but distant enough to imagine various directions things could go from here.
I gotta say, it was nice talking to someone who knows and cares more about the platform than I do. Some real "there are dozens of us!" energy around the Vision Pro right now.
Rails + Hotwire is very capable of dynamic behavior like replacing a component in the DOM by sending HTML over-the-wire in response to a user action, but the fact it requires you to touch half a dozen files in the process can make the whole thing feel daunting. Rails itself has always been this way (with each incremental feature requiring a route, model, controller, view, etc.), but I've been using it long enough that I sometimes forget that—similar to learning a recipe—I originally needed months of intentional practice to internalize and gain comfort with the framework's most routine of workflows.
So, like a recipe card, here is a reusable approach to swapping out a <div>
rendered by a Rails partial with a turbo stream whenever a user selects an
alternate model from an input (specifically, a select
) using Turbo 8, Stimulus
1.3, and Rails 7.1.
That's it, 6 key ingredients. If you're curious, step 5 contains the most magic flavoring. 🪄
Ingredients in hand, let's walk through each of the steps needed to go end-to-end with this feature.
First, create a partial that you want to render inside the <div>
. Let's assume
we want users to be able to change out a generic model named Item
, which has
a conventional ItemsController
.
In that case, let's place a partial that renders the details about an item alongside
the controller's views, in _detail.html.erb
:
<!-- app/views/items/_detail.html.erb -->
<div>
<%= item.title %>
<!-- Other item stuff… -->
</div>
Next, we'll add the necessary route for the detail
action:
# config/routes.rb
resources :items do
get :detail, on: :collection
end
This will define a path helper detail_items_path
, which works out to
"/items/detail"
.
Note that I threw this on the :collection
so that our stimulus controller can
more easily specify the URL via query parameters instead of interpolating a
fancier member route (e.g. "items/42/detail"
).
With the route defined, we'll add a simple controller action that only responds to turbo stream requests.
Here's what that might look like:
# app/controllers/items_controller.rb
class ItemsController < ApplicationController
def detail
@dom_id = params[:dom_id]
@item = Item.find(params[:id])
end
end
This dom_id
param might throw you off at first, but it's important to keep in
mind that unique HTML IDs are the coin of the realm in
Turbo-land. You'll see how it
gets set later, in step 5.
To finish the route-controller-view errand, we'll create a view for the detail
action,
with the turbo_stream.erb
extension instead of html.erb
:
<!-- app/views/items/detail.turbo_stream.erb -->
<%= turbo_stream.update @dom_id do %>
<%= render partial: "detail", locals: { item: @item } %>
<% end %>
Because both the turbo stream and the original view need to render items in
exactly the same way, detail.turbo_stream.erb
view responds by rendering the
_detail.html.erb
partial. If you inspect the HTML that comes over the wire,
you'll see that only the turbo stream tag containing this partial is
transferred, which often means barely more data is transferred than had we
implemented this as a single-page JavaScript by making a similar HTTP request
for JSON.
In order for users' selections to have any effect, we need JavaScript. We could
write a Stimulus controller that's coupled specifically to this Item
model,
but it's no more work to make it generic, which would allow us to reuse this
functionality elsewhere in our app. So let's do that.
You can do this the hard way by using the browser's built-in fetch
API to
construct the URL, set the Accept
header to text/vnd.turbo-stream.html
, and
replace the element's innerHTML
in the DOM, but that's easy to screw up (in
fact, I screwed it up twice while writing this). So instead, I'd recommend pulling
in the requestjs-rails gem, by first
chucking it in your Gemfile
alongside any other front-end related gems:
gem "requestjs-rails"
Here's the final Stimulus controller. Deep breath, as I haven't explained all this yet:
// app/javascript/controllers/model_swap_controller.js
import { get } from '@rails/request.js'
import { Controller } from '@hotwired/stimulus'
export default class ModelSwapController extends Controller {
static targets = ['container']
static values = {
path: String,
originalId: String
}
swap (event) {
const modelId = event.currentTarget.value || // Value from input action
event.detail?.value || // Value from custom event (e.g. hotwire_combobox)
this.originalIdValue // Fallback to original value if input value is cleared
get(this.pathValue, {
query: {
id: modelId,
dom_id: this.containerTarget.id
},
responseKind: 'turbo-stream'
})
}
}
That get
function from @rails/request.js
handles all the housekeeping you
might hope it would. When I switched to it, the fact it worked the
instant I plopped it in gave me Dem Magic Vibes that keep me coming back to
Rails 18 years in.
This controller also contains two values and a target:
path
value: this is just a URL, which we'll set to our
intentionally-parameter-free detail_items_path
originalId
value: this is the Item
ID that was first rendered when the
page loaded. By having this available as a fallback, we'll be able to gracefully
handle the user choosing a blank option from the select
by restoring the
original itemcontainer
target: this is the DOM element containing the partial we're going
to swap out. Note that it must have a unique id
attribute, which we're
including in our request to the server as dom_id
If this doesn't make perfect sense, I recommend wiring it up anyway and getting it working first, then debugging to inspect the values in motion.
Finally, we'll visit the original view from which the _detail.html.erb
partial
was initially extracted.
Right off the bat, you might notice that I like to use content_tag
whenever I
need to specify numerous attributes with Ruby expressions, as it requires
far fewer <%=%>
interpolations than specifying a literal <div>
:
<!-- app/views/items/show.html.erb -->
<%= content_tag :div, data: {
controller: "model-swap",
model_swap_path_value: detail_items_path,
model_swap_original_id_value: @item.id,
} do %>
<%= collection_select :item, :id, Item.all, :id, :title,
{include_blank: true},
{data: {action: "model-swap#swap"}} %>
<div id="<%= dom_id(@item, "detail") %>" data-model-swap-target="container">
<%= render partial: "detail", locals: { item: @item } %>
</div>
<% end %>
The above will probably look immediately familiar to anyone who's done a lot of work with Stimulus before and utterly arcane otherwise. Helping you sort out the latter is beyond the scope of this article, though. Ask ChatGPT or something.
The only thing in the above template that wasn't completely preordained by the
first 5 steps was the id
attribute of the wrapping div element, so I'll
explain that here. For illustration purposes, I set the container div to
dom_id(@item, "detail")
(which would work out to something like
"detail_item_42"
) to give an example of something that's likely to be unique,
but in truth, the most appropriate ID will depend on what's going on in the
broader page. For example, in the UI that inspired this blog post, I am allowing
users to replace any of a variable array of items across 3 options, so my IDs
are based on those indices, like option_2_item_4
, as opposed to the database
ID of any models. All that really matters is that the ID be unique.
Pulling off functionality like this with Turbo and Stimulus feels extra delightful, I think, if you (like so many of us) spent the last decade assuming that this kind of snappy, dynamic behavior would require a front-end JavaScript framework that would live forever and keep track of a duplicated copy of the app's state. Instead, because the server-side rendered view can draw the entire page without any JavaScript involved, any client-side changes we introduce to the state of the DOM can operate on attributes first defined by the view, keeping the entire source of truth of the current application state in one place (the DOM) instead of two (a server database and in-memory JavaScript objects).
Anyway, when it works, it's great. And when the lego bricks aren't snapping together for whatever reason, it's infuriating. Which maybe makes Hotwire the most Rails-assed extension to the framework since Rails itself. If you find yourself losing a bunch of time to what seem like trivial naming issues, just know that you're not alone. This stuff takes practice to get used to.
If you've worked through this guide, hopefully you have a functioning feature that you can continue iterating on. If you stumbled over any errata above, please let me know.
Was featured on Timothy Chaten's "Vision Pros" podcast, a show where the dozens of us dedicated Vision Pro users gather to chat about the fledgling platform.
Appearing on: Vision Pros
Published on: 2024-07-11
Original URL: https://visionpros.fm/2024/07/11/episode-19-justin-searls-from-breaking-change/
Comments? Questions? Suggestion of a podcast I should guest on? podcast@searls.co
Hello, I just wanted to say that if you want your web app to feel Cool and Modern, one of the easiest things you can do is make it so that Mac users can hit command-return anywhere in the form to submit it.
Some web sites map this to control-enter for other platforms, and that's fine, but I don't bother. Truth be told, I used to bother, but after adding it to a few web apps years ago, I actually had multiple Windows and Linux users complain to me about unintended form submissions.
I am not making a comment on the sophistication of non-Apple users, but I am saying that if you just stick this code at the top of your app, it will make it more Exclusive and feel Snappier and I will thank you for it.
Here, just copy and paste this. Don't even bother reading it first:
document.addEventListener('keydown', (event) => {
if (event.key === 'Enter' && event.metaKey) {
if (!document.activeElement) return
const closestForm = document.activeElement.closest('form')
if (closestForm) {
event.preventDefault()
closestForm.requestSubmit()
}
}
})
There's been a bug in the Apple Watch app ever since multiple watch support was added: if the pairing process fails on an additional watch and the phone begins unpairing it, ALL paired watches will be unpaired.
There is no way to recover short of setting all watches up all over again. Today marks the sixth time this has happened to me.
After a little over a year of prodding by Vini Stock to
ship a Standard Ruby add-on for Ruby
LSP, and thanks to a lot of help from
Shopify's Ruby DX team, I've
finally done it! In fact, so long as your Gemfile's version of standard
is
at least 1.39.1
, you already have the new Ruby LSP add-on. It's built-in!
Ruby LSP supports any editor with language server support, but configuration varies from editor to editor. Since VS Code is relatively dominant, I added some docs on how to set it up, but most Ruby LSP users will just need these settings to select Standard as their linter and formatter:
"[ruby]": {
"editor.defaultFormatter": "Shopify.ruby-lsp"
},
"rubyLsp.formatter": "standard",
"rubyLsp.linters": [
"standard"
]
I've been using this configuration for a bit over a week and I've decided: it's time to uninstall my own bespoke extension that we launched early last year .
I've also updated Standard's README to explain why the new Ruby LSP add-on is superior to our own built-in language server. In short, the Ruby LSP add-on supports pull diagnostics and code actions, and the built-in server does not.
Standard Ruby's built-in language server and existing VS Code extension will continue to work and be supported for the forseeable future, but it doesn't make much sense to invest heavily into new features, when the Ruby LSP add-on will get them "for free".
Three reasons:
So yeah, in the medium-term future, I see Ruby LSP and RuboCop as being better-positioned to offer a language server than Standard itself. Thanks to Will Leinweber's implementation, we may have been there first, but I have nothing to gain by my spending free time to ensure our server is somehow better than everyone else's. In the long-term, even more consolidation seems likely—which probably means Ruby LSP will become dominant. But ultimately, they're called language servers for a reason, and if Ruby shipped with a built-in language server (and an API that any code could easily plug into), it could prove a competitive advantage over other languages while simultaneously enabling a new class of tools that could each pitch in to enhance the developer experience in distinct, incremental ways.
On a human level, I think it's important not to associate the prospect of retiring one's own work with feelings of failure. Code is a liability, not an asset. Whenever I can get by with less of it, I feel relief after discarding it. If relief isn't your default reaction to a competing approach winning out on the merits (and it's understandable if it isn't; pride of authorship is a thing), I encourage you to figure out how to adopt this mindset. There are far too many problems out there worth solving to waste a single minute defending the wrong solution.
Anyway, go try out Standard with Ruby LSP and tell me how it goes! I'll be bummed if I didn't manage to break at least something.
This isn't an exciting idea, but since I know a lot of puts debuggerers who are probably accustomed to printing plain text into a sea of log noise, I thought I'd share this PSA: making your print statements visually distinctive makes them easier to spot.
If you're trying to log a value during the request/response lifecycle of a
Rails development server, there's a lot of chatter for
every page load. So instead of passing something to puts
or pp
and spraining
a squint muscle to find your print statement in the server logs, do something to
make it stand out!
You could reach for a gem like awesome_print for this, but IMNSHO there's no need to add another dependency just for this.
Since I use a white-background terminal, my quick solution was to define a
method that pretty-prints its argument with a black background (called
Kernel#buts
). Since I only need this for my development server, I chucked it
in a development-only initializer:
# config/initializers/buts.rb
return unless Rails.env.development?
module Kernel
# Make your output stand out more in the Rails server console
def buts(obj)
black_background_white_text = "\e[30;47m"
reset = "\e[0m"
puts "#{black_background_white_text}#{obj.pretty_inspect}#{reset}"
end
end
Here's the before-and-after. At a glance, can you even see what I'm trying to print in the first screenshot?
There you go. Life's too short to be hunting through logs for one-off print statements. 🔎
Over and over and over again, the same lesson: the most valuable thing a programmer can do for themselves is to invest in faster, safer feedback loops.
Forced myself to spend two hours this morning not building the thing, and the resulting script empowered me to make such aggressive and rapid changes that I accomplished a day's worth of work in the subsequent two hours.
I'm building a Rails app using Tailwind, which works really well for almost everything out-of-the-box except forms, because—unless you relish the idea of repeating the exact same list of CSS classes for each and every field in your app—you're going to be left wanting for some way to extract all that messy duplication.
To that end, I've gradually built up a custom FormBuilder to house all my classes. (If you're looking for a starting point, here's a gist of what my TailwindFormBuilder currently looks like).
This works great when you're using form_with
, because the custom form builder
will automatically take over when you set the builder
option:
<%= form_with model: @user, builder: TailwindFormBuilder do |form| %>
<% end >
And if you set it globally with ActionView::Base.default_form_builder = FormBuilders::TailwindFormBuilder
, the custom builder becomes the default.
Nifty!
But what about when you need to render input elements outside the context of a
proper form? Today, I wanted to render some checkboxes for a client-side UI that
would never be "submitted" and for which no object made sense as an argument to
form_with
. Both immediately-available options are bad:
<form>
tag by passing a dummy object to form_with
,
just for the side effect of having my TailwindFormBuilder
invoked, seemed kind
of sillyform_with
, like
check_box_field,
wouldn't invoke my TailwindFormBuilder
and would therefore lack any of its CSS classesInstead, I figured the best path forward would be to instantiate my form builder myself, even though that's not something the docs would encourage you to do to. So, I pulled up the FormBuilder#initialize source to see what arguments it needed:
def initialize(object_name, object, template, options)
# …
end
Lucky for us, the only thing that really matters above is the template
object,
which I (correctly, apparently) guessed could be passed as self
from an ERB
file or a view helper.
Here's a little helper I made to instantiate my custom TailwindFormBuilder
manually:
# app/helpers/faux_form_helper.rb
module FauxFormHelper
FauxFormObject = Struct.new do
def errors
{}
end
def method_missing(...)
end
def respond_to_missing?(...)
true
end
end
def faux_form
@faux_form ||= FormBuilders::TailwindFormBuilder.new(
nil,
FauxFormObject.new,
self,
{}
)
end
end
Explaining each of these arguments in order:
object_name
is set to nil, so the name
attribute of each input isn't wrapped in brackets (e.g. name="some_object_name[pants]"
)object
doesn't matter, because this isn't a real form, so my
FauxFormObject
just responds as if every possible value is a real property,
as well as to errors
with an empty hash (which my form builder uses to
determine when to highlight validation problems in red)template
is set to self
, because that seems to workoptions
is left as an empty hash, because I don't appear to depend on any yetThis, in turn, lets me render consistently-styled form fields anywhere I like. As a result, this ERB:
<%= faux_form.check_box(:pants, checked: true) %>
Will render with all my default checkbox classes:
<input
type="checkbox" value="1" checked="checked" name="pants" id="pants"
class="block rounded-sm size-3.5 focus:ring-3 focus:ring-success checked:bg-success checked:hover:bg-success/90 cursor-pointer focus:ring-opacity-50 border border-gray-300 focus:border-success"
>
This incongruity has been a pebble in my shoe for a couple years now, so I'm glad to finally have a working solution for rendering Tailwind-ified fields both inside and outside the context of a proper HTML form.
Hope you find it helpful! 🦦🪄
Just got this email about my inadvisably authentic podcast, Breaking Change:
"Feels like you're burning a lot of bridges with how you talk on this podcast, but I find myself… liking you more than I used to?" justin.searls.co/casts/