Skip to content

Instantly share code, notes, and snippets.

View dcorking's full-sized avatar

David Corking dcorking

View GitHub Profile
@dcorking
dcorking / detect.js
Created August 10, 2021 10:21
detect global JS feature
// from https://github.com/github/fetch/blob/master/fetch.js
// detecting the existince of fetch() and only patching if it is absent
var global =
(typeof globalThis !== 'undefined' && globalThis) ||
(typeof self !== 'undefined' && self) ||
(typeof global !== 'undefined' && global) ||
{}
@dcorking
dcorking / is_it_overdue.sql
Created May 28, 2021 07:17
In MySQL: a task is to be done every 12 months. Is it due?
SELECT DATE_ADD('2020-05-18', INTERVAL 12 MONTH) > NOW()
@dcorking
dcorking / pivot_with_case.sql
Last active May 25, 2021 20:44
SQL pivot tables without the CROSSTAB function, just for fun
SELECT c.name,
MAX(CASE detail WHEN 'good' THEN quantity ELSE 0 END) AS good,
MAX(CASE detail WHEN 'ok' THEN quantity ELSE 0 END) AS ok,
MAX(CASE detail WHEN 'bad' THEN quantity ELSE 0 END) AS bad
FROM
(
SELECT name, detail, count(*) as quantity
FROM products p
JOIN details d ON p.id = d.product_id
GROUP BY name, detail
@dcorking
dcorking / authentication_spec.rb
Created January 9, 2020 17:20
testing a doorkeeper token request via a password grant
require 'rails_helper'
RSpec.describe '/oauth/', type: :request do
let(:application) { create(:doorkeeper_application, confidential: false) }
let(:user) { create(:user, email: 'joe@example.com', password: "PASSWORD") }
let(:valid_params) do
{
username: user.email,
password: "PASSWORD",
grant_type: 'password',
@dcorking
dcorking / demo_option_parser.rb
Created December 23, 2019 12:26
lightweight-ish option parsing with Ruby standard library
# run with
# ruby tools/demo-option-parser.rb --skip-download
# or
# bundle exec rails runner tools/demo-option-parser.rb --skip-download
require 'optparse' # not needed for rails runner
skip = nil
p ARGV # ["--skip-download"]
@dcorking
dcorking / footer_helper.rb
Last active November 15, 2019 17:00
year, or range of years with en dash if needed - for example for copyright messages
@dcorking
dcorking / hide-hnq-everywhere.css
Created March 6, 2019 14:48
Hide Hot Network Questions everywhere (to aid concentration)
/* for use as a user style, for example in a browser extension like Stylus for Google Chrome */
#hot-network-questions {
display: none
}
@dcorking
dcorking / code.sql
Last active November 22, 2018 16:36 — forked from alicethewhale/code.rb
SELECT timeline_items.* FROM (
# Query 1
SELECT lead_alerts.id AS id, lead_alerts.action AS body, users.first_name AS first_name, users.last_name AS last_name, lead_alerts.due_on AS date, "lead_alerts" AS model
FROM `lead_alerts` LEFT OUTER JOIN `users` ON `users`.`id` = `lead_alerts`.`author_id`
WHERE `lead_alerts`.`lead_id` = 1
UNION ALL
# Query 2
SELECT completed_lead_alerts.id AS id, lead_alerts.action AS body, users.first_name AS first_name, users.last_name AS last_name, completed_lead_alerts.completed_on AS date, "completed_lead_alerts" AS model
@dcorking
dcorking / console.log
Created July 12, 2017 09:56
error from oo7
traverseAllChildren.js:143 Uncaught TypeError: Cannot convert a Symbol value to a string
at String (<anonymous>)
at traverseAllChildrenImpl (traverseAllChildren.js:143)
at traverseAllChildrenImpl (traverseAllChildren.js:93)
at traverseAllChildren (traverseAllChildren.js:172)
at Object.instantiateChildren (ReactChildReconciler.js:70)
at ReactDOMComponent._reconcilerInstantiateChildren (ReactMultiChild.js:185)
at ReactDOMComponent.mountChildren (ReactMultiChild.js:224)
at ReactDOMComponent._createInitialChildren (ReactDOMComponent.js:703)
at ReactDOMComponent.mountComponent (ReactDOMComponent.js:522)
@dcorking
dcorking / rails_helper.rb
Last active May 17, 2017 07:58
rails_helper or spec_helper snippet to trace the clock before and after each rspec example
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
# ...
RSpec.configure do |config|
# Can help you spot problems with system time, timezones and time travel,
# especially if multiple specs interact with each other
# Check this doc for the difference between ::now and ::current :
# http://api.rubyonrails.org/classes/ActiveSupport/Testing/TimeHelpers.html#method-i-travel_to
config.around(:each) do |example|
puts "Time.now: #{Time.now}"