Skip to content

Instantly share code, notes, and snippets.

View andrewmp1's full-sized avatar

Drew Purdy andrewmp1

View GitHub Profile
@BDQ
BDQ / Import
Created December 3, 2008 21:37
namespace :spree do
desc "Export Products to CSV File"
task :export_products => :environment do
require 'fastercsv'
products = Product.find(:all)
puts "Exporting to #{RAILS_ROOT}/products.csv"
FasterCSV.open("#{RAILS_ROOT}/products.csv", "w") do |csv|
csv << ["id", "name", "description","sku", "master_price" ]
// ----------------------------------------------------------
// A short snippet for detecting versions of IE in JavaScript
// without resorting to user-agent sniffing
// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
// ie === undefined
// If you're in IE (>=5) then you can determine which version:
// ie === 7; // IE7
// Thus, to detect IE:
// if (ie) {}
@dhh
dhh / gist:1014971
Created June 8, 2011 18:09
Use concerns to keep your models manageable
# autoload concerns
module YourApp
class Application < Rails::Application
config.autoload_paths += %W(
#{config.root}/app/controllers/concerns
#{config.root}/app/models/concerns
)
end
end
@madhums
madhums / mapreduce.js
Created August 3, 2011 20:33
mapreduce using mongodb and mongoose
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/db_name');
// map function
var map = function(){
emit(this.field_to_group_by, {
other_fields: this.other_fields
// list other fields like above to select them
})
}
@mindscratch
mindscratch / mongoid_id_monkey_patch.rb
Created August 15, 2011 09:24
Mongoid: return 'id' instead of '_id' when serializing documents as JSON
module Mongoid
module Document
def as_json(options={})
attrs = super options
attrs["id"] = attrs.delete "_id" if attrs.has_key? "_id"
attrs
end
end
end
@gkop
gkop / gist:1371962
Created November 17, 2011 00:13
Capture javascript errors in Cucumber+Capybara+Webdriver tests
# in features/support/env.rb
require 'selenium/webdriver'
# we need a firefox extension to start intercepting javascript errors before the page
# scripts load
Capybara.register_driver :selenium do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
# see https://github.com/mguillem/JSErrorCollector
profile.add_extension File.join(Rails.root, "features/support/extensions/JSErrorCollector.xpi")
Capybara::Selenium::Driver.new app, :profile => profile
@stu-smith
stu-smith / form-utility.js
Last active October 1, 2015 20:07
Simple utility methods to copy Backbone models to and from forms
define([
'jQuery',
'Underscore'
], function ($, _) {
'use strict';
return {
clearForm: function (f) {
@ahawkins
ahawkins / deploy.rb
Created March 29, 2012 13:59
Deploy script for Heroku apps
#!/usr/bin/env ruby
# This is a basic deploy script for Heroku apps.
# It provides a structure you can use to expand on
# and add your own prereqs and deploy tasks.
#
# It basically ensures that:
# 1. There are no uncommited files
# 2. You can ssh to github
# 3. You can connect to heroku
@wolph
wolph / to_json.sql
Created April 6, 2012 10:35
Some functions to convert arrays/hstore to json :)
CREATE OR REPLACE FUNCTION escape_json (text) RETURNS text AS $$
SELECT replace($1, '''', '\'''); $$ LANGUAGE SQL IMMUTABLE;
CREATE OR REPLACE FUNCTION to_json(text) RETURNS text AS $$
SELECT escape_json($1) $$ LANGUAGE SQL IMMUTABLE;
CREATE OR REPLACE FUNCTION to_json(KEY text, value text) RETURNS text AS $$
SELECT '''' || to_json($1) || ''': ''' || to_json($2) || ''''; $$ LANGUAGE SQL IMMUTABLE;
@BDQ
BDQ / order_decorator.rb
Created November 9, 2012 17:09
Spree API patches to allow 'checkout'.
#order model decorator
Order.class_eval do
accepts_nested_attributes_for :line_items, :allow_destroy => true
def self.build_from_api(user, params)
order = create
params[:line_items_attributes].each do |line_item|
order.add_variant(Spree::Variant.find(line_item[:variant_id]), line_item[:quantity])
end