Rails Trackable Concern: Automatically Set Created By

The Trackable concern automatically assigns the created_by field when a record is created, using Current.user. This keeps model logic clean while ensuring proper tracking of record ownership. The Post model includes Trackable, to get this functionalit

Rails Email Domain Validation: Restrict to Approved Domains

This custom EmailDomainValidator ensures users can only register with emails from approved domains. It extracts the domain from the email and checks it against a predefined list. The User model applies this validation. A challenge is making the allowe

Rails Callback: Automatically Normalize Usernames Before Save

This snippet ensures usernames are consistently formatted by automatically trimming spaces and converting them to lowercase before saving. The before_save callback in the User model prevents duplicates caused by case differences. The UsersController h

Rails Background Jobs with ActiveJob and Sidekiq

Background jobs improve performance by offloading time-consuming tasks like sending emails. This snippet uses ActiveJob with Sidekiq to send a welcome email asynchronously. The SendWelcomeEmailJob finds the user and triggers the mailer, while the User

JavaScript Debounce Function to Optimize Event Handling

Debouncing is essential for optimizing performance when handling frequent events like scrolling, resizing, or input typing. This snippet defines a debounce function in a separate file for modularity, allowing easy reuse across projects. The resizing.j

Rails ActiveStorage: Attach and Display Images Easily

This snippet enables a User model to attach an avatar image with just has_one_attached :avatar. The view logic checks if an avatar exists and displays it using image_tag. This approach avoids complex file storage logic, letting Rails handle uploads se

Using Rails Callbacks to Auto-Generate a Unique Slug

Slugs are essential for SEO-friendly URLs and user-friendly navigation, and this snippet ensures each article automatically generates a unique slug based on its title. Using the before_create callback, we transform the title into a URL-safe format usi

Rails Enum for Clean and Readable Status Management

Managing statuses in a Rails model can become messy with hardcoded values, but enums provide a clean and readable way to handle them. This snippet shows how to define an enum in an Order model, making it easy to reference statuses with descriptive sym

Ruby on Rails Concern for Soft Deletable Records Without Losing Data

Trix editor with copy-paste Youtube embeds in Ruby on Rails, htmx and hyperscript

Add product to cart in Shopify with Liquid, htmx and hyperscript

Build image, run container, connect to already running container and investigate