Skip to content

Instantly share code, notes, and snippets.

View rmm5t's full-sized avatar
🌐
building itsy bitsy parts of the internet that grow

Ryan McGeary rmm5t

🌐
building itsy bitsy parts of the internet that grow
View GitHub Profile
@rmm5t
rmm5t / wheel-group-to-sudoers.sh
Last active February 1, 2019 16:58
Add wheel group to sudoers on macOS
# Add current user to wheel group
sudo dseditgroup -o edit -a $USER -t user wheel
# Add wheel to sudoers
sudo su -
cd /etc
chmod 660 sudoers
vi /etc/sudoers
: <<'END_COMMENT'
@rmm5t
rmm5t / date_time_formats.rb
Last active February 1, 2019 17:05
Example of config/initializers/date_time_formats.rb
Time::DATE_FORMATS[:long_date] = Date::DATE_FORMATS[:long_date] = "%b %e, %Y"
Time::DATE_FORMATS[:day] = Date::DATE_FORMATS[:day] = "%a, %e %b %Y"
Time::DATE_FORMATS[:local_time] = Date::DATE_FORMATS[:local_time] = "%I:%M%P %Z"
Time::DATE_FORMATS[:human] = Date::DATE_FORMATS[:human] = lambda { |t| t.strftime("%b %e, %Y %I:%M%P").sub(/0(\d):/, '\1:') } # Oct 2, 2011 5:57pm
Time::DATE_FORMATS[:rfc822] = "%e %b %Y" # Already in Date::DATE_FORMATS
@rmm5t
rmm5t / seeds.rb
Last active February 1, 2019 17:04
Example of idempotent seeds:
# Author: Ryan McGeary : http://github.com/rmm5t
# License: MIT : https://rmm5t.mit-license.org/
def restart_sequence(klass)
seq = [klass.table_name, "id", "seq"].join("_")
next_id = klass.maximum(:id) + 1
klass.connection.execute "ALTER SEQUENCE #{seq} RESTART #{next_id}"
end
def load_seed_data(klass, records)
# Author: Ryan McGeary : http://github.com/rmm5t
# License: MIT : https://rmm5t.mit-license.org/
#
# TL;DR: A poor-man's full-text search.
#
# This module employs a mechanism by which we can easily add left outer joins
# to queries (typically for full-text-like searches that need to dig into
# specific associations).
#
# One way to do this is to just use ActiveRecord's `eager_load` as part of a
@rmm5t
rmm5t / promisify-node4.js
Created August 15, 2017 01:32
Node.js "promisify" implementations
const promisify = (fn) => {
return function () {
const args = Array.from(arguments);
return new Promise(function (resolve, reject) {
fn.apply(undefined, args.concat([function (error, data) {
if (error) return reject(error);
if (data === undefined) return resolve();
return resolve(data);
}]));
});
@rmm5t
rmm5t / OUTPUT.md
Last active February 19, 2021 21:18
How to properly introduce a new counter_cache to an existing Rails project.

Fast/efficient approach:

-- execute("UPDATE posts SET comments_count = (SELECT count(1) FROM comments WHERE comments.post_id = posts.id)")
   -> 1.3197s

Slow/naïve approach:

require 'benchmark'
require 'time'
n = 1_000_000
t = Time.new.utc
Benchmark.bmbm do |x|
x.report("unix epoch") { n.times { t.to_i.to_s } }
x.report("iso 8601 ") { n.times { t.iso8601 } }
end
@rmm5t
rmm5t / keybase.md
Created June 3, 2015 16:42
keybase.md

Keybase proof

I hereby claim:

  • I am rmm5t on github.
  • I am rmm5t (https://keybase.io/rmm5t) on keybase.
  • I have a public key whose fingerprint is 79DA 34B8 A2E7 3C96 E0CC 5BB2 005A C77D 7CBF 3EAF

To claim this, I am signing this object:

@rmm5t
rmm5t / bob.rb
Last active April 9, 2020 02:06
class Bob
def respond_to(input)
@input = input.strip
return "Fine. Be that way!" if silence?
return "Whoa, chill out!" if shouting?
return "Sure." if question?
"Whatever."
end