module StatementTimeout
extend ActiveSupport::Concern
class TimeoutExceeded < StandardError; end
def with_statement_timeout(milliseconds)
module DangerousAction
extend ActiveSupport::Concern
FRESH_WINDOW = 10.minutes
class ConfirmationMismatch < StandardError; end
require "loofah"
class HtmlSanitizer
ALLOWED_TAGS = %w[p br a strong em ul ol li blockquote code pre h2 h3].freeze
ALLOWED_ATTRS = %w[href title].freeze
SAFE_SCHEMES = %w[http https mailto].freeze
production:
primary:
adapter: postgresql
database: app_production
username: app
password: <%= ENV["PRIMARY_DB_PASSWORD"] %>
class OrderCreationService
Result = Struct.new(:success?, :order, :error, keyword_init: true)
def initialize(customer:, line_params:)
@customer = customer
@line_params = line_params
class AddSoftDeleteToDocuments < ActiveRecord::Migration[7.0]
disable_ddl_transaction!
def change
add_column :documents, :marked_for_deletion_at, :datetime, null: true
class Comment < ApplicationRecord
belongs_to :post
belongs_to :author, class_name: "User"
validates :body, presence: true, length: { maximum: 5_000 }
class CreateAccountsAndLedger < ActiveRecord::Migration[7.1]
def change
create_table :accounts do |t|
t.string :name, null: false
t.string :currency, null: false, default: "USD"
t.bigint :balance_cents, null: false, default: 0
class CreateIdempotencyKeys < ActiveRecord::Migration[7.1]
def change
create_table :idempotency_keys do |t|
t.string :key, null: false
t.string :request_path, null: false
t.datetime :locked_at
module ConnectionHealth
extend ActiveSupport::Concern
def with_fresh_connection
conn = ActiveRecord::Base.connection
conn.verify! # pings and reconnects if the socket is dead
class Order < ApplicationRecord
has_many :line_items, dependent: :destroy, inverse_of: :order
accepts_nested_attributes_for :line_items,
reject_if: :all_blank,
allow_destroy: true
class Cart < ApplicationRecord
TTL = 30.minutes
has_many :line_items, dependent: :destroy
enum status: { active: 0, expired: 1, checked_out: 2 }