This week in #dev (Dec 30, 2022)

thoughtbot
Edited by Stephanie Minn

Hey, happy new year! Welcome to the first 2023 edition of This Week in #dev, a series of posts where we bring some of the most interesting Slack conversations to the public. Today we’re talking about hiding HTML content, Active Record methods, and debugging system tests.

HTML’s hidden Global Attribute

Steve Polito has discovered a global attribute in HTML that can be used to hide content from the user. This attribute is hidden from all presentations, including screen readers. You can read more about it here.

<p>This content should be read right now, as it is important. I am so glad you are able to find it!</p>
<p hidden>This content is not relevant to this page right now, so should not be seen. Nothing to see here. Nada.</p>

Using destroy_async on has_many Associations

Rémy Hannequin discussed the use of the dependent: :destroy_async option for has_many associations in Ruby on Rails. This option will enqueue an ActiveRecord::DestroyAssociationAsyncJob job which will call destroy on the associated objects, but Active Job must be set up for it to work.

The Benefits of Enumerator#with_index

Neil Carvalho explained that Enumerator#with_index can take an offset argument, which is useful when you need to start the index at a number other than 0.

# Note the `each` to build an enumerator
["first", "second", "third"].each.with_index(1) do |element, index|
  # element => "first" / index => 1
  # element => "second" / index => 2
  # element => "third" / index => 3
end

Active Record Query Methods: in_order_of

Matheus Richard suggested using the Active Record method in_order_of method to order records in a specific order.

User.in_order_of(:id, [1, 5, 3])
# This generates this SQL query:
# SELECT "users".* FROM "users"
#   WHERE "users"."id" IN (1, 5, 3)
#   ORDER BY CASE
#     WHEN "users"."id" = 1 THEN 1
#     WHEN "users"."id" = 5 THEN 2
#     WHEN "users"."id" = 3 THEN 3
#   END ASC
#
# => [#<User id: 1, name: "First User">,
#     #<User id: 5, name: "Fifth User">,
#     #<User id: 3, name: "Third User">]

Debugging with Debugger vs. Binding.pry

Neil also discovered a difference between using debugger and binding.pry to pause a system test using Capybara. debugger pauses the entire program execution, while binding.pry pauses only the spec. With binding.pry, the background process running the system spec server is not paused, allowing the user to manually click and inspect the network effects of that click.

Thanks

This edition was brought to you by Matheus Richard, Neil Carvalho, Steve Polito, and Rémy Hannequin. Thanks to all contributors! 🎉