Code Explainers
Code explainers tagged #callbacks
ruby
class Order < ApplicationRecord include AASM belongs_to :warehouse
Modeling an order lifecycle with AASM in Rails
state machine
guards
callbacks
Intermediate
10 steps
ruby
module Authenticatable extend ActiveSupport::Concern included do
JWT authentication as a Rails concern
authentication
jwt
concerns
Intermediate
6 steps
ruby
class Contact < ApplicationRecord PHONE_FORMATS = { "US" => /\A\+1\d{10}\z/, "GB" => /\A\+44\d{10}\z/,
Country-aware phone validation in Rails
validation
callbacks
regex
Intermediate
6 steps
ruby
class Product < ApplicationRecord belongs_to :category, touch: true has_many :reviews, dependent: :destroy
How Rails models wire up associations
active record
associations
validations
Intermediate
7 steps
java
public List<OrderSummary> streamRecentOrders(LocalDateTime since, Consumer<OrderSummary> handler) { String sql = """ SELECT id, customer_id, total_cents, status, created_at FROM orders
Streaming large JDBC result sets safely
jdbc
streaming
resource-management
Intermediate
7 steps
ruby
class Order class InvalidTransition < StandardError; end TRANSITIONS = {
A state machine for order transitions in Ruby
state-machine
data-driven
error-handling
Intermediate
8 steps
ruby
class ApplicationJob < ActiveJob::Base retry_on ActiveRecord::Deadlocked, wait: 5.seconds, attempts: 3 discard_on ActiveJob::DeserializationError
Instrumenting every job in Rails
active-job
structured-logging
retries
Intermediate
6 steps
ruby
class PaymentMethod < ApplicationRecord belongs_to :account encrypts :card_number, deterministic: false
How Rails encrypts sensitive payment fields
encryption
validation
scopes
Intermediate
9 steps
ruby
class Account < ApplicationRecord has_many :projects, dependent: :destroy_async has_many :memberships, dependent: :destroy_async has_many :invoices, dependent: :restrict_with_error
Safely closing an Account in Rails
associations
callbacks
transactions
Intermediate
9 steps
python
import os import time from pathlib import Path
Polling a directory for file changes in Python
polling
filesystem
set-operations
Intermediate
8 steps
ruby
class Product < ApplicationRecord belongs_to :account after_save :record_audit_trail
Building an audit trail with Rails callbacks
callbacks
audit-logging
dirty-tracking
Intermediate
7 steps
javascript
function createCountdownTimer(durationSeconds, { onTick, onComplete } = {}) { let remaining = durationSeconds; let intervalId = null;
Building a countdown timer with closures
closures
factory-function
timers
Intermediate
9 steps
php
<?php namespace App\Events;
A priority event dispatcher in PHP
observer-pattern
callbacks
priority-ordering
Intermediate
8 steps
ruby
class Comment < ApplicationRecord belongs_to :post belongs_to :author, class_name: "User"
Live-updating comments with Turbo in Rails
turbo-streams
callbacks
associations
Intermediate
8 steps
ruby
class Comment < ApplicationRecord belongs_to :commentable, polymorphic: true, counter_cache: true belongs_to :author, class_name: "User"
How polymorphic comments work in Rails
polymorphic-association
concerns
counter-cache
Intermediate
8 steps
ruby
module SoftDeletable extend ActiveSupport::Concern included do
How a soft-delete concern works in Rails
concerns
soft-delete
scopes
Intermediate
8 steps
ruby
class Post < ApplicationRecord before_validation :generate_slug, on: :create validates :slug, presence: true, uniqueness: true
How a Rails model builds unique slugs
slugs
callbacks
validations
Intermediate
7 steps
ruby
class EventEmitter def initialize @listeners = Hash.new { |hash, key| hash[key] = [] } end
Building an EventEmitter in Ruby
pub-sub
closures
callbacks
Intermediate
7 steps