Skip to content

Instantly share code, notes, and snippets.


Dan Adams mrdanadams

View GitHub Profile
mrdanadams / .buildpacks
Created June 22, 2013 01:34
Adding Geos and Proj4 support to Heroku using buildpacks
View .buildpacks
mrdanadams /
Created May 2, 2013 13:05
Conditionally loading jQuery / Zepto.js in Rails 3
#= require jquery
#= require application.js
mrdanadams / some_spec.rb
Created March 30, 2013 02:38
Triggering custom events with capybara-webkit using jQuery $.trigger
View some_spec.rb
def trigger(selector, name, options={})
# if you return the default object capybara tries to parse the jquery object
page.evaluate_script("$('#{selector}').trigger(jQuery.Event('#{name}', #{options.to_json})); undefined")
it 'triggers a couple events' do
# Ex: lets simulate a drag and drop
trigger '.canvas', :mousedown, {pageX:200, pageY:300}
trigger '.canvas', :mousemove, {pageX:400, pageY:400}
trigger '.canvas', :mouseup
mrdanadams / _pems.scss
Created March 29, 2012 13:32
PX to EMs conversion in Sass
View _pems.scss
/* See */
/* Default font size in pixels if not overridden. */
$baseFontSize: 16;
/* Convert PX units to EMs.
Ex: margin-right: pem(16);
@function pem($pxval, $base: $baseFontSize) {
@return #{$pxval / $base}em;
mrdanadams / controller_spec.rb
Created March 28, 2012 21:45
Testing MongoDB with Mongoid, RSpec, and Rails
View controller_spec.rb
# do our update
@controller.record_votes(winner._id.to_s, loser._id.to_s)
# will this pass or fail? who knows!?
Item.count(conditions:{ votes:1 }).should == 0
mrdanadams / controller.rb
Created March 28, 2012 21:40
db.eval() and server-side updates on MongoDB with the Ruby driver
View controller.rb
# Records the vote against 2 items.
# Both parameters are strings
def record_votes(winner_id, loser_id)
# grab the connection from the underlying driver
db = Mongoid::Config::master
# a future improvement would be to store this away somewhere, perhaps as a constant
# note: javascript comments added for the post: they are not in the real code
f = """
function(winner_id, loser_id) {
mrdanadams / Gemfile
Created March 28, 2012 21:31
Using Sunspot, Websolr, and Solr on Heroku
View Gemfile
gem 'sunspot_rails'
gem 'kaminari'
gem 'sunspot_with_kaminari'
mrdanadams / controller.rb
Created March 28, 2012 20:55
Boosting search results for Facebook friends with fb_graph, omniauth, Sunspot and Solr
View controller.rb
ids = current_user.facebook_friends if current_user
@search = Profile.solr_search do
keywords params[:q] do
boost_fields name: 10.0, user: 4.0
unless ids.blank?
adjust_solr_params do |params|
# See "bq (boost query)" in
params[:bq] = "facebook_id_s:(#{ids.join(' OR ')})^10"
mrdanadams / application.js
Created March 28, 2012 20:41
Updating a DIV with partials after remote AJAX form submit in Rails 3
View application.js
$(function() {
/* Convenience for forms or links that return HTML from a remote ajax call.
The returned markup will be inserted into the element id specified.
$('form[data-update-target]').live('ajax:success', function(evt, data) {
var target = $(this).data('update-target');
$('#' + target).html(data);
mrdanadams / scale.scss
Created January 30, 2012 15:11
scale Sass mixin to convert pixels to EMs
View scale.scss
@mixin scale($props, $sizes, $base: 16) {
$values: ();
$sublists: false;
@each $s in $sizes {
/* unwrap lists for values that have multiple list of values such as text-shadow */
@if type-of($s) == list {
$sublists: true;
$vv: ();
@each $ss in $s {
$vv: append($vv, if(type-of($ss) == number, #{$ss / $base}em, $ss));