Skip to content

Instantly share code, notes, and snippets.

View toddsiegel's full-sized avatar

Todd Siegel toddsiegel

  • Solera Software, Inc.
  • Denver, CO
  • 00:57 (UTC -06:00)
View GitHub Profile
@toddsiegel
toddsiegel / gist:9be4ba387c30674a3ce733d5f6cef379
Created August 9, 2024 19:58
One file ActiveRecord script
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'sqlite3', '~> 1.4'
gem 'activerecord', '7.1.3.4'
end
require 'sqlite3'
@toddsiegel
toddsiegel / Elasticsearch Cheatsheet.md
Last active February 9, 2024 16:53
Elasticsearch Cheatsheet

Introspection

List all indicies

The v query params shows the heading in the response

GET /_cat/indices?v

List indicies with wildcard

GET /_cat/indices/profiles*?v
@toddsiegel
toddsiegel / histogram.sql
Last active February 5, 2024 19:00
PostgreSQL Histogram
-- this groups by 5 to keep the length down
select length(username) as length,
count(*) as count,
repeat('*'::text, (COUNT(length(username))::int / 5) + 1) AS histogram
from users
where username is not null
group by length(username)
order by length;
@toddsiegel
toddsiegel / introspection.sql
Last active December 29, 2023 21:35
Helpful PostgreSQL Queries
-- List all Foreign Keys
select kcu.table_schema || '.' ||kcu.table_name as foreign_table,
'>-' as rel,
rel_tco.table_schema || '.' || rel_tco.table_name as primary_table,
string_agg(kcu.column_name, ', ') as fk_columns,
kcu.constraint_name
from information_schema.table_constraints tco
join information_schema.key_column_usage kcu
on tco.constraint_schema = kcu.constraint_schema
and tco.constraint_name = kcu.constraint_name
@toddsiegel
toddsiegel / gist:7474971
Last active August 18, 2023 21:59
Dependecy Injection
class Payment
def process(gateway)
gateway.verify(self) # duck typed.
end
end
class PaymentTest
def test_process
payment = Payment.new
payment.process(MockGatway.new)
@toddsiegel
toddsiegel / capybara.md
Last active January 11, 2022 16:08
Capybara Cheatsheet

Forms

Fill in inputs by name

# e.g.
# <table>
#   <tbody>
#     <tr>
#      <td><input name="foo" type="text" /></td>
#      <td><input name="bar" type="text" /></td>
# 
@toddsiegel
toddsiegel / cookbook.md
Created January 4, 2022 23:18
Ruby Cookbook

CSVs

Generate a CSV

csv = CSV.generate(headers: true, force_quotes: true) do |row|
  row << ['ID', 'Name',  'Email']
  people.each do |person|
    row << [person.id, person.name, person.email]
  end
end
csv = "#{csv}" # Add BOM if people are going open this in Excel
@toddsiegel
toddsiegel / generator.rb
Created September 30, 2021 22:42
Rails Generators: Turn off I don't use
# config/initialers/generators.rb
# This config will not generate the following:
# - per model view helpers, or stylesheets
# - scaffold.scss
# - jbuilder templates, which I only occasionally use
# - controller tests, which I don't really use. I do system tests instead.
Rails.application.config.generators do |g|
@toddsiegel
toddsiegel / aws.md
Last active August 18, 2021 20:30
Cheatsheets

Get object count

$ aws s3 ls s3://<bucketname> --recursive | wc -l

@toddsiegel
toddsiegel / uses_tempfile.rb
Created July 23, 2021 19:28
Ruby and Rails Utiilities
# frozen_string_literal: true
module UsesTempfile
def with_tempfile(name: 'temp-zip-file', temp_dir: Rails.root.join('tmp').to_s, binmode: false)
tempfile = Tempfile.new('temp-zip-file', temp_dir)
tempfile.binmode if binmode
yield tempfile
ensure
tempfile.close