Skip to content

Instantly share code, notes, and snippets.

@jorgemanrubia
jorgemanrubia / harmony_test.rb
Created March 22, 2010 19:11
Accesing javascript from ruby using harmony
require 'rubygems'
require 'harmony'
code = <<-eos
javascript_object = {
nombre: 'Jorge',
say: function(){
return "Hi there!"
}
};
@jorgemanrubia
jorgemanrubia / rails 3 ujs driver patch for jquery 1.4.2.diff
Created April 15, 2010 14:48
rails 3 ujs driver for jquery - patch for JQuery 1.4.2 and aborted ajax requests
--- rails.js
+++ (clipboard)
@@ -40,10 +40,7 @@
el.trigger('ajax:loading', xhr);
},
success: function (data, status, xhr) {
- if (xhr.status)
el.trigger('ajax:success', [data, status, xhr]);
- else
- el.trigger('ajax:failure', [xhr, "aborted", data]);
@jorgemanrubia
jorgemanrubia / ViewHelpers.rb
Created May 2, 2010 22:16
Code examples from my post 'Testing PURE javascript templates from RSpec'
module ViewHelpers
def render_javascript_template(data)
render
include_javascript_files
yield do_render_javascript_template(data)
end
def include_javascript_files
%w{jquery.js pure.js}.each {|file| js("load('public/javascripts/#{file}');")}
@jorgemanrubia
jorgemanrubia / gist:710465
Created November 22, 2010 19:16
Jasmine factory for creating spy objects with common JQuery methods. For the common need of a mock object that need to act as a JQuery object ($() requires a method `remove` and `unbind` to exist)
jasmine.create$SpyObj = function(name, listOfSpyMethods) {
listOfSpyMethods = listOfSpyMethods || ['unbind', 'remove'];
return jQuery.extend($('<div/>'), jasmine.createSpyObj(name, listOfSpyMethods));
};
@jorgemanrubia
jorgemanrubia / gist:714101
Created November 24, 2010 18:05
JQuery selector for currently focused element
$.expr[':'].focus = function(a){
return (a == document.activeElement);
}
$(".:focus"); // The currently focused element
@jorgemanrubia
jorgemanrubia / gist:729345
Created December 5, 2010 18:56
Jasmine matcher that checks the class of an object and some expected set of properties
toBeObjectWithProperties: function(expectedClass, expectedProperties) {
var isObjectWithProperties = (this.actual instanceof expectedClass);
for(var propertyName in expectedProperties){
var expectedPropertyValue = expectedProperties[propertyName];
isObjectWithProperties = isObjectWithProperties && (this.actual[propertyName] == expectedPropertyValue);
}
return isObjectWithProperties;
}
@jorgemanrubia
jorgemanrubia / gist:977786
Created May 18, 2011 00:46
Definition of helper methods for simulating requests with raw content (raw_post parameter) (RSpec)
# Methods like "put_raw" will put the raw content in the request
[:put, :post].each do |verb|
define_method "#{verb}_raw" do |method, content|
request.stub!(:raw_post).and_return(content)
put method
end
end
@jorgemanrubia
jorgemanrubia / delete_git_tags.rb
Created August 2, 2011 15:05
Script to delete all the git tags from a repository (both local and remote). Take care, because it really does what it says :)
def delete_local_tags
`git tag`.split(/\n/).each do |tag|
%x{git tag -d #{tag}}
end
end
def delete_remote_tags
`git ls-remote --tags origin`.split(/\n/).each do |remote_tag_line|
remote_tag_line =~ /\/([^\/]*)$/
%x{git push origin :refs/tags/#{$1}}
@jorgemanrubia
jorgemanrubia / gist:1370586
Created November 16, 2011 16:37
Prepare Jasmine's Pretty Printer for working with Backbone. Jasmine has serious problems for printing Backbone objects linked with other objects/Collections (e.g when an expectation fails). It enters in long loops leaving the browser totally frozen.
var addBackboneSupportToJasminePrettyPrinter = function() {
var oldPrettyPrinterFormat = jasmine.PrettyPrinter.prototype.format;
jasmine.PrettyPrinter.prototype.format = function(value) {
var self = this;
if(value instanceof Backbone.Model){
this.emitObject(value.attributes);
}
else if(value instanceof Backbone.Collection){
value.each(function(model) {
self.emitScalar(model.cid);
(function( $ ){
$.fn.delayedKeyup = function(period, handler) {
var delayedHandler = (function() {
var timer = 0;
return function(callback, miliSeconds) {
clearTimeout(timer);
timer = setTimeout(callback, miliSeconds);
}
})();