This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SingleTableInheritance | |
def self.included(klass) | |
klass.extend ClassMethods | |
end | |
def class_type=(value) | |
self[:type] = value | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Java | |
public boolean isEmpty(String s) { | |
return s.length() == 0; | |
} | |
# ruby | |
def empty?(s) | |
return s.size == 0 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_hash.sort { |h| h["name"].upcase } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hash | |
def with_sym_keys | |
self.inject({}) { |memo, (k,v)| memo[k.to_sym] = v; memo } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
OlderNewer