Skip to content

Instantly share code, notes, and snippets.

@developish
developish / gist:200489
Created October 3, 2009 08:04
email-with-pony.rb
require "rubygems"
gem "hiroshi-pony"
require "pony"
Pony.mail({
:to => "brandon@developish.com",
:subject => "subject",
:body => "body",
:via => :smtp, :smtp => {
:host => 'smtp.gmail.com',
@developish
developish / example.rb
Created January 7, 2010 15:09
How to use searchlogic and geokit together
@zipcode = Zipcode.find_by_name("85023")
# Passing in a hash of search parameters is more convenient for my purposes,
# but searchlogic allows the second, chained method of searching also works.
@locations = Location.by_location(:origin => @zipcode, :within => 10).search(:name_like => "searchlogic", :city_is => "Phoenix")
@locations = Location.by_location(:origin => @zipcode, :within => 10).name_like("searchlogic").city_is("Phoenix")
@developish
developish / postal-code-search.rb
Created May 22, 2010 23:26
Simple postal code search using the Geonames web service
require 'rubygems'
require 'geonames' # ppe-ruby-geonames
class PostalCodeSearch
include Geonames
def initialize(postal_code, country_code = "US")
@postal_code = postal_code
@country_code = country_code
@response = nil
@developish
developish / user.rb
Created November 9, 2010 01:43
How to use authogic without requiring a password
class User < ActiveRecord::Base
acts_as_authentic do |config|
config.validate_password_field :if => :require_password_on_signup?
end
def require_password_on_signup?
false
end
end
@developish
developish / jquery.bindcheckbox.js
Created April 11, 2011 21:48
Simple way to bind the state of a checkbox to an associated function.
// Bind a checkbox to a with checked and unchecked callback functions
// and run the appropriate action on page load:
//
// Examples
//
// $("input[type=checkbox][name=thecheckbox]").bindCheckbox({
// checked: function() {
// alert("the checkbox is checked!")
// },
// unchecked: function() {
@developish
developish / jquery.appendfromtemplate.js
Created April 13, 2011 03:18
For adding nested form objects with Rails
// Works in conjunction with the generate_template helper to insert new
// objects into a form that has a collection of items.
$.fn.appendFromTemplate = function(template) {
return this.each(function () {
var newId = new Date().getTime();
$(this).append(template.replace(/NEW_RECORD/g, newId));
});
};
@developish
developish / gist:1059914
Created July 2, 2011 10:09
I thought the problem was Rails safe buffers
# Assuming that :something is trusted
Mustache.render("<h1>template</h1> {{ something }}", {:something => "<h2>Testing</h2>" })
# => <h1>template</h1> &lt;h2&gt;Testing&lt;/h2&gt;
# Desired out is "<h1>template</h1> <h2>Testing</h2>"
# Solution, use the triple mustache - {{{ }}} - for content that shouldn't be escapes
@developish
developish / post.rb
Created October 14, 2011 07:25
Test ActiveRecord #delete vs #destroy
class Post < ActiveRecord::Base
before_destroy :protect_from_destruction
def protect_from_destruction
if protected?
errors.add(:base, "Whoa this one is important")
false
end
end
@developish
developish / duration_spec.rb
Created October 15, 2011 02:55
How to create an isolated ActiveRecord connection for testing without breaking the rest of the suite.
require "active_record"
require "lib/duration"
class Event < ActiveRecord::Base
include Duration
end
describe Duration do
before do
ActiveRecord::Base.establish_connection(
@developish
developish / format-disk3.sh
Created January 9, 2013 22:47
How to destructively remove unnecessary "Boot OS X" record from a drive by completely reformatting
# where /dev/disk3 is the disk you want to reset
# GPT is the GUID partition table
# HFS+ is the Apple HFS format
diskutil partitionDisk /dev/disk3 GPT HFS+ newDiskName 100%