Skip to content

Instantly share code, notes, and snippets.

version = 3.months.ago.utc.strftime("%Y%m%d%H%M%S")
puts "Deleting migrations before version #{version.inspect}"
migrations =
ActiveRecord::MigrationContext.new(['db/migrate'])
.migrations
.take_while { |migration| migration.version < version }
File.delete(*migrations.map(&:filename))
@matthieuprat
matthieuprat / keybase.md
Created January 25, 2019 13:12
Keybase proof

Keybase proof

I hereby claim:

  • I am matthieuprat on github.
  • I am mpr (https://keybase.io/mpr) on keybase.
  • I have a public key ASAm9UMZTlRgNLRP5p2rUbP9uK9N_-ld7JRI4eGdQYHbYwo

To claim this, I am signing this object:

@matthieuprat
matthieuprat / _kata.rb
Last active August 19, 2020 17:54
KataPotter
# http://codingdojo.org/kata/Potter/
UNIT_PRICE = 8
DISCOUNTS = [0, 0.05, 0.1, 0.2, 0.25]
def price(books)
# Count the number of occurences of each book
counts = books.group_by(&:itself).values.map(&:size)
raise if counts.size > DISCOUNTS.size
@matthieuprat
matthieuprat / bad.rb
Last active December 20, 2017 15:23
Wait for AJAX
def wait_for_ajax(timeout: Capybara.default_max_wait_time)
Timeout.timeout(timeout) do # Bad.
loop until finished_all_ajax_requests?
end
end
def finished_all_ajax_requests?
!page.evaluate_script '(window.jQuery || {}).active'
end
@matthieuprat
matthieuprat / object-extensions.js
Created July 19, 2017 08:49
Getting read of Immutable.js smoothly
/* eslint-disable no-extend-native */
// Cf https://facebook.github.io/immutable-js/docs/#/Map.
function notifySentry(method) {
const error = new Error(`Warning: '${method}' was called on an Object`)
Raven.captureException(error, { level: 'warning', tags: { objectUnknownMethod: method } })
}
const DEFAULTS = {
configurable: true,
Rx.Observable.prototype.debug = function(name = 'default') {
const s = (window.__subscriptions = window.__subscriptions || {}) // Subscriptions by name.
s[name] = s[name] || [] // Subscriptions. Useful to track memory leaks.
let i = -1
let j = 0
return Rx.Observable.create(observer => {
i += 1 // Subscription index.
j += 1 // Number of live subscriptions.
const log = (...args) => console.log(`[${name}][${i}] %c%s`, ...args)
log('color: blue; font-weight: bold', 'subscribe', `(${j} live)`)
@matthieuprat
matthieuprat / unfuck-encoding.rb
Last active June 28, 2017 13:41
Dealing with Excel encoding issues
string.chars.map.with_index do |c, i|
n = c.encode('windows-1252') rescue nil
n ||= c.encode('iso-8859-1') rescue nil
puts "#{i}: #{c}" unless n
n.bytes.first
end
.pack('C*')
.force_encoding('utf-8')
@matthieuprat
matthieuprat / playground.js
Last active March 9, 2017 11:13
Enumerability and ownership of Symbol properties
const s = Symbol()
const o = { [s]: 1 }
o.hasOwnProperty(s)
//-> true
Object.getOwnPropertyNames(o)
//-> []
Object.getOwnPropertySymbols(o)
@matthieuprat
matthieuprat / reactor.patch
Last active January 24, 2017 17:38
Patch for puma reactor
--- reactor.rb
+++ reactor.rb
@@ -165,16 +165,18 @@
def calculate_sleep
if @timeouts.empty?
@sleep_for = DefaultSleepFor
else
diff = @timeouts.first.timeout_at.to_f - Time.now.to_f
if diff < 0.0
#! /bin/bash
PREV_HEAD=$1
HEAD=$2
BRANCH_CHECKOUT=$3
# Skip the hook if the checkout is not a branch checkout.
[ "$BRANCH_CHECKOUT" = 1 ] || exit 0
# Tells wether a file has been modified between 'PREV_HEAD' and 'HEAD'.