ruby

ruby
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

Redis caching for expensive computations

rails redis caching
by Alex Kumar 1 tab
ruby
module TurboResponder
  extend ActiveSupport::Concern

  def respond_turbo_or_html(success_redirect:, invalid_template:)
    respond_to do |format|
      format.turbo_stream

A reusable respond_to block for turbo_stream + html

rails hotwire turbo
by Henry Kim 2 tabs
ruby
class CreateAuditLogs < ActiveRecord::Migration[6.1]
  def change
    create_table :audit_logs do |t|
      t.references :user, null: true, foreign_key: true
      t.string :action, null: false
      t.string :resource_type

Audit logging for sensitive operations

rails security audit-logging
by Alex Kumar 3 tabs
ruby
class ProcessPaymentWorker
  include Sidekiq::Worker

  sidekiq_options queue: :critical, retry: 10

  sidekiq_retry_in do |count, exception|

Background job retry strategies

rails sidekiq background-jobs
by Alex Kumar 1 tab
erb
<%= turbo_stream_from [current_member, :notifications] %>

<div id="notification_badge">
  <%= render 'notifications/badge', count: current_member.notifications.unread.count %>
</div>

Notifications dropdown that live-updates with Turbo Streams

rails hotwire turbo
by Henry Kim 2 tabs
ruby
class JwtService
  ACCESS_TOKEN_LIFETIME = 15.minutes
  REFRESH_TOKEN_LIFETIME = 7.days
  SECRET = Rails.application.credentials.jwt_secret

  def self.encode_access_token(user_id)

JWT authentication with refresh tokens

rails authentication jwt
by Alex Kumar 1 tab
ruby
class ApplicationController < ActionController::Base
  helper_method :turbo_native_app?

  private

  def turbo_native_app?

Turbo Native bridge for mobile hybrid apps

hotwire turbo-native mobile
by Jordan Lee 3 tabs
ruby
class BillingController < ApplicationController
  before_action :authenticate_member!

  def show
    turbo_cache_control :no_cache
    response.headers['Cache-Control'] = 'no-store'

Avoid caching sensitive pages in Turbo Drive

rails hotwire turbo
by Henry Kim 2 tabs
erb
<%= turbo_frame_tag "sidebar" do %>
  <nav>
    <%= link_to "All", snips_path, data: { turbo_frame: "sidebar" } %>
    <%= link_to "Trending", snips_path(filter: :trending), data: { turbo_frame: "sidebar" } %>
    <%= link_to "Editors' choice", snips_path(filter: :editors_choice), data: { turbo_frame: "sidebar" } %>
  </nav>

Turbo Frames: scoped navigation inside a sidebar

rails turbo hotwire
by Sarah Chen 2 tabs
ruby
Rails.application.config.filter_parameters += [
  :password,
  :password_confirmation,
  :token,
  :authorization,
  :ssn,

Sanitizing logs so secrets and PII do not leak downstream

logging pii redaction
by Kai Nakamura 1 tab
ruby
class DeliveryObserver
  def self.delivered_email(message)
    new(message).record
  end

  def initialize(message)

Action Mailer Delivery Observability Hook

rails observability action-mailer
by codesnips 3 tabs
ruby
require 'ipaddr'
require 'resolv'

uri = URI.parse(params[:url])
allowed_hosts = %w[images.example-cdn.com api.partner.com]

SSRF mitigation with URL allowlists and egress controls

ssrf network-security secure-coding
by Kai Nakamura 1 tab