Skip to content

Instantly share code, notes, and snippets.

View tom-lord's full-sized avatar
🔥
🚒

Tom Lord tom-lord

🔥
🚒
View GitHub Profile
Limit (cost=0.11..4232.55 rows=1000 width=4)
-> Index Scan using lease_deals_pkey on lease_deals (cost=0.11..3130311.24 rows=739600 width=4)
Filter: ((expired_at IS NULL) AND (updated_at < '2018-10-09 09:26:33.507679'::timestamp without time zone) AND (dealership_id = 5678))
# Snippets taken from:
# https://github.com/rails/rails/blob/fc5dd0b85189811062c85520fd70de8389b55aeb/activerecord/lib/active_record/relation/batches.rb#L201
def in_batches(of: 1000, start: nil, finish: nil, load: false, error_on_ignore: nil)
relation = self
# ...
batch_limit = of
# ...
relation = relation.reorder(batch_order).limit(batch_limit)
relation = apply_limits(relation, start, finish)
class LeaseDeal < ApplicationRecord
def self.expire_all(time: Time.current, batch_size: 1000)
ids = pluck(:id)
ids.each_slice(batch_size) do |ids_slice|
unscoped.where(id: ids_slice).update_all(expired_at: time)
end
end
end
class AddOrderedDealershipIdExpiredAtIndexToLeaseDeals < ActiveRecord::Migration[5.2]
def change
remove_index :lease_deals, name: :index_lease_deals_on_dealership_id_and_expired_at, algorithm: :concurrently
add_index :lease_deals, %i[id dealership_id expired_at], where: 'expired_at IS NULL', using: :btree, order: { id: :asc }, algorithm: :concurrently
end
end
@tom-lord
tom-lord / rails_admin.de.yml
Created December 19, 2018 16:17 — forked from soemo/rails_admin.de.yml
German translation for RailsAdmin
# German rails_admin translation
de:
admin:
home:
name: "Home"
pagination:
previous: "&laquo; Vor"
next: "Zurück &raquo;"
truncate: "…"
@tom-lord
tom-lord / euler_square.rb
Created May 14, 2020 11:24
Find all unique solutions to the 4x4 playing cards puzzle
class Array
def no_duplicates?
tally.values.all? {|v| v == 1 }
end
end
class Card
attr_reader :picture, :suit
def initialize(picture, suit)
@tom-lord
tom-lord / thebutton.js
Last active December 22, 2020 18:12
A "nicely formatted" (readable) version of the javascript sourcecode at www.reddit.com/r/thebutton -- taken from http://www.redditstatic.com/reddit.en-gb.KaNkkPAPGl0.js
r.thebutton = {
_setTimer:function(e){
var t = "00000",
n = (e > 0 ? e : 0).toString(),
i = t.substring(0, t.length - n.length) + n;
for(var s = 0; s < 4; s++)
r.thebutton._timerTextNodes[s].nodeValue = i[s];
e%100 === 0 && r.thebutton._drawPie(e, 6e4)
},
_countdown:function(){
@tom-lord
tom-lord / active_emoji.rb
Created June 25, 2018 08:49
Samples from active_emoji.rb
class Array
alias ⏪ <<
alias 🈴 concat
alias 💧 drop
alias 🔁 each
alias 🈳❓ empty?
alias 🍀 sample
alias 🎲 sample
alias ♻️ shuffle
alias 👈 push