Skip to content

Instantly share code, notes, and snippets.

@jorgemanrubia
jorgemanrubia / harmony_test.rb
Created Mar 22, 2010
Accesing javascript from ruby using harmony
View harmony_test.rb
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 Apr 15, 2010
rails 3 ujs driver for jquery - patch for JQuery 1.4.2 and aborted ajax requests
View rails 3 ujs driver patch for jquery 1.4.2.diff
--- 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
Code examples from my post 'Testing PURE javascript templates from RSpec'
View ViewHelpers.rb
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 Nov 22, 2010
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)
View gist:710465
jasmine.create$SpyObj = function(name, listOfSpyMethods) {
listOfSpyMethods = listOfSpyMethods || ['unbind', 'remove'];
return jQuery.extend($('<div/>'), jasmine.createSpyObj(name, listOfSpyMethods));
};
@jorgemanrubia
jorgemanrubia / gist:714101
Created Nov 24, 2010
JQuery selector for currently focused element
View gist:714101
$.expr[':'].focus = function(a){
return (a == document.activeElement);
}
$(".:focus"); // The currently focused element
@jorgemanrubia
jorgemanrubia / gist:729345
Created Dec 5, 2010
Jasmine matcher that checks the class of an object and some expected set of properties
View gist:729345
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
Definition of helper methods for simulating requests with raw content (raw_post parameter) (RSpec)
View gist:977786
# 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 Aug 2, 2011
Script to delete all the git tags from a repository (both local and remote). Take care, because it really does what it says :)
View delete_git_tags.rb
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 Nov 16, 2011
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.
View gist:1370586
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);
View jquery.delayedkeyup.js
(function( $ ){
$.fn.delayedKeyup = function(period, handler) {
var delayedHandler = (function() {
var timer = 0;
return function(callback, miliSeconds) {
clearTimeout(timer);
timer = setTimeout(callback, miliSeconds);
}
})();