Understanding Rails

Understanding Rails · On the job · Ticket 003

Long notes get rejected

Episode 103 · deep dive · about 13 minutes · Rails 8.1 · by

How was this episode?

The series' author reads every note, and no one else sees it. When something is wrong or unclear, it gets fixed and the episode re-cut. How this series stays right.

Was it clear?

Not endorsed yet. Are you a Rails developer? What's an endorsement?

Endorse this version To endorse: create an account, confirm your email, then link GitHub.

A senior works ticket 003, a feature, out loud: refine past raising the limit, a system test, a Stimulus counter that counts the way the server does, the no-JavaScript text, and CI's review.

Free to watch. If it helped, support the series.

Transcript

0:00 Long notes get rejected

On the job. Ticket three: long notes get rejected. The first two tickets were about something going wrong. This one is a feature: someone wants the app to do something new. A feature tempts you to build the first idea on the ticket. So again, the work starts at the ticket, not in the code.

0:22 The ticket

Here it is. Morgan, the product manager, writes: since we fixed the vanishing notes, hikers at least find out when a note is too long. But people write a whole report, and only find out when they press post. Can we stop people writing essays that get rejected? Could we just raise the limit to five thousand? Below it, Dana: I wrote up the washout on my phone at the trailhead. I pressed post, and it said my note was too long. Too long by how much? I had to guess what to cut, standing in the rain.

0:49 Shovel ready

Is it shovel ready? Not yet. Nothing is broken, so there's no bug to reproduce. For a feature, we ask what happens today, and what should happen instead. Nobody has said what done looks like. And a fix is already on the table: raise the limit.

Read it slowly. Only find out when they press post: the problem is when they find out. Too long by how much: the error doesn't say. On my phone, at the trailhead: a small screen, and maybe a weak signal. And raise the limit to five thousand? That's Morgan's fix. Keep the facts, and hold the fix loosely.

1:26 Questions for the reporter

Now, questions for Morgan. Start with the fix on the table. Would raising it to five thousand fix it? For a while, maybe. People who write long would hit five thousand instead. And followers already say the emails are long. If hikers could see the limit coming, I'd keep five hundred. So the limit isn't the problem. Finding out too late is.

When does a hiker find out about the limit today? Only after they press post. Nothing on the form says there's a limit. That turns the ticket around. It's not change the limit. It's show the limit, and how close they are, before they press post.

What counts as a character? Do hikers use emoji, or line breaks? All the time. Boots, mountains, the warning sign. I don't mind how it counts, as long as the number they see is the number that gets checked. Write that down. It's a rule a test can check, and we'll need it.

Should the form stop them typing at five hundred? I'd hate that. Dana writes in her notes app and pastes it in. I'd rather she sees she's over by forty, and trims it herself. A box that stops at five hundred cuts off a pasted report with no warning. Words lost, like ticket one.

Does it need to work on a bad connection? Yes. Some trailheads get one bar, and people post from a half loaded page. A half loaded page may not run JavaScript yet. So the limit has to be in the page itself, and the server keeps checking it.

Do any hikers use a screen reader? Yes. Jo leads our accessible trails group, and posts from her phone with one. She turns off sites that read out a number after every letter. So the count has to be heard too, but politely, once she pauses.

I asked other questions too. Why five hundred? What are hikers writing on? How many have hit this? Can they edit a note? Do long notes cost us anything? Morgan answered each one honestly. Phones matter for the layout. None of the rest changes what to build.

3:30 Ticket 001's fix first

One thing before we look. This ticket builds on ticket one's fix. If you did ticket one, it's on your fork's master. Switchback's own master keeps ticket one's bug, for learners. So I start from a branch with ticket one's reference fix, and treat it as my master.

3:47 What happens today

Now, what happens today, in the real app. Sign in, and open Granite Switchbacks. Here's the note form: a box, and a post button. Nothing says there's a limit. Paste Dana's report: six hundred and four characters. Post it. Your note is too long, maximum is five hundred characters. Every word stays in the box. But too long by how much? It doesn't say.

4:11 Why it happens

Why does it happen? Search the app for five hundred. One line: the note model's validation. The body is required, and at most five hundred characters. The limit lives only there, on the server, and it runs after the hiker presses post. The form never mentions it.

4:29 Acceptance criteria and scope

Now we can write the ticket properly. A title that says what's wrong: the note form doesn't show the five hundred character limit until it's too late. Then acceptance criteria. One: before typing, the form says notes can be up to five hundred characters, even without JavaScript. Two: as the hiker types, the form shows how many are left, counted the way the server counts. Three: the hiker can type past the limit, nothing is cut off, and the form shows how far over.

Four: a screen reader politely hears how far over. Five: the server still rejects a note over five hundred, and a note of five hundred still posts. Notice criterion two. It doesn't say how to count. It says agree with the server. That's Morgan's rule, made testable.

Then what's out of scope. Raising the limit. Counting what looks like one character as one. A family emoji is five characters to the server, so the counter says five too. Line breaks in a note sent without JavaScript: a small next ticket. And editing a note after it's posted.

Check it against shovel ready. We know who, where, what they need, and what happens today. The criteria can be tested, and the scope is said. It fits in one pull request. Nothing blocks starting. Now, code.

5:49 A failing system test

First, a test that fails. The counter runs in the browser, so its test needs one: a system test, in headless Chrome. Sign in, and open a trail. Type, bridge out at mile two, and expect four hundred and eighty characters left. Then type five hundred and thirty seven. Every one stays in the box. Expect thirty seven over the limit, on the page and in a polite live region.

Run it. First, an error: star is not a valid URL. Recent Chrome refuses the call Capybara makes to clear cookies, so the system test case clears them itself. Run it again: two failures, the right ones. The page has no count at all.

6:31 The Stimulus counter

Now the counter: a Stimulus controller. Where does it get five hundred? Don't type it again in JavaScript. Give the note model a constant, body limit, and the validation uses it. The form hands it to the controller as a value. One number, in one place. On each input, the controller shows so many left, or so many over.

And for Jo: a second paragraph, hidden from the eye. It's a live region, marked polite. It's set a second after the hiker stops typing, so a screen reader hears the count once, when she pauses. Run the tests. Both pass.

7:07 The trap

Morgan said hikers use emoji all the time. So add a test: wade the creek, and two boot emoji. That's seventeen characters, so four hundred and eighty three left. Run it. One failure. Expected four hundred and eighty three. The page says four hundred and eighty one. The counter called each boot two characters.

Who's right? Ask Rails, with bin rails runner: a boot's length is one. Ask Chrome: two. Ruby counts code points. A JavaScript string's length counts U T F sixteen units, and a boot takes two. So in the app, type four hundred and ninety nine letters and a boot. The counter says one character over. Post it anyway: note posted.

7:50 Counting like the server

So count the way the server counts. Array dot from splits a string into code points, so a boot is one. That's Morgan's rule: the number they see is the number that gets checked. Run the tests: three runs, no failures. In the app, the two boots now leave four hundred and eighty three.

8:09 Without JavaScript

Now the half loaded page. Turn JavaScript off, and open the trail. Under the box, nothing, because only the controller fills in the count. A hiker on one bar sees no limit. First a test, and this one needs no browser: get the trail page, and expect up to five hundred characters. It fails.

So the server writes it: up to, note body limit, characters. When the controller connects, it replaces that with the live count. The test passes. The page works on its own first, and JavaScript makes it better.

8:43 The server decides

And the server still decides. Paste Dana's report, with JavaScript on. The count says a hundred and four characters over. Post it anyway. Your note is too long, and the count is still there under the box. Now Dana knows how much to cut.

9:00 bin/ci and the commit

Before pushing, run what C I runs. Bin C I: the style check, the security audits, Brakeman, the tests, and the seeds. All pass. But it ran seventeen tests, and none of them were system tests. Bin C I skips those. So run them too: three runs, no failures.

Then a commit message that says why. Hikers found out a note was over five hundred characters only after they pressed post, and then not by how much. And a second paragraph on counting: Rails counts code points, so a boot is one character, and value dot length called it two.

9:36 The pull request

Push the branch, named for the ticket, and open a pull request. Its base is the branch with ticket one's fix, so it shows only this ticket's work. I pushed the test first. C I's review: one of five pass, and the review check is red. The test check is green: it doesn't run system tests either.

9:56 CI's review

Then push the fix. The same comment updates: five of five, and every check goes green. Three of those criteria ran in headless Chrome on GitHub's machine, emoji and all.

10:09 Answering the reviewer

Passing isn't the same as approved. So the comment adds what a reviewer would still ask. Answer each one.

Where is your test? The system tests, in headless Chrome, failed before the counter existed. And an integration test checks the limit in the page, with no browser at all.

Where does the five hundred live now? In one place: note body limit. Search the app for five hundred: one line, the constant. Change it to six hundred, and the page says up to six hundred.

How do you count? Code points, the way Rails does. A boot is one, and a family emoji is five, in Ruby and in the counter.

What does a screen reader hear while the hiker types? I typed a sentence a key at a time, and watched the polite region. While typing, it didn't change. After a pause, it changed once. I haven't listened with VoiceOver or TalkBack yet. That's the next check, on a real phone.

What happens without JavaScript? Post a note with nine line breaks. With JavaScript, Turbo sends each one as one character: five hundred, and it posts. Without it, the browser sends each as two: five hundred and nine, rejected. Out of scope, so write it up as a ticket.

Did you block anything? No. There's no max length on the box. Five hundred and thirty seven typed, five hundred and thirty seven kept. A max length would cut off a pasted report, ticket one's problem again.

Does it fit on a phone? On a phone sized screen, the box is narrow, the count sits under it, and the post note button stays beside it, in reach.

Does your commit message say why? It says what went wrong for Dana, and why the counter counts code points.

11:58 Closing the loop

Last, close the loop with Morgan. We kept five hundred. Now the form shows the limit before anyone types, counts down as they write, and says how far over they are. And one small thing for later: line breaks in a note sent without JavaScript.

12:15 The rules

So: a feature is a ticket too. Ask whether the fix on the ticket fixes the problem. Ask who it's for: a phone, one bar, a screen reader. Count the way the server counts. Make the page work first, then make it better. And let the server decide.

12:32 Your turn

Now it's your turn. Ticket three has its own page, where you ask Morgan your own questions before you see ours. Do ticket one first. Then make a branch named for the ticket, from your fork's master, and open a pull request in your fork. C I reviews it, some of it in a real browser.

Check your understanding

  1. A note's body is validated with length: { maximum: 500 }. How many characters does Rails count for one boot emoji, 🥾?

    • Four, one for each byte
    • Two, the way a browser's value.length does
    • One, because the validation measures String#length, which counts characters
    Show answer

    One, because the validation measures String#length, which counts characters. LengthValidator compares value.length with the maximum, and Ruby's String#length counts characters, not bytes. 🥾 is one. Source: LengthValidator measures value.length (Rails source, v8.1.4.1).

  2. In the browser, "🥾".length is 2. Why?

    • A JavaScript string's length counts UTF-16 code units, and 🥾 takes two
    • The browser counts the emoji and its color
    • JavaScript counts bytes
    Show answer

    A JavaScript string's length counts UTF-16 code units, and 🥾 takes two. A JavaScript string's length is its number of UTF-16 code units. Characters outside the first 65,536 code points, such as most emoji, take two. Array.from iterates by code point, so Array.from("🥾").length is 1. Source: String: length, in UTF-16 code units (MDN).

  3. The count is copied into an element with aria-live="polite". When does a screen reader say it?

    • At once, interrupting whatever it is saying
    • At the next graceful opportunity, such as when the user pauses typing
    • Only when the user moves focus to it
    Show answer

    At the next graceful opportunity, such as when the user pauses typing. A polite live region's updates are presented at the next graceful opportunity, such as the end of the current sentence or when the user pauses typing. Source: aria-live (MDN).

  4. Which kind of test can check that the counter updates as the hiker types?

    • An integration test, which runs the controller and checks the HTML
    • A system test, which drives a real browser that runs the page's JavaScript
    • A model test of Note
    Show answer

    A system test, which drives a real browser that runs the page's JavaScript. The counter runs in the browser. A system test drives a real browser, here headless Chrome, so the page's JavaScript runs. An integration test can still check the limit written into the HTML. Source: Testing Rails Applications: System Testing (Rails Guides 8.1).

Sources

Every example in this episode was run on Rails 8.1.4.1 and Ruby 4.0.10.

Comments

Sign in to comment or sign up.