Skip to content

Instantly share code, notes, and snippets.

@hannesl
hannesl / close_comments.php
Created January 27, 2015 09:42
Drupal: Close comments after a specified time.
<?php
$breakpoint = time() - 3 * 30 * 24 * 60 * 60;
$q = db_select('node');
$q->addField('node', 'nid');
$q->addField('node', 'vid');
$q->condition('comment', 2);
$q->condition('type', 'article');
$q->condition('created', $breakpoint, '<');
@hannesl
hannesl / format_number.js
Created January 22, 2015 00:49
Format a numeric value with spaces between every third digit.
function formatNumber(value) {
var input = value.toString(),
inputParts = input.split('.'),
int = inputParts[0],
decimals = inputParts.length > 1 ? '.' + inputParts[1] : '',
output = [];
while (int.length > 3) {
output.unshift(int.substr(-3));
int = int.substr(0, int.length - 3);
@hannesl
hannesl / application_controller.rb
Created August 21, 2014 09:52
Use temporary (302) redirects to force ssl in Ruby on Rails, rather than the standard configuration approach which uses permanent (301) redirects. This is useful when you may want to switch to a non-ssl environment later.
class ApplicationController < ActionController::Base
...
# Redirect all requests to SSL. We use this rather than config.force_ssl since
# we want to use temporary rather than permanent redirects.
if Rails.env.production?
before_filter do |controller|
controller.force_ssl_redirect({:status => :found}) # 302: Moved temporarily
end
@hannesl
hannesl / gaTrackEvent.js
Last active August 25, 2021 17:04
Track an event in Google analytics using the legacy or Universal version.
/**
* Track an event in Google Analytics (legacy or Universal).
*
* @param category
* The event category, e.g. "Download".
* @param action
* The event action, e.g. "PDF".
* @param label
* The event label, e.g. "Myfile.pdf".
* @param value
@hannesl
hannesl / xhprof.rb
Created February 13, 2014 12:28
A Homebrew formula for installing the latest Xhprof from Github.
require 'formula'
class Xhprof <Formula
url 'https://github.com/facebook/xhprof.git'
homepage 'http://mirror.facebook.net/facebook/xhprof/doc.html'
version 'master'
depends_on 'pcre'
def install
@hannesl
hannesl / cantor_pairing.php
Created December 18, 2013 23:02
Cantor pairing functions in PHP. Pass any two positive integers and get a unique integer back. Feed the unique integer back into the reverse function and get the original integers back. Explanation and JS implementation here: http://stevegardner.net/2012/07/09/javascript-cantor-pairing-function-and-reverse-function/
<?php
/**
* Calculate a unique integer based on two integers (cantor pairing).
*/
function cantor_pair_calculate($x, $y) {
return (($x + $y) * ($x + $y + 1)) / 2 + $y;
}
/**
@hannesl
hannesl / application.rb
Last active December 23, 2015 06:49
Use English in Rails admin even though you're using a different default locale for the app.
...
# Use Swedish as default locale.
config.i18n.default_locale = :sv
...
@hannesl
hannesl / mymodule.module
Created May 29, 2013 08:49
Drupal messages testing/styling spree.
function mymodule_init() {
drupal_set_message('A test message.', 'status');
drupal_set_message(str_repeat('Another message. ', 10), 'status');
drupal_set_message('A test warning.', 'warning');
drupal_set_message('A test error!!!', 'error');
}
@hannesl
hannesl / gist:4953693
Created February 14, 2013 15:52
A Drupal 7 helper function that takes a taxonomy term name and returns a corresponding term ID – even if it has to create the term first. Also supports adding Parent -> Child terms.
/**
* Helper function to link term names to taxonomy terms, and create new terms
* on-the-fly when necessary.
*
* @param $name
* - The name of the taxonomy term.
* @param $vocabulary_name
* - The machine name of the vocabulary where the term should be stored.
* @param $parent_name
* - Optionally the name of the parent term.
@hannesl
hannesl / gist:3983323
Created October 30, 2012 21:56
A couple of Ruby functions for transliterating file names or URLs.
# Remove whitespace and special characters and replace with dashes (-).
def self.filename(input)
input.split(%r{ |!|/|:|&|-|$|,|\?|%|\(|\)}).map { |i| i.downcase if i != '' }.compact.join('-')
end
# Convert to ascii and replace non-ascii letters.
def self.transliterate(input)
replacements = {
'å' => 'a',
'ä' => 'a',