ruby

ruby
event_id = request.headers.fetch('X-Event-Id')
timestamp = request.headers.fetch('X-Signature-Timestamp').to_i

raise ActionController::BadRequest, 'stale request' if Time.now.to_i - timestamp > 300
raise ActionController::BadRequest, 'replay detected' if WebhookEvent.exists?(external_id: event_id)

Secure webhook endpoint design with replay protection

webhooks replay-protection hmac
by Kai Nakamura 1 tab
ruby
class SlugValidator < ActiveModel::Validator
  SLUG_REGEX = /\A[a-z0-9]+(?:-[a-z0-9]+)*\z/

  def validate(record)
    slug = record.send(options[:attribute] || :slug)

Rails validators for custom business logic

rails validations activemodel
by Maya Patel 2 tabs
erb
<%= link_to project.name, project_path(project), data: { turbo_preload: true }, class: 'font-medium hover:underline' %>

Speed up perceived performance with Turbo preload links

rails hotwire turbo
by Henry Kim 2 tabs
ruby
raw_token = SecureRandom.urlsafe_base64(32)
token_digest = Digest::SHA256.hexdigest(raw_token)

PasswordReset.create!(
  user: user,
  token_digest: token_digest,

Secure random token generation for sessions and recovery flows

randomness tokens authentication
by Kai Nakamura 1 tab
ruby
class CreateTopSnipsMaterializedView < ActiveRecord::Migration[6.1]
  def change
    execute <<~SQL
      CREATE MATERIALIZED VIEW top_snips AS
      SELECT id, title, score
      FROM snips

Cache-Friendly “Top N” with Materialized View Refresh

rails postgres performance
by Sarah Chen 2 tabs
erb
<%= link_to "Show audit", audit_project_path(@project), data: { turbo_frame: "audit" }, class: "btn" %>
<%= turbo_frame_tag "audit" do %>
  <p class="text-sm text-gray-500">Audit will load here.</p>
<% end %>

Turbo Frames: “details” panel that lazy-loads on click

rails turbo hotwire
by Sarah Chen 2 tabs
ruby
class PostSerializer < ActiveModel::Serializer
  attributes :id, :title, :excerpt, :body, :published_at, :views, :likes_count, :comments_count

  attribute :can_edit, if: :current_user_can_edit?

  belongs_to :author, serializer: UserSummarySerializer

Serializers with ActiveModel::Serializers

rails api serialization
by Alex Kumar 2 tabs
ruby
class DeadLetter < ApplicationRecord
  validates :job_class, :payload, :error_class, presence: true
end

Background Job Dead Letter Queue (DLQ) Table

rails reliability background-jobs
by Sarah Chen 2 tabs
ruby
class Api::CollectionsController < ActionController::API
  def show
    collection = Collection.includes(snips: [:author, :code_blocks]).find(params[:id])
    render json: CollectionSerializer.new(collection).as_json
  end
end

N+1 Proof Serialization with preloaded associations

rails activerecord json
by Sarah Chen 2 tabs
ruby
class Analytics::TopAuthors
  def initialize(since: 30.days.ago)
    @since = since
  end

  def call(limit: 20)

Safe Raw SQL with exec_query + Binds

rails activerecord sql
by Sarah Chen 1 tab
ruby
class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.build(comment_params.merge(author: current_member))

    if @comment.save

Action Text comment form that works inside a Turbo Frame

rails hotwire turbo
by Henry Kim 2 tabs
erb
<%= turbo_frame_tag dom_id(user) do %>
  <tr>
    <td><%= user.name %></td>
    <td><%= user.role %></td>
    <td>
      <%= link_to 'Edit', edit_user_path(user), data: { turbo_frame: dom_id(user) } %>

Inline edit a table row with Turbo Frames

rails hotwire turbo
by Henry Kim 2 tabs