ruby

ruby
module CursorPagination
  extend ActiveSupport::Concern

  private

  def paginate_with_cursor(scope, per_page: 20)

Pagination with cursor-based approach

rails api pagination
by Alex Kumar 1 tab
ruby
class AdminController < ApplicationController
  before_action :authenticate_admin!
  before_action :disable_turbo_cache, only: %i[dashboard moderation_queue]

  helper_method :turbo_cache_control_tag

Turbo Drive: disable caching on volatile admin pages

rails turbo hotwire
by codesnips 3 tabs
ruby
class Article < ApplicationRecord
  scope :ranked, -> { order(score: :desc, id: :desc) }

  scope :after_cursor, ->(cursor) do
    return all if cursor.nil?

Deterministic Sorting with Secondary Key

rails activerecord pagination
by codesnips 4 tabs
ruby
class AddIndexesToPosts < ActiveRecord::Migration[6.1]
  def change
    add_index :posts, :author_id
    add_index :posts, :published_at
    add_index :posts, [:author_id, :published_at]
    add_index :posts, :created_at, order: { created_at: :desc }

Database indexes for query optimization

rails postgresql database
by Alex Kumar 1 tab
sql
-- Product.active.touch_all(:cache_synced_at) for catalog 42
UPDATE "products"
SET "updated_at" = '2024-05-01 12:00:00.123456',
    "cache_synced_at" = '2024-05-01 12:00:00.123456'
WHERE "products"."catalog_id" = 42
  AND "products"."status" = 'active';

Use `touch_all` for Efficient “Bump Updated At”

rails activerecord performance
by codesnips 4 tabs
ruby
module KeysetPageable
  extend ActiveSupport::Concern

  included do
    scope :keyset_page, ->(cursor: nil, per: 20) do
      per = per.to_i.clamp(1, 100)

Safe Pagination with Keyset (No OFFSET)

rails activerecord performance
by codesnips 3 tabs
ruby
class Document < ApplicationRecord
  belongs_to :account

  validates :source_url, presence: true

  before_save :normalize_checksum

Prevent Long Transactions with after_save_commit for Heavy Work

rails activerecord background-jobs
by codesnips 3 tabs
ruby
class Order < ApplicationRecord
  belongs_to :customer

  scope :for_export, -> {
    select(:id, :reference, :total_cents, :currency, :created_at, :customer_id)
      .where.not(exported_at: nil)

Avoid Memory Blowups: find_each + select Columns

rails activerecord performance
by codesnips 3 tabs
ruby
module ConditionalGet
  extend ActiveSupport::Concern

  private

  def render_conditional(resource, extra: nil)

ETag + Conditional GET for JSON API

rails performance http-caching
by codesnips 2 tabs
ruby
class Contract
  INVALID = Object.new.freeze

  COERCERS = {
    string: ->(v) { v.is_a?(String) ? v : v.to_s },
    integer: ->(v) { Integer(v.to_s) rescue INVALID },

Validated JSON Schema with dry-validation-style contract (lightweight)

api validation contracts
by codesnips 4 tabs
erb
<%= form_with model: @document, data: { controller: "upload" } do |form| %>
  <div class="field">
    <%= form.label :file, "Attach a document" %>
    <%= form.file_field :file,
          direct_upload: true,
          data: { upload_target: "input" } %>

Active Storage direct upload progress with Stimulus

rails hotwire stimulus
by codesnips 3 tabs
ruby
class CreateApiKeys < ActiveRecord::Migration[6.1]
  def change
    create_table :api_keys do |t|
      t.references :user, null: false, foreign_key: true
      t.string :name, null: false
      t.string :key_digest, null: false

API key authentication for service-to-service calls

rails authentication api
by Alex Kumar 3 tabs