Skip to content

Instantly share code, notes, and snippets.

@alfie-max
alfie-max / list_tables.rb
Created August 26, 2022 14:15 — forked from davidleandro/list_tables.rb
Rails list all tables count
tables = []
ActiveRecord::Base.connection.tables.each do |t|
count = ActiveRecord::Base.connection.exec_query("select count(*) from #{t}").rows[0][0]
tables << [t, count.to_i]
end
tables.sort_by { |t| t[1] }.reverse!
@alfie-max
alfie-max / rails-upgrade.rb
Created May 12, 2022 15:18 — forked from andynu/example.sh
rails-upgrade script, a helper for moving pins and doing other checks during ruby upgrades
#!/usr/bin/env ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'thor'
gem 'rainbow'
gem 'awesome_print'
gem 'terminal-table'
gem 'terminal-emojify'
/**
* This Google Sheets script keeps data in the specified column sorted any time
* the data changes.
*
* After much research, there wasn't an easy way to automatically keep a column
* sorted in Google Sheets, and creating a second sheet to act as a "view" to
* my primary one in order to achieve that was not an option. Instead, I
* created a script that watches for when a cell is edited and triggers
* an auto sort.
*
@alfie-max
alfie-max / rake_benchmark.rb
Created September 7, 2020 12:25 — forked from harley/rake_benchmark.rb
Benchmarking a rake task
# Put this in Rakefile (doesn't matter where)
require 'benchmark'
class Rake::Task
def execute_with_benchmark(*args)
bm = Benchmark.measure { execute_without_benchmark(*args) }
puts " #{name} --> #{bm}"
end
alias_method :execute_without_benchmark, :execute
@alfie-max
alfie-max / deploy.rb
Created May 28, 2019 01:01 — forked from gilcierweb/deploy.rb
Mina deploy ruby on rails 5.1 ++
require 'mina/rails'
require 'mina/git'
require 'mina/rbenv' # for rbenv support. (https://rbenv.org)
# require 'mina/rvm' # for rvm support. (https://rvm.io)
set :application_name, 'my_app'
set :domain, 'my_server'
set :deploy_to, '/home/rails/my_app'
set :repository, 'git@bitbucket.org:my_user/my_app.git'
set :branch, 'master'
@alfie-max
alfie-max / gist:82d0083f9d78837f3771d9971b435713
Created January 14, 2017 10:19 — forked from shripadk/gist:552554
Setting up Heroku Hostname SSL with GoDaddy SSL Cert
How to setup Heroku Hostname SSL with GoDaddy SSL Certificate and Zerigo DNS
Heroku recently added an exciting new 'Hostname SSL' option. This option offers the broad compatibility of IP-based SSL, but at 1/5 the price ($20 / month at the time of this writing).
The following tutorial explains how to use Heroku's new 'Hostname SSL' option on your Heroku project. Before we begin, let's list what we're using here:
* Heroku Hostname SSL
* GoDaddy Standard SSL Certificate
* Zerigo DNS
@alfie-max
alfie-max / gist:5c28c8f282064b653911a9ab8ea25c81
Created September 22, 2016 09:24 — forked from 1v/gist:04901ed17202cddfe42d
God and Puma configuration

God and Puma configuration

Add to Gemfile:

gem 'puma'
gem 'god'

Run in app folder:

bundle install
@alfie-max
alfie-max / gist:9369511921deedddb31b634878d94a19
Created August 24, 2016 11:20 — forked from cpjolicoeur/gist:3590737
Ordering a query result set by an arbitrary list in PostgreSQL

I'm hunting for the best solution on how to handle keeping large sets of DB records "sorted" in a performant manner.

Problem Description

Most of us have work on projects at some point where we have needed to have ordered lists of objects. Whether it be a to-do list sorted by priority, or a list of documents that a user can sort in whatever order they want.

A traditional approach for this on a Rails project is to use something like the acts_as_list gem, or something similar. These systems typically add some sort of "postion" or "sort order" column to each record, which is then used when querying out the records in a traditional order by position SQL query.

This approach seems to work fine for smaller datasets, but can be hard to manage on large data sets with hundreds (or thousands) of records needing to be sorted. Changing the sort position of even a single object will require updating every single record in the database that is in the same sort group. This requires potentially thousands of wri

@alfie-max
alfie-max / Count Code lines
Created August 4, 2016 10:30 — forked from amitchhajer/Count Code lines
Count number of code lines in git repository per user
git ls-files -z | xargs -0n1 git blame -w | perl -n -e '/^.*\((.*?)\s*[\d]{4}/; print $1,"\n"' | sort -f | uniq -c | sort -n