Skip to content

Instantly share code, notes, and snippets.

Avatar

Dan Adams mrdanadams

View GitHub Profile
@mrdanadams
mrdanadams / .buildpacks
Created Jun 22, 2013
Adding Geos and Proj4 support to Heroku using buildpacks
View .buildpacks
https://github.com/peterkeen/heroku-buildpack-vendorbinaries.git
https://github.com/aaronrenner/heroku-buildpack-rgeo-prep.git
https://github.com/kr/heroku-buildpack-inline.git
https://github.com/heroku/heroku-buildpack-ruby.git
@mrdanadams
mrdanadams / application-ie.coffee
Created May 2, 2013
Conditionally loading jQuery / Zepto.js in Rails 3
View application-ie.coffee
#= require jquery
#= require application.js
@mrdanadams
mrdanadams / some_spec.rb
Created Mar 30, 2013
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")
end
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
mrdanadams / _pems.scss
Created Mar 29, 2012
PX to EMs conversion in Sass
View _pems.scss
/* See http://mrdanadams.com/2012/pixel-ems-css-conversion-sass-mixin/ */
/* 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
mrdanadams / controller_spec.rb
Created Mar 28, 2012
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
mrdanadams / controller.rb
Created Mar 28, 2012
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
mrdanadams / Gemfile
Created Mar 28, 2012
Using Sunspot, Websolr, and Solr on Heroku
View Gemfile
gem 'sunspot_rails'
gem 'kaminari'
gem 'sunspot_with_kaminari'
@mrdanadams
mrdanadams / controller.rb
Created Mar 28, 2012
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
end
unless ids.blank?
adjust_solr_params do |params|
# See "bq (boost query)" in http://wiki.apache.org/solr/DisMaxQParserPlugin
params[:bq] = "facebook_id_s:(#{ids.join(' OR ')})^10"
@mrdanadams
mrdanadams / application.js
Created Mar 28, 2012
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
mrdanadams / scale.scss
Created Jan 30, 2012
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));