ruby

ruby
module Types
  class PostType < Types::BaseObject
    field :id, ID, null: false
    field :title, String, null: false
    field :body, String, null: false
    field :excerpt, String, null: true

GraphQL API with graphql-ruby gem

rails graphql api
by Alex Kumar 2 tabs
ruby
class AddCommentsCountToPosts < ActiveRecord::Migration[6.1]
  def up
    add_column :posts, :comments_count, :integer, default: 0, null: false

    # Populate existing counts
    Post.find_each do |post|

Counter cache for association counts

rails performance activerecord
by Alex Kumar 2 tabs
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
class DownloadsController < ApplicationController
  before_action :authenticate_user!

  def show
    report = current_account.reports.find(params[:report_id])
    authorize! :download, report

Signed, Expiring S3 Download URLs for Active Storage Attachments in Rails

rails active-storage s3
by codesnips 4 tabs
erb
<h1>Create your account</h1>

<%= render "error_summary", record: @user %>

<%= form_with model: @user, url: signups_path do |f| %>
  <div class="field">

Update an error summary area via turbo_stream.update

rails hotwire turbo
by codesnips 3 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
ruby
class CreateCustomerRevenueSummaries < ActiveRecord::Migration[7.0]
  def change
    create_view :customer_revenue_summaries, materialized: true, version: 1

    add_index :customer_revenue_summaries,
              :customer_id,

Database Views for Read Models

rails postgres performance
by codesnips 4 tabs
ruby
require "securerandom"
require "digest"

class SlidingWindowLimiter
  Result = Struct.new(:allowed, :count, :remaining)

Sliding-Window API Rate Limiting with Rack Middleware and Redis

ruby rack redis
by codesnips 4 tabs
erb
<div class="dashboard">
  <header class="dashboard__header">
    <h1>Overview</h1>
    <p class="muted">Real-time metrics update as they load.</p>
  </header>

Server-rendered skeleton UI for slow frames

rails hotwire turbo
by codesnips 4 tabs
ruby
class ArticlesController < ApplicationController
  before_action :set_article, only: %i[edit update]

  def new
    @article = Article.new
  end

Turbo-friendly 422 responses for invalid forms

rails hotwire turbo
by codesnips 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
ruby
class ReportsController < ApplicationController
  def monthly_sales
    year = params.fetch(:year, Date.current.year).to_i
    rows = Order.monthly_sales(year: year)
    @report = MonthlySalesReport.new(rows, year: year)

Aggregate Monthly Sales Reports in Rails with group_by SQL and a Presenter

rails postgres reporting
by codesnips 3 tabs