module KeysetPaginatable
extend ActiveSupport::Concern
class_methods do
def keyset_page(cursor: nil, limit: 20, direction: :desc)
limit = limit.to_i.clamp(1, 100)
Rails.application.config.middleware.insert_before(
Rack::Runtime,
Rack::Timeout,
service_timeout: 15 # 15 seconds
)
require 'securerandom'
require 'digest'
require 'openssl'
require 'rack/utils'
class RememberToken < Sequel::Model
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
class CreateWebhookEvents < ActiveRecord::Migration[7.1]
def change
create_table :webhook_events do |t|
t.string :event_id, null: false
t.string :source, null: false, default: "stripe"
t.string :event_type, null: false
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
module MyApp
class Application < Rails::Application
config.load_defaults 6.1
# Enable response compression
config.middleware.use Rack::Deflater
class SyncContactJob < ApplicationJob
queue_as :external
BACKOFF = ->(executions) do
(2**executions) + rand(0.0..1.0) # exponential + jitter, in seconds
end
class PopularPostsQuery
def initialize(relation = Post.all)
@relation = relation
end
def call(timeframe: 7.days, min_views: 100, limit: 20)
class User < ApplicationRecord
has_one_attached :avatar do |attachable|
attachable.variant :thumb,
resize_to_limit: [256, 256],
convert: :webp,
saver: { quality: 80 }
module Authorizable
extend ActiveSupport::Concern
class NotAuthorized < StandardError; end
included do
class TrendingPostsService
CACHE_KEY = 'trending_posts:v1'.freeze
CACHE_TTL = 15.minutes
def self.call(limit: 10)
Rails.cache.fetch(CACHE_KEY, expires_in: CACHE_TTL) do