# app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
default from: 'noreply@example.com'
def welcome_email(user)
@user = user
# BAD: N+1 query problem
@users = User.all
@users.each do |user|
puts user.posts.count # Fires query for each user!
end
json.array! @posts do |post|
json.cache! ['v1', post], expires_in: 1.hour do
json.id post.id
json.title post.title
json.excerpt post.excerpt
json.published_at post.published_at
RegistrationSchema = Dry::Schema.Params do
required(:email).filled(:string, format?: URI::MailTo::EMAIL_REGEXP)
required(:password).filled(:string, min_size?: 12)
optional(:marketing_opt_in).filled(:bool)
optional(:country).filled(:string, included_in?: %w[US CA GB AU])
end
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_member
def connect
self.current_member = env['warden']&.user || reject_unauthorized_connection
class AddEnabledFlagIndexToAccounts < ActiveRecord::Migration[6.1]
def change
execute <<~SQL
CREATE INDEX index_accounts_on_beta_enabled
ON accounts ((settings->>'beta'))
WHERE (settings->>'beta') = 'true';
class Member < ApplicationRecord
before_validation :normalize_email
validates :email, presence: true, uniqueness: { case_sensitive: false }
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }
class CodeBlock < ApplicationRecord
belongs_to :snip, touch: true
end
class Comment < ApplicationRecord
belongs_to :post
belongs_to :author, class_name: 'Member'
after_create_commit -> { broadcast_prepend_later_to(post, target: dom_id(post, :comments)) }
end
# Installation
# rails active_storage:install
# rails db:migrate
# config/storage.yml
local:
<%# users can only subscribe to streams signed for them %>
<%= turbo_stream_from current_member.signed_stream_name %>
<%= form_with url: issues_path, method: :get, data: { turbo_frame: 'issue_results' } do |f| %>
<%= f.select :status, options_for_select(%w[open closed], params[:status]), include_blank: 'Any' %>
<%= f.submit 'Apply', class: 'ml-2 rounded bg-gray-100 px-3 py-1' %>
<% end %>
<%= turbo_frame_tag 'issue_results' do %>