ruby

ruby
require 'swagger_helper'

RSpec.describe 'Api::V1::Posts', type: :request do
  path '/api/v1/posts' do
    get 'Retrieves all posts' do
      tags 'Posts'

API documentation with Swagger/OpenAPI

rails api documentation
by Alex Kumar 1 tab
ruby
class OverdueInvoicesQuery
  def initialize(relation: Invoice.all, as_of: Time.current)
    @relation = relation
    @as_of = as_of
  end

ActiveRecord::Relation as a Boundary (No Arrays)

rails activerecord query-objects
by codesnips 3 tabs
ruby
class SessionsController < ApplicationController
  def create
    # ... auth ...
    redirect_to dashboard_path, status: :see_other
  end
end

Use 303 See Other after POST in Turbo flows

rails hotwire turbo
by Henry Kim 2 tabs
ruby
class Order < ApplicationRecord
  include TransactionalEnqueue

  belongs_to :customer
  has_many :line_items, dependent: :destroy

Transaction-Safe After-Commit Hook (Avoid Ghost Jobs)

rails activerecord background-jobs
by codesnips 4 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 ApplicationController < ActionController::Base
  before_action do
    Current.client = request.headers['X-Client']
  end
end

Attach custom headers to Turbo fetch requests (stimulus-free)

rails hotwire turbo
by Henry Kim 2 tabs
ruby
class HealthController < ApplicationController
  skip_before_action :verify_authenticity_token

  def liveness
    render json: { status: 'ok' }, status: :ok
  end

Health check endpoint for deployment monitoring

rails deployment monitoring
by Alex Kumar 2 tabs
ruby
class CreateAuditLogs < ActiveRecord::Migration[7.1]
  def change
    create_table :audit_logs do |t|
      t.references :auditable, polymorphic: true, null: false
      t.string :actor
      t.string :action, null: false

Audit Trail with JSON Diff (Minimal, Useful)

rails activerecord auditing
by codesnips 4 tabs
ruby
module Types
  class PostType < Types::BaseObject
    field :id, ID, null: false
    field :title, String, null: false
    field :body, String, null: false
    field :excerpt, String, null: true

GraphQL API with graphql-ruby gem

rails graphql api
by Alex Kumar 2 tabs
ruby
class AddCommentsCountToPosts < ActiveRecord::Migration[6.1]
  def up
    add_column :posts, :comments_count, :integer, default: 0, null: false

    # Populate existing counts
    Post.find_each do |post|

Counter cache for association counts

rails performance activerecord
by Alex Kumar 2 tabs
ruby
# Basic middleware structure
class RequestTimerMiddleware
  def initialize(app)
    @app = app
  end

Rack middleware for request/response processing

ruby rails rack
by Sarah Mitchell 2 tabs
erb
<%# Fragment caching - caches rendered HTML %>
<% cache @product do %>
  <h1><%= @product.name %></h1>
  <p><%= @product.description %></p>
  <p>Price: $<%= @product.price %></p>
<% end %>

Rails caching strategies for performance

ruby rails caching
by Sarah Mitchell 4 tabs