Skip to content

Instantly share code, notes, and snippets.

@ddemaree
ddemaree / application.js.coffee
Created June 12, 2011 21:54
Simple Google Maps API v3 integration in CoffeeScript, with non-JS Static Maps fallback
#= require jquery
#= require jquery_ujs
#= require lib/modernizr
#= require lib/jquery.lettering
#= require_tree .
$ ->
$('*[data-googlemap]').googleMap()
true
@ddemaree
ddemaree / text-balancer.ts
Created April 3, 2023 13:24
Simple text balancing class written in TypeScript, complete with React hook
/*
Balances text blocks, making them as narrow as possible while maintaining their current height (i.e. number of lines), preventing typographic 'widows' and
'orphans' (single words on a line by themselves).
As of early 2023, native browser support for text balancing is planned but not yet implemented. See https://developer.mozilla.org/en-US/docs/Web/CSS/text-wrap
This feature can be previewed in Chrome 114+ by enabling the `Experimental Web Platform features` flag in `chrome://flags`.
For browsers that don't yet support `text-wrap`, this script uses a binary search to find the narrowest width that maintains the current height, based on
@ddemaree
ddemaree / schema.js
Last active November 9, 2022 00:59
Example of WordPress–Sanity import
// First, we must import the schema creator
import createSchema from "part:@sanity/base/schema-creator";
// Then import schema types from any plugins that might expose them
import schemaTypes from "all:part:@sanity/base/schema-type";
// We import object and document schemas
import blockContent from "./blockContent";
import category from "./category";
import post from "./post";
@ddemaree
ddemaree / no-more-masters.md
Last active February 4, 2022 16:14
Draft blog post for renaming Git master branches

No More Masters

In my book Git for Humans, published in 2016, I made copious references to master as the primary branch name used in Git repositories.

The term master can refer to a "master copy," meaning the original or canonical version of something from which other copies are made. But its primary meaning in the English language is "a person who has general authority over others." And, especially in American and British English, it's hard to separate the word "master," and that meaning of it, from another related word: "slave."

If we're being honest, folks in tech have known that "master" and "slave" are problematic terms for a while, even if they haven't felt motivated to change them. There are those who say that if these words refer to objects or systems, rather than people, then they can't be offensive, just as people argue that tech is a meritocracy and algorithms can't be biased. These argument

@ddemaree
ddemaree / _retina.scss
Created April 26, 2013 20:49
Example Sass mixin for a "bulletproof" Hi-DPI media query
@mixin retina($ratio: 1.5) {
$dpi: $ratio * 96;
$opera-ratio: $ratio * 100;
@media only screen and (-webkit-min-device-pixel-ratio: #{$ratio}),
only screen and ( -o-min-device-pixel-ratio: '#{$opera-ratio}/100'),
only screen and ( min-resolution: #{$dpi}dpi),
only screen and ( min-resolution: #{$ratio}dppx) {
@content;
}
@ddemaree
ddemaree / 01_ticket.rb
Created May 30, 2011 20:58
I cannot believe this is valid Ruby syntax. P.S. - I love Ruby
class Ticket < ActiveRecord::Base
SEARCH_COLUMNS = {
:tickets => [:token, :subject, :body, :customer_name, :customer_email],
:notes => [:body, :author_name, :author_email]
}
def self.conditions_for_keyword_search(table, keywords)
# Because we may use this string elsewhere, we don't want to mutate the only copy
query_words = keywords.dup
@ddemaree
ddemaree / 01_random_specs.rake
Created June 8, 2011 21:54
Rake task for running a random sampling of RSpec tests
namespace :spec do
SPEC_SUITES = %w(models requests integration helpers controllers lib)
def sample_spec_files(num=5)
[].tap do |specs|
SPEC_SUITES.each do |dirname|
files = Dir[Rails.root.join("spec", dirname, "**", "*_spec.rb")]
specs.concat files.shuffle.first(num)
end.shuffle
end
@ddemaree
ddemaree / 01_README.md
Created November 30, 2011 05:23
How Sunspot implements its wonderful search/index DSL

This code is extracted/adapted from Mat Brown's Sunspot gem. One of Sunspot's nicest features is an expressive DSL for defining search indexes and performing queries. It works by instance_eval-ing a block you pass into it in the context of its own search builder object. In this code, the pig thing1 statement is roughly equivalent to zoo = Zoo.new; zoo.pig(thing1).

Sunspot's DSL has to resort to trickery: the instance_eval_with_context method uses eval to get the block to give up the object it considers to be self, then sets up an elaborate system of delegates and method_missing calls so any methods not handled by the DSL are forwarded to the surrounding object. But as a result, this syntax is minimal and beautiful, and it works the way you expect whether or not you prefer blocks to yield an object.

Without this trick the block would be restricted to either the original, "calling" context (as a closure) or the DSL's "receiving" context (using instance_eval), but not both.

Using `ins

@ddemaree
ddemaree / talk_notes.md
Last active December 26, 2015 18:09
Notes and links for my Web Directions South 2013 talk

Slides

You can view or download my slides from Speaker Deck: The Weight of the Web (Web Directions South).

Referenced in my talk

@ddemaree
ddemaree / application_controller.rb
Created September 6, 2012 15:56
Sample code for WCR2012 lightning talk
class ApplicationController < ActionController::Base
def current_user
User.find_by_id(session[:user_id])
end
def current_user=(user)
session[:user_id] = user.id
end