ruby

ruby
class DynamicFinder
  def initialize(records)
    @records = records
  end

  def method_missing(method_name, *args, &block)

Metaprogramming with method_missing and define_method

ruby metaprogramming method_missing
by Sarah Mitchell 3 tabs
ruby
class NotificationsChannel < ApplicationCable::Channel
  def subscribed
    stream_for current_user
  end

  def unsubscribed

Action Cable for real-time WebSocket communication

rails actioncable websockets
by Alex Kumar 3 tabs
javascript
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["sentinel"]
  static values = {
    url: String

Infinite scroll with Turbo Frames and Intersection Observer

hotwire turbo stimulus
by Jordan Lee 3 tabs
ruby
class User < ApplicationRecord
  has_many :posts, foreign_key: :author_id

  validates :email, presence: true,
                    uniqueness: { case_sensitive: false },
                    format: { with: URI::MailTo::EMAIL_REGEXP }

Model validations for data integrity

rails activerecord validation
by Alex Kumar 1 tab
yaml
cleanup_expired_sessions:
  cron: '0 2 * * *'  # Daily at 2 AM
  class: CleanupExpiredSessionsWorker
  queue: low
  description: Remove expired sessions from Redis

Background job scheduling with sidekiq-scheduler

rails sidekiq background-jobs
by Alex Kumar 2 tabs
erb
<%= turbo_frame_tag 'wizard_step' do %>
  <%= render "onboarding/steps/#{@step}", onboarding: @onboarding %>
<% end %>

Multi-step wizard navigation with Turbo Frames

rails hotwire turbo
by Henry Kim 2 tabs
ruby
module KeysetPaginatable
  extend ActiveSupport::Concern

  class_methods do
    def keyset_page(cursor: nil, limit: 20, direction: :desc)
      limit = limit.to_i.clamp(1, 100)

Keyset Cursor Pagination for ActiveRecord Instead of OFFSET

ruby rails activerecord
by codesnips 4 tabs
ruby
Rails.application.config.middleware.insert_before(
  Rack::Runtime,
  Rack::Timeout,
  service_timeout: 15  # 15 seconds
)

Request timeout handling with Rack::Timeout

rails reliability performance
by Alex Kumar 2 tabs
ruby
class LeaderboardCache
  TOP_KEY = "leaderboard:top".freeze
  STATS_KEY = "leaderboard:stats".freeze

  def top_players
    Rails.cache.fetch(TOP_KEY, expires_in: 5.minutes, race_condition_ttl: 15.seconds) do

Cache Stampede Protection with race_condition_ttl

rails caching performance
by codesnips 3 tabs
ruby
module MyApp
  class Application < Rails::Application
    config.load_defaults 6.1

    # Enable response compression
    config.middleware.use Rack::Deflater

API response compression with Rack::Deflater

rails performance api
by Alex Kumar 1 tab
ruby
class CreateOutboxEvents < ActiveRecord::Migration[7.1]
  def change
    create_table :outbox_events do |t|
      t.string :event_type, null: false
      t.string :aggregate_type, null: false
      t.string :aggregate_id, null: false

Transactional Outbox for Reliable Event Publishing

rails background-jobs reliability
by codesnips 4 tabs
ruby
class PopularPostsQuery
  def initialize(relation = Post.all)
    @relation = relation
  end

  def call(timeframe: 7.days, min_views: 100, limit: 20)

Query objects for complex ActiveRecord queries

rails activerecord patterns
by Alex Kumar 2 tabs