Skip to content

Instantly share code, notes, and snippets.

@athap
athap / download_xls.rb
Created June 17, 2012 03:22
Downloading data as xls or Excel without a Gem
# This gist demonstrates - How to download data from database in excel or xls format in Ruby on Rails
# Tested with - Ruby 1.9.3, rails 3.2.1, db - postgres
class DownloadsController < ApplicationController
before_filter :define_header, :except => :index
def index
...
end
@athap
athap / Fibonacci log n solution
Created August 3, 2012 06:09
Fibonacci sequence in log(n) using matrix multiplication
/*
* | 1 1 |n |Fn+1 Fn |
* | 1 0 | = |Fn Fn-1 |
*
* Figure 1
*/
public int FibonacciLogN(int n)
{
if(n <= 0)
return 0;
@athap
athap / JQuery Flash Messages
Created September 1, 2012 17:43
Rails like flash messages using JQuery for JS or AJAX calls
# This gist shows how to display Rails like flash messages while handling AJAX or JS requests
# Note - This might not be the best way to do this but it works for me
Step 1. Create a partial which will contain the message to be displayed
Example - _success.html.erb
<div id="flash_message">
<p>Success</p>
</div>
Step 2. In the Controller, add code to call this partial on a JS request
@athap
athap / config_files.js.coffee
Last active December 10, 2015 09:48
Content of application.js for adding Require.js to Backbone.js application
-----------------------------------------------
application.js
-----------------------------------------------
require [
'backbone',
'path/to/router/with/index/handler'],
(Backbone, IndexRouter) ->
$ ->
Main = new IndexRouter()
@athap
athap / wait_for_2_req.coffee
Created February 19, 2014 03:06
Wait for N or Multiple or Unknown number (Depends on some logic) of ajax request using JQuery
class Test
waitForTwoCalls: () ->
$.when(@firstCall(), @secondCall()).then () =>
doSomeThing()
firstCall: () ->
token = $.Deferred
$.ajax({
url: foo/bar,
@athap
athap / wait_for_n_call.coffee
Last active August 29, 2015 13:56
Wait for N or Multiple or Unknown number (Depends on some logic) of ajax request using JQuery
# Makes ajax req to fetch data
class Model
initialize(id)
@id = id
load: (token) ->
$.ajax({
url: "foo/bar/#{@id}",
....
success: (response) =>
@athap
athap / truncate.rb
Created March 19, 2014 01:01
Truncate text in Ruby
MaxLength = N
valid_string = some_long_text[0,N] if some_long_text.length > N
@athap
athap / watch_file_spawn_process.js
Last active August 29, 2015 13:57
Watch File And List File Information By Spawning A Process
var fs = require('fs');
var spawnProcess = require('child_process').spawn;
var filename = 'some/file'
fs.watch(filename, function() {
var list = spawnProcess('ls', ['-lh', filename]);
var info = '';
// listener get invoked every time a chunk of data is available. Data is read in chunks
list.stdout.on('data', function(chunk) {
@athap
athap / simple_connect.js
Last active August 29, 2015 13:58
Connecting To MongoDB From Node.js Beginner's Basic
var MongoClient = require('mongodb').MongoClient;
var connectionString = 'mongodb://localhost:27017/test';
// connect to test db
MongoClient.connect(connectionString, function(err, db) {
if(err) throw err;
console.log("CONNECTED");
var q = {name: 'Super Man'};
// query superheroes collection and display result
@athap
athap / node.js
Last active August 29, 2015 14:06
Node - Represent node in the tree
// Represents the node in the tree. Will be displayed as a small circle in the browser.
// x, y --> x, y co-ordinates of the center of circle
// r --> radius of the circle
// ctx --> context of the canvas
// data --> data to be displayed (Only number)
var Node = function(x,y,r, ctx, data) {
// left child of a node
this.leftNode = null;
// right child of a node