Skip to content

Instantly share code, notes, and snippets.

@hannesl
hannesl / gist:22523ac60a6a5f632115
Created April 9, 2015 10:56
Another take on opening external links in a new window.
$(function() {
// Get the current domain name without any sub domains.
var internalDomain = (function () {
var domainParts = [],
hostnameParts = location.hostname.split("."),
i;
if (hostnameParts.length == 1) {
return location.hostname;
@hannesl
hannesl / field.tpl.php
Created March 4, 2015 21:02
Drupal: bring some sanity to the field markup.
<?php if (!empty($items) && !empty($items[0]) && !empty($items[0]['#markup'])): ?>
<div class="<?php print $classes; ?>"<?php print $attributes; ?>>
<?php if ($multiple): ?>
<?php if (!$label_hidden && $label) : ?>
<h3 class="field-label"<?php print $title_attributes; ?>><?php print $label ?></h3>
<?php endif; ?>
<ul class="field-items">
<?php foreach ($items as $item) : ?>
<li class="field-item"><?php print render($item); ?></li>
@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');
}