rails

ruby
class AddSoftDeleteToUsers < ActiveRecord::Migration[7.1]
  def change
    add_column :users, :deleted_at, :datetime

    add_index :users, :deleted_at, where: "deleted_at IS NULL", name: "index_users_on_live"

Soft-Delete with a Default Scope, Restore Action, and Unique Index Guard in Rails

rails activerecord soft-delete
by codesnips 3 tabs
ruby
class AddSlugToArticles < ActiveRecord::Migration[7.1]
  def change
    add_column :articles, :slug, :string
    add_index :articles, :slug, unique: true

    reversible do |dir|

Auto-Generating URL Slugs in Rails with a before_validation Callback and Friendly Finder

rails activerecord slugs
by codesnips 4 tabs
erb
<!DOCTYPE html>
<html>
  <head>
    <title><%= content_for(:title) || "App" %></title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

Disable Turbo Drive on external links

rails hotwire turbo
by codesnips 3 tabs
javascript
import { Turbo } from "@hotwired/turbo-rails"

Turbo.StreamActions.redirect = function () {
  const url = this.getAttribute("url")
  if (!url) return

Turbo Streams: server-driven redirect (Turbo Native friendly)

rails hotwire turbo-streams
by codesnips 4 tabs
ruby
require 'activerecord-import'

class BulkImportPostsService
  BATCH_SIZE = 1000

  def initialize(csv_file_path)

Bulk operations with ActiveRecord import

rails performance activerecord
by Alex Kumar 1 tab
erb
<!DOCTYPE html>
<html>
  <head>
    <title><%= content_for(:title) || "App" %></title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

Importmap setup for Stimulus controllers (Rails 7 style)

rails hotwire stimulus
by codesnips 4 tabs
erb
<div class="bookmarks">
  <h1>Saved Bookmarks</h1>

  <table>
    <tbody id="bookmarks">
      <%= render partial: "bookmark", collection: @bookmarks %>

Link that submits DELETE with Turbo (data-turbo-method)

rails hotwire turbo
by codesnips 4 tabs
ruby
require "securerandom"

class RedisMutex
  class LockError < StandardError; end

  UNLOCK_SCRIPT = <<~LUA.freeze

Redis-Based Distributed Mutex (with TTL)

rails redis concurrency
by codesnips 2 tabs
javascript
let installed = false;

function notify(message, level = "error") {
  document.dispatchEvent(
    new CustomEvent("flash:show", { detail: { message, level } })
  );

Turbo Drive lifecycle: attach global error handler

rails turbo hotwire
by codesnips 3 tabs
ruby
class PresenceRegistry
  TTL = 30 # seconds a user counts as present without a heartbeat

  def initialize(room_id, redis: REDIS)
    @room_id = room_id
    @redis = redis

Action Cable Presence Tracking (Lightweight)

rails actioncable redis
by codesnips 3 tabs
javascript
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["wrapper", "template", "anchor"]

  add(event) {

Stimulus: nested fields add/remove without re-rendering

rails stimulus hotwire
by codesnips 4 tabs
ruby
class Article < ApplicationRecord
  has_many :taggings, dependent: :destroy
  has_many :tags, through: :taggings

  scope :published, -> { where.not(published_at: nil) }

Filtering a Listing by Tags with a has_many :through Scope and a Query Object

rails activerecord has-many-through
by codesnips 3 tabs