Skip to content

Instantly share code, notes, and snippets.

View bensheldon's full-sized avatar
🏊‍♂️
Swimming through code.

Ben Sheldon [he/him] bensheldon

🏊‍♂️
Swimming through code.
View GitHub Profile
# bin/rails-production
#!/usr/bin/env ruby
# Simulate a production environment locally
# for the purpose of profiling
ENV['RAILS_ENV'] = 'production'
ENV['NODE_ENV'] = 'production'
ENV['RACK_ENV'] = 'production'
diff --git a/app/controllers/account/registrations_controller.rb b/app/controllers/account/registrations_controller.rb
index 376f20a77..a554c9f8d 100644
--- a/app/controllers/account/registrations_controller.rb
+++ b/app/controllers/account/registrations_controller.rb
@@ -1,6 +1,8 @@
# frozen_string_literal: true
module Account
class RegistrationsController < Devise::RegistrationsController
+ before_action :reject_spam, only: :create
+
@bensheldon
bensheldon / add-geekbot-questions.js
Last active July 11, 2023 18:40 — forked from JoseInTheArena/add-geekbot-questions.js
Script for adding questions to a geekbot random question type
/**
* Disclaimer: This works as of the date of writing (7/11/2023) but there's no guarantees it will work in the future
* as it depends on selectors on geekbot's web UI, which may change at any time. Use at your own risk.
*
* To use this:
* 1. Navigate to your geekbot random question settings and click "Edit questions"
* so that you see a list of input fields and an "+ Add question" button
* 2. Open your devtools
* 3. Pasting this script into the console
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", '~>6.1'
end

Emails and SMS. Cousins or Sisters?

Many apps send both emails and SMS messages via Twilio.

My experience with GCF

Emails go out through Rails built-in Action Mailer construct

Raise your hand. Who here has worked with Rails Action Mailer?

ActionMailer:

@bensheldon
bensheldon / credential.rb
Created January 12, 2019 00:20
Example of using Postgres advisory locks with Rails ActiveRecord
class Credential < ApplicationRecord
CredentialNotAvailableError = Class.new(StandardError)
def self.find_and_lock_credential
where("pg_try_advisory_lock(to_regclass('#{table_name}')::int, id::int)").first
end
def self.find_and_lock_credential!
find_and_lock_credential || raise(CredentialNotAvailableError)
end
def check_accessibility(scroll_to_top: true)
return if Capybara.current_driver != Capybara.javascript_driver
expect(page).to be_accessible.skipping(
'color-contrast',
'landmark-one-main',
'region',
'radiogroup',
'label',
'heading-order',
)
# config/initializers/skylight.rb
Rails.application.config.skylight.environments += %w[staging]
instrumenter = Skylight.instrumenter
if instrumenter.present?
ApplicationJob.around_perform do |job, block|
title = "#{job.class.name}#perform"
queue = job.queue_name
segment = queue != 'default' ? "<sk-segment>#{queue}</sk-segment>" : ""
@bensheldon
bensheldon / postgres.rb
Last active August 27, 2018 14:27
A 'top-n per group' query (last N requests per city/request) suggestion https://twitter.com/postgresql/status/1033797250936389633
### ORIGINAL QUERY USING RANK()
PRODUCTION [9] pry(main)> City.all.includes(:service_list_status).explain(analyze: true)
D, [2018-08-27T01:47:18.119931 #4] DEBUG -- : City Load (1.6ms) SELECT "cities".* FROM "cities"
D, [2018-08-27T01:47:19.172424 #4] DEBUG -- : Status Load (1047.6ms) SELECT "statuses".* FROM "statuses" INNER JOIN (SELECT id, RANK() OVER(PARTITION BY city_id, request_name ORDER BY created_at DESC) rank
FROM statuses
) rankings ON rankings.id = statuses.id WHERE "statuses"."request_name" = $1 AND (rankings.rank <= 1) AND ("statuses"."city_id") IN ($2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26) [["request_name", "service_list"], ["city_id", 1], ["city_id", 2], ["city_id", 3], ["city_id", 6], ["city_id", 7], ["city_id", 8], ["city_id", 9], ["city_id", 10], ["city_id", 11], ["city_id", 12], ["city_id", 13], ["city_id", 14], ["city_id", 15], ["city_id", 16], ["city_id", 17], ["city_id", 18], ["city_id", 19], ["city_id",