ruby

ruby
class CreateMaintenanceTasks < ActiveRecord::Migration[7.0]
  def change
    create_table :maintenance_tasks do |t|
      t.string :name, null: false
      t.string :state, null: false, default: "pending"
      t.datetime :started_at

Database-Backed “Run Once” Migrations for Maintenance Tasks

rails migrations background-jobs
by codesnips 3 tabs
ruby
class CreateEmailDeliveries < ActiveRecord::Migration[7.1]
  def change
    create_table :email_deliveries do |t|
      t.string :dedupe_key, null: false
      t.string :mailer, null: false
      t.string :action, null: false

Transactional Email “Send Once” with Delivered Marker

rails reliability activerecord
by codesnips 4 tabs
erb
<%# Append is also broadcast from the model; this response scrolls the author's view %>
<%= turbo_stream.append "messages" do %>
  <%= render partial: "messages/message", locals: { message: @message } %>
<% end %>

<turbo-stream action="scroll_to" target="<%= dom_id(@message) %>" behavior="smooth" block="end">

Scroll into view after append using a custom Turbo Stream action

rails hotwire turbo
by codesnips 4 tabs
ruby
require "faraday"
require "faraday/retry"

module Http
  class RetryableError < StandardError; end
  class CircuitOpenError < StandardError; end

HTTP Timeouts + Retries Wrapper (Faraday)

rails http reliability
by codesnips 3 tabs
yaml
:concurrency: 10
:queues:
  - [critical, 4]
  - [default, 2]
  - [low, 1]

Background jobs with Sidekiq and reliable queues

rails sidekiq background-jobs
by Alex Kumar 2 tabs
ruby
class SupportMailbox < ApplicationMailbox
  def process
    ticket = Ticket.find_or_create_by!(message_id: mail.message_id) do |t|
      t.subject = mail.subject
      t.from_email = mail.from&.first
      t.body = mail.decoded

Action Mailbox: broadcast incoming emails into a feed

rails hotwire turbo
by Henry Kim 3 tabs
ruby
FactoryBot.define do
  factory :post do
    association :author, factory: :user
    sequence(:title) { |n| "Post Title #{n}" }
    body { Faker::Lorem.paragraphs(number: 3).join("\n\n") }
    status { :draft }

Rails fixtures vs factories for test data

rails testing fixtures
by Maya Patel 2 tabs
ruby
class AtLeastOneOfValidator < ActiveModel::Validator
  def validate(record)
    fields = Array(options[:fields])
    raise ArgumentError, "provide :fields" if fields.empty?

    return if fields.any? { |field| filled?(record.public_send(field)) }

Custom Validator for “At Least One of” Fields

rails activemodel validations
by codesnips 3 tabs
ruby
class CircuitBreaker
  class CircuitOpenError < StandardError; end

  INCREMENT = <<~LUA.freeze
    local n = redis.call('INCR', KEYS[1])
    if n == 1 then redis.call('EXPIRE', KEYS[1], ARGV[1]) end

Graceful Degradation: Feature-Based Rescue

rails resilience circuit-breaker
by codesnips 3 tabs
ruby
# Fastlane Fastfile for automated deployment
default_platform(:ios)

platform :ios do
  desc "Push a new beta build to TestFlight"
  lane :beta do

App Store submission and TestFlight beta testing

ios app-store testflight
by Sofia Martinez 2 tabs
ruby
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  before_action :authenticate_user!
end

CSRF protection for Rails and JSON APIs

csrf rails api
by Kai Nakamura 2 tabs
ruby
class Timer
  def self.measure
    start_time = Time.now
    yield if block_given?
    end_time = Time.now
    end_time - start_time

Blocks, Procs, and Lambdas for functional programming

ruby blocks procs
by Sarah Mitchell 3 tabs