Custom product badges based on tags in Shopify Liquid

This setup separates the product badge logic into its own snippet product-badge.liquid, making it reusable across different sections like product listings and featured collections. The product-card.liquid section renders the badge alongside the produc

Automatically generate two-word slugs in Ruby on Rails

This snippet ensures every article gets a unique, readable slug based on its title, keeping it short and pronounceable. It grabs the first two words of the title and appends a 4-character random string to prevent duplicates. The before_validation call

Storing & querying IP addresses efficiently in Ruby on Rails

This snippet stores user IPs efficiently using the PostgreSQL inet type, which optimizes space and lookup speed. The from_same_network scope allows querying users within the same /24 subnet, useful for analytics or security checks. The update_ip metho

Detecting and storing user's timezone in Ruby on Rails automatically

This snippet automatically detects and stores a user's timezone by capturing it in JavaScript and saving it in a cookie. On every request, Rails reads this value and updates the user’s timezone in the database if needed. The local_time method then all

Adding polymorphic emoji reactions to any model in Ruby on Rails

This snippet introduces a polymorphic emoji reaction system in Rails, allowing models like Post (or any other) to receive emoji reactions. The Reaction model supports different reactable types via polymorphic: true. A simple emoji regex ensures valid

Ruby on Rails custom validator to validate HEX color format

This snippet defines a custom validator for hex color codes, ensuring that values follow the correct #RGB or #RRGGBB format. By using ActiveModel::EachValidator, the validation logic remains reusable across multiple models. This approach keeps models

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