Sarah Mitchell

33 code snips · on codesnips 5 months

Senior Ruby Developer with 10+ years of experience crafting elegant, performant Rails applications. Deep expertise in Ruby metaprogramming, Rails internals, and building...

ruby
# Basic middleware structure
class RequestTimerMiddleware
  def initialize(app)
    @app = app
  end

Rack middleware for request/response processing

ruby rails rack
by Sarah Mitchell 2 tabs
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
# app/channels/chat_channel.rb
class ChatChannel < ApplicationCable::Channel
  def subscribed
    # Subscribe to a specific room
    room = Room.find(params[:room_id])

ActionCable for real-time WebSocket communication

ruby rails actioncable
by Sarah Mitchell 3 tabs
ruby
# Controller responding with Turbo Stream
class PostsController < ApplicationController
  def create
    @post = Post.new(post_params)

    if @post.save

Hotwire Turbo for SPA-like user experiences

ruby rails hotwire
by Sarah Mitchell 3 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 UserRegistrationForm
  include ActiveModel::Model
  include ActiveModel::Attributes

  attribute :email, :string
  attribute :name, :string

Form objects for complex form handling

ruby rails form-objects
by Sarah Mitchell 2 tabs
ruby
# Create table migration
class CreateUsers < ActiveRecord::Migration[7.0]
  def change
    create_table :users do |t|
      t.string :email, null: false, index: { unique: true }
      t.string :name, null: false

Database migrations and schema management

ruby rails migrations
by Sarah Mitchell 2 tabs
ruby
# spec/factories/users.rb
FactoryBot.define do
  factory :user do
    sequence(:email) { |n| "user#{n}@example.com" }
    name { Faker::Name.name }
    password { 'password123' }

Factory Bot for flexible test data generation

ruby rspec factory-bot
by Sarah Mitchell 1 tab
ruby
# Basic exception handling
begin
  result = 10 / 0
rescue ZeroDivisionError => e
  puts "Error: #{e.message}"
  result = nil

Exception handling and error management

ruby exceptions error-handling
by Sarah Mitchell 3 tabs
ruby
# Basic matching
email = "user@example.com"
email =~ /@/  # => 4 (position of match)
email.match?(/@/)  # => true

# Capture groups

Regular expressions for pattern matching

ruby regex regular-expressions
by Sarah Mitchell 3 tabs
ruby
module Api
  module V1
    class UsersController < ApplicationController
      before_action :authenticate_user!, except: [:index, :show]
      before_action :set_user, only: [:show, :update, :destroy]

RESTful API design with Rails

ruby rails api
by Sarah Mitchell 4 tabs
erb
<%# Fragment caching - caches rendered HTML %>
<% cache @product do %>
  <h1><%= @product.name %></h1>
  <p><%= @product.description %></p>
  <p>Price: $<%= @product.price %></p>
<% end %>

Rails caching strategies for performance

ruby rails caching
by Sarah Mitchell 4 tabs