ruby
class JsonLogFormatter < ActiveSupport::Logger::SimpleFormatter
  def call(severity, timestamp, _progname, message)
    if message.is_a?(Hash)
      entry = {
        ts: timestamp.utc.iso8601(3),
        level: severity

Lograge-Style JSON Logging Without Extra Gems

rails logging observability
by codesnips 3 tabs
ruby
class SignupForm
  include ActiveModel::Model
  include ActiveModel::Attributes

  attribute :account_name, :string
  attribute :email, :string

Shallow Controller, Deep Params: Form Object Pattern

rails activemodel form-object
by codesnips 3 tabs
ruby
class CreateInventoryReservations < ActiveRecord::Migration[7.1]
  def change
    create_table :inventory_items do |t|
      t.string  :sku, null: false
      t.integer :quantity_on_hand,  null: false, default: 0
      t.integer :quantity_reserved, null: false, default: 0

Transactional “Reserve Inventory” with SELECT … FOR UPDATE

rails activerecord transactions
by codesnips 3 tabs
ruby
class WebhookSignature
  class VerificationError < StandardError; end

  TOLERANCE = 300 # seconds

  def initialize(payload:, header:, secrets:)

Robust Webhook Verification (HMAC + Timestamp)

rails security webhooks
by codesnips 3 tabs
ruby
class CacheNamespace
  attr_reader :name

  def initialize(name, store: Rails.cache)
    @name = name.to_s
    @store = store

Cache Key Versioning with a Single “namespace”

rails caching cache-invalidation
by codesnips 3 tabs
ruby
module DomainEvents
  class Registry
    def initialize
      @subscribers = Hash.new { |h, k| h[k] = [] }
    end

Avoid Callback Chains: Use Domain Events (In-App)

rails architecture domain-events
by codesnips 4 tabs
ruby
class DeliveryObserver
  def self.delivered_email(message)
    new(message).record
  end

  def initialize(message)

Action Mailer Delivery Observability Hook

rails observability action-mailer
by codesnips 3 tabs
ruby
module StatementTimeout
  extend ActiveSupport::Concern

  class TimeoutExceeded < StandardError; end

  def with_statement_timeout(milliseconds)

Guard Against Slow Queries with statement_timeout

rails postgres performance
by codesnips 3 tabs
ruby
module Paginatable
  extend ActiveSupport::Concern

  MAX_PER_PAGE = 100
  DEFAULT_PER_PAGE = 25

API Pagination Headers (Link + Total)

rails pagination rest-api
by codesnips 3 tabs
ruby
class AddUniqueIndexToTags < ActiveRecord::Migration[7.1]
  disable_ddl_transaction!

  def up
    execute <<~SQL
      UPDATE tags SET name = LOWER(TRIM(name)) WHERE name IS NOT NULL;

Safer “find or create” with Unique Constraint + Retry

rails activerecord concurrency
by codesnips 3 tabs
ruby
class SnipPolicyScope
  def initialize(member, relation = Snip.all)
    @member = member
    @relation = relation
  end

Composable “Policy Scope” without a Gem

rails authorization patterns
by Sarah Chen 2 tabs
ruby
require "csv"

class PeopleCsvStream
  include Enumerable

  HEADERS = %w[id full_name email signed_up_at plan].freeze

Resilient CSV Export as a Streamed Response

rails performance streaming
by codesnips 3 tabs