Skip to content

Instantly share code, notes, and snippets.

View diegocasmo's full-sized avatar
👨‍💻

Diego Castillo diegocasmo

👨‍💻
View GitHub Profile
@diegocasmo
diegocasmo / myAjaxFunction.js
Last active August 29, 2015 14:07
Javascript CORS Ajax request IE < 9 fallback.
myAjaxFunction: function(attributes, callback) {
if ($.browser.msie && window.XDomainRequest && parseInt($.browser.version) <= 9) {
var xdr = new XDomainRequest();
xdr.open('POST', 'URL', true);
xdr.send(attributes);
xdr.onload = function() {
callback(JSON.parse(xdr.responseText));
}
} else {
$.ajax({
@diegocasmo
diegocasmo / beautify_date.js
Last active June 7, 2016 15:36
Handlebars helper to transform a time stamp in ms to a nicely formatted "31 minutes ago", "1 hour ago" text.
/**
* Handlebars helper to transform a time stamp ms to a nicely
* formatted "31 minutes ago", "1 hour ago" type date.
*/
Handlebars.registerHelper('beautify_date', function(options) {
var timeAgo = new Date(parseInt(options.fn(this)));
if (Object.prototype.toString.call(timeAgo) === '[object Date]') {
if (isNaN(timeAgo.getTime())) {
return 'Not Valid';
} else {
@diegocasmo
diegocasmo / Database.php
Created October 28, 2014 17:35
Laravel Eloquent setup snippet for use outside of the framework.
<?php
/* Setup Eloquent. */
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule = new Capsule;
$capsule->addConnection([
"driver" => "mysql",
@diegocasmo
diegocasmo / Validator.js
Last active August 29, 2015 14:09
Dynamic validation helper.
/**
* Validator Class
*/
define([
'jquery',
'underscore'
], function($, _){
var Validator = {
@diegocasmo
diegocasmo / ImageHelper.js
Last active August 29, 2015 14:10
Helper object to preload images.
define([
'jquery',
'underscore'
], function ($, _) {
var ImageHelper = {
initialize: function () {
var example = [
"img/image1.jpg",
"img/image2.jpg"
@diegocasmo
diegocasmo / SingletonModel.js
Last active May 10, 2016 16:37
Defining a backbone model with the singleton pattern. Useful for models that represent things such as the "AppState".
define([
'underscore',
'backbone'
], function (_, Backbone) {
var Singleton = Backbone.Model.extend({
}, {
singleton: null,
@diegocasmo
diegocasmo / local_storage_size.js
Last active August 29, 2015 14:25
Calculates localStorage key and total size occupied by data in MB
var total = 0;
for (var key in localStorage) {
var keySize = ((localStorage[key].length * 2) / 1024 / 1024).toFixed(2);
console.log(key + '=' + keySize + ' MB');
total += parseFloat(keySize);
}
console.log('total=' + total.toFixed(2) + ' MB');
@diegocasmo
diegocasmo / array_paginator.js
Last active May 11, 2016 14:24
This a requireJS module helpful for paginating an array data structure. I have used this module for features like infinite scrolling and others.
// Helper service to paginate an array
// Usage:
// var bigArray = [1, 2, 3, ..., 1000],
// paginatedArray = new ArrayPaginator(bigArray);
// paginatedArray.getFirstPage() <-- will always return the first 10 elements in array
// paginatedArray.getNextPage() <-- will return next 10 elements in array of the next page
/*global define*/
define([], function() {
'use strict';
@diegocasmo
diegocasmo / create_order.rb
Last active September 28, 2016 01:17
An example scenario written in Gherkin.
Feature: Create an Order
Ubiqua allows salesmen to order products for their clients.
Scenario: Order Some Products
Given I am logged in as a salesman assigned to "Farmacia Moreno", with the following products:
| product_name | price |
| Caja Gatorade | 1.00 |
| Botella Agua | 2.00 |
When I send an order for client "Farmacia Moreno" with the following products:
@diegocasmo
diegocasmo / create_order_step.rb
Created September 28, 2016 01:15
An example step definition to create an order
Given(/^I am logged in as a salesman assigned to "(.*?)", with the following products:$/) do |client, table|
salesman = Support::Salesman.find_or_create
client = Support::Client.new(client)
Support::Salesman.assign_client(client)
Support::Product.assign_products(salesman, table.hashes)
LoginPage.visit.login(salesman)
end