Skip to content

Instantly share code, notes, and snippets.

View glebm's full-sized avatar

Gleb Mazovetskiy glebm

View GitHub Profile
@glebm
glebm / README.md
Last active March 29, 2023 19:56
Rails Link Preload Headers

This lets you set the preload headers for your assets, so that the browser can start fetching them before it begins parsing HTML.

@glebm
glebm / README.md
Last active October 12, 2018 15:36
Universal Rails async onPageLoad script (Turbolink v2, v5.0, v5.1, jquery-turbolinks, and no Turbolinks)

This lets you load application JavaScript asynchronously from <head>. Compatible with Turbolink v2, v5.0, v5.1, jquery-turbolinks, and no Turbolinks. Compatible with all browsers and IE9+ (only IE10+ will actually benefit from this though).

How to use this:

  1. Add the HTML snippet just before the closing </body> tag.

  2. Add the on_page_load.js script to the beginning of your application.js, and use window.App.onPageLoad(...) instead of document.addEventListener('DOMContentLoaded', ...) and jQuery(($) -> ...) everywhere.

  3. Include your JavaScript like so:

    <head>
@glebm
glebm / primes.rs
Last active August 22, 2020 19:24
Rust: Erathosthenes prime sieve
fn is_prime(n: usize, primes: &Vec<usize>) -> bool {
for &p in primes {
let q = n / p;
if q < p { return true };
let r = n - q * p;
if r == 0 { return false };
}
panic!("too few primes")
}
@glebm
glebm / dependencies_logger.rb
Created November 20, 2016 07:50
A logger for ActiveSupport::Dependencies
# Place this file into lib/dependencies_logger.rb and require it from application.rb after Bundler.require as:
# require_relative '../lib/dependencies_logger'
require 'active_support/dependencies'
class DependenciesLogger
def initialize
@need_newline = false
@load_depth = 0
end
@glebm
glebm / console.log
Created August 12, 2016 09:24
Thredded #352 failure log
User tracking what they have and have not already read
D, [2016-08-12T09:16:45.975024 #5152] DEBUG -- :  (0.1ms) BEGIN
D, [2016-08-12T09:16:45.975303 #5152] DEBUG -- :  (0.2ms) COMMIT
D, [2016-08-12T09:16:45.975575 #5152] DEBUG -- :  (0.1ms) BEGIN
D, [2016-08-12T09:16:45.977178 #5152] DEBUG -- :  (0.2ms) SAVEPOINT active_record_1
D, [2016-08-12T09:16:45.978641 #5152] DEBUG -- : SQL (0.3ms) INSERT INTO "users" ("email", "name", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["email", "user220@example.com"], ["name", "Dr. Ellis O'Keefe"], ["created_at", "2016-08-12 09:16:45.977492"], ["updated_at", "2016-08-12 09:16:45.977492"]]
D, [2016-08-12T09:16:45.979315 #5152] DEBUG -- :  (0.2ms) RELEASE SAVEPOINT active_record_1
D, [2016-08-12T09:16:45.980593 #5152] DEBUG -- :  (0.1ms) SAVEPOINT active_record_1
D, [2016-08-12T09:16:45.982087 #5152] DEBUG -- : [1
@glebm
glebm / i18n_status.html.slim
Created June 22, 2014 18:06
Missing and unused translations report (i18n-tasks v0.5.0+)
h1 Missing and unused translations
- if @missing.present?
.panel.panel-default
.panel-heading: h3.panel-title #{@missing.leaves.count} missing keys
table.table.table-striped.table-condensed
thead: tr
th.text-right Locale
th Key
th Value
@glebm
glebm / object_enumerable.rb
Last active August 29, 2015 14:00
make all Ruby things Enumerable
class Object
def each(&block)
return to_enum(:each) { 1 } unless block
[self].each(&block)
end
include Enumerable
end
class NilClass
def each(&block)
@glebm
glebm / gist:9665022
Created March 20, 2014 14:32
mailgun dkim bug
Delivered-To: glex.spb@gmail.com
Received: by 10.52.103.9 with SMTP id fs9csp318329vdb;
Thu, 20 Mar 2014 07:31:14 -0700 (PDT)
X-Received: by 10.52.8.225 with SMTP id u1mr556372vda.64.1395325873937;
Thu, 20 Mar 2014 07:31:13 -0700 (PDT)
Return-Path: <bounce+f7da5c.ff052-glex.spb=gmail.com@itbyen.no>
Received: from mail-s77.mailgun.info (mail-s77.mailgun.info. [184.173.153.205])
by mx.google.com with ESMTP id of4si455498vcb.175.2014.03.20.07.31.09
for <glex.spb@gmail.com>;
@glebm
glebm / i18_status_controller.rb
Last active December 25, 2015 00:19
i18n-tasks html report example
require 'i18n/tasks/base_task'
...
def i18n_status
task = I18n::Tasks::BaseTask.new
@missing = task.missing_keys
@unused = task.unused_keys
end
...
@glebm
glebm / md-hl-code
Created September 9, 2013 20:50
Convert all markdown code blocks from indented to the syntax-highlighted ``` blocks
#!/usr/bin/env ruby
def convert(lang, md)
md.gsub(/\t/, ' ' * 4).gsub(/((?:^\s{4}[^\n]*\n)+)/) {
"\n```#{lang}#{$1.gsub(/^[ ]{4}/, '')}```\n"
}
end
argv = ARGV.dup
path = argv.pop or raise 'pass path'