Skip to content

Instantly share code, notes, and snippets.

View rmw's full-sized avatar

Rebecca Miller-Webster rmw

View GitHub Profile
@rmw
rmw / single_table_inheritance.rb
Created August 30, 2011 21:21
Single Table Inheritance module
module SingleTableInheritance
def self.included(klass)
klass.extend ClassMethods
end
def class_type=(value)
self[:type] = value
end
@rmw
rmw / validates_polymorphic.rb
Created September 29, 2011 20:37
Validate a polymorphic association for a new record
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
#use these instead of validates_presence_of :addressable
validates_presence_of :addressable_type
validates_presence_of :addressable_id, :unless => Proc.new { |a|
#if it's a new record and addressable is nil and addressable_type is set
# then try to find the addressable object in the ObjectSpace
# if the addressable object exists, then we're valid;
# if not, let validates_presence_of do it's thing
@rmw
rmw / spec.js
Created September 30, 2011 02:59
Stubbing Backbone.js's fetch method with Jasmine & Sinon.js
beforeEach(function() {
var self = this;
//contacts
this.contacts = new App.Collections.Contacts();
_.each(this.fixtures.Contacts.valid, function(c) {
self.contacts.add(new Contact(c));
});
this.contactsCollectionStub =
sinon.stub(App.Collections, 'Contacts')
.returns(this.contacts);
@rmw
rmw / gist:1406893
Created November 29, 2011 22:31
What is the primary difference between these two pieces of code?
// Java
public boolean isEmpty(String s) {
return s.length() == 0;
}
# ruby
def empty?(s)
return s.size == 0
end
@rmw
rmw / backbone-secondary-sort.js
Created December 10, 2011 07:45
Backbone.js secondary sort order with collection comparator example
App.Collections.Notes = Backbone.Collection.extend({
model: Note,
url: '/notes.json',
comparator : function(note) {
//ordering by status (alphabetically asc) and id (numeric desc)
var stat = note.get('status');
var note_id = note.get('id');
var sort_order = (stat.charCodeAt(0) * 100000) - note_id;
return sort_order;
}
@rmw
rmw / sort_hash_by_string.rb
Created March 12, 2012 03:10
Sort a hash by a string
my_hash.sort { |h| h["name"].upcase }
@rmw
rmw / hash.rb
Created May 16, 2012 13:50
Extend Ruby Hash with method to return a recursive OpenStruct
class Hash
# options:
# :exclude => [keys] - keys need to be symbols
def to_ostruct_recursive(options = {})
convert_to_ostruct_recursive(self, options)
end
private
def convert_to_ostruct_recursive(obj, options)
result = obj
@rmw
rmw / hash.rb
Created May 16, 2012 14:04
Extend Ruby Hash with a method to return all keys as symbols
class Hash
def with_sym_keys
self.inject({}) { |memo, (k,v)| memo[k.to_sym] = v; memo }
end
end
@rmw
rmw / rspec_tap.rb
Created June 18, 2012 18:32
Using tap to DRY up Rspec
def cache_should_receive_fetch(return_val = nil)
Rails.cache.should_receive(:fetch).
with(cache_key, { :race_condition_ttl => 10}).
tap { |o| o.and_return(return_val) unless return_val.nil? }
end
@rmw
rmw / default_image.js
Created June 18, 2012 19:49
Showing a default image if an image doesn't exist in jQuery
var img = $('img');
var default_url = "http://example.org/images/img.jpg";
var img_url = "http://example.org/images/" + some_variable + ".jpg";
img.error(function() {
$(this).attr('src', default_url)
});
img.attr('src', img_url);