ruby

ruby
class PasswordResetsController < ApplicationController
  before_action :find_user_by_token, only: [:edit, :update]

  def create
    user = User.find_by(email: params[:email]&.downcase)

Secure password reset flow with signed tokens

rails security authentication
by Alex Kumar 1 tab
ruby
# app/policies/post_policy.rb
class PostPolicy < ApplicationPolicy
  class Scope < Scope
    def resolve
      if user.admin?
        scope.all

Pundit for authorization and policy objects

ruby rails pundit
by Sarah Mitchell 2 tabs
ruby
class UserSearchQuery
  def initialize(relation = User.all)
    @relation = relation
  end

  def call(params)

Query objects for complex database queries

ruby rails query-objects
by Sarah Mitchell 2 tabs
ruby
class Post < ApplicationRecord
  has_many :comments, dependent: :destroy

  # comments_count is maintained by counter_cache on Comment#belongs_to

  scope :stale_comment_counts, lambda {

Counter Cache Repair Job (Consistency Tooling)

rails activerecord counter-cache
by codesnips 3 tabs
ruby
class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.build(comment_params)
    @comment.author = current_user

Turbo Streams for real-time list updates

hotwire turbo turbo-streams
by Jordan Lee 2 tabs
ruby
class TransferFundsService
  def initialize(from_account:, to_account:, amount:)
    @from_account = from_account
    @to_account = to_account
    @amount = amount
  end

Database transactions for data consistency

rails database activerecord
by Alex Kumar 1 tab
ruby
# Gemfile
gem 'draper'

# app/decorators/user_decorator.rb
class UserDecorator < Draper::Decorator
  delegate_all

Decorator pattern with Draper for view logic

ruby rails draper
by Sarah Mitchell 2 tabs
ruby
class CacheNamespace
  attr_reader :name

  def initialize(name, store: Rails.cache)
    @name = name.to_s
    @store = store

Cache Key Versioning with a Single “namespace”

rails caching cache-invalidation
by codesnips 3 tabs
javascript
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["checkbox", "selectAll", "count", "submit"]
  static values = { selectedIds: Array }

Stimulus: bulk selection + Turbo batch action

rails stimulus hotwire
by codesnips 4 tabs
ruby
class AddConstraintsToUsers < ActiveRecord::Migration[6.1]
  def change
    # Null constraints
    change_column_null :users, :email, false
    change_column_null :users, :username, false

Database constraints for data integrity

rails postgresql database
by Alex Kumar 1 tab
ruby
require "sidekiq/api"

class QueueDepthGuard
  class QueueSaturated < StandardError
    attr_reader :queue, :depth

Background Job Backpressure with Queue Depth Guard

rails reliability sidekiq
by codesnips 3 tabs
erb
<div class="search-page">
  <div class="search-header mb-6">
    <h1 class="text-2xl font-bold mb-4">Search Posts</h1>

    <%= form_with url: posts_path,
        method: :get,

Live search with Turbo Frames and debouncing

hotwire turbo stimulus
by Jordan Lee 3 tabs