ruby

ruby
class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.new(comment_params.merge(author: current_member))

    if @comment.save

Turbo Streams: Create with prepend + HTML fallback

rails turbo hotwire
by Sarah Chen 2 tabs
ruby
# config/initializers/turbo_stream_actions.rb
Turbo::Streams::TagBuilder.class_eval do
  def flash(message:, level: :notice)
    update('flash', partial: 'shared/flash', locals: { message: message, level: level })
  end
end

Turbo Streams: custom action for flash messages

rails turbo hotwire
by Sarah Chen 2 tabs
ruby
class ButtonComponent < ViewComponent::Base
  VARIANTS = {
    primary: "bg-blue-600 hover:bg-blue-700 text-white",
    secondary: "bg-gray-200 hover:bg-gray-300 text-gray-900",
    danger: "bg-red-600 hover:bg-red-700 text-white"
  }.freeze

ViewComponent for reusable UI components

rails viewcomponent components
by Jordan Lee 3 tabs
ruby
# Gemfile
gem 'view_component'

# app/components/button_component.rb
class ButtonComponent < ViewComponent::Base
  VARIANTS = %w[primary secondary danger].freeze

ViewComponent for reusable, testable view components

ruby rails view-component
by Sarah Mitchell 2 tabs
ruby
class Webhooks::Verifier
  def initialize(secret: Rails.application.credentials.webhooks_secret)
    @secret = secret
  end

  def valid?(request)

Robust Webhook Verification (HMAC + Timestamp)

rails security webhooks
by Sarah Chen 2 tabs
ruby
class CreateComments < ActiveRecord::Migration[6.1]
  def change
    create_table :comments do |t|
      t.references :commentable, polymorphic: true, null: false
      t.references :author, null: false, foreign_key: { to_table: :users }
      t.text :body, null: false

Polymorphic associations for flexible relationships

rails activerecord database
by Alex Kumar 3 tabs
erb
<% if @project.errors.any? %>
  <%= turbo_stream.replace dom_id(@project, :form), partial: "projects/form", locals: { project: @project } %>
<% else %>
  <%= turbo_stream.replace dom_id(@project), partial: "projects/project", locals: { project: @project } %>
  <%= turbo_stream.update "flash", partial: "shared/flash" %>
<% end %>

Turbo Stream form errors: replace only the form frame

rails turbo hotwire
by Sarah Chen 2 tabs
ruby
class DashboardPresenter
  def initialize(member)
    @member = member
  end

  def unread_notifications_count

Hot Path Memoization (within request only)

rails performance
by Sarah Chen 1 tab
sql
UPDATE outbox_events
SET status = 1, locked_at = NOW()
WHERE id = (
  SELECT id
  FROM outbox_events
  WHERE status = 0

Atomic “Read + Mark Processed” with UPDATE … RETURNING

rails postgres concurrency
by Sarah Chen 2 tabs
ruby
class AddUniqueSlugToArticles < ActiveRecord::Migration[6.1]
  def change
    add_column :articles, :slug, :string, null: false
    add_index :articles, :slug, unique: true
  end
end

Database-Backed Unique Slugs with Retry

rails postgres slugs
by Sarah Chen 2 tabs
ruby
module Posts
  class CreateService
    def initialize(author:, params:)
      @author = author
      @params = params
    end

Rails service objects for business logic

rails architecture service-objects
by Maya Patel 2 tabs
ruby
# Map - transform elements
[1, 2, 3, 4].map { |n| n * 2 }          # => [2, 4, 6, 8]
['alice', 'bob'].map(&:upcase)          # => ['ALICE', 'BOB']
users.map(&:email)                       # => ['alice@example.com', ...]

# Select/Reject - filter elements

Enumerables and collection manipulation

ruby enumerable collections
by Sarah Mitchell 3 tabs