Skip to content

Instantly share code, notes, and snippets.

@rohit9889
rohit9889 / paginate.js
Last active October 27, 2017 12:03
Javascript: Build pagination ability into array. Proves helpful in scenarios when you have a large collection at hand and need to paginate its items. TODO: fetch remote data source
Array.prototype.initalizePaging = function(itemsPerPage){
this.pageNumber = 1
this.itemsPerPage = isNaN(itemsPerPage) ? 10 : Number(itemsPerPage)
this.totalPages = Math.ceil(this.length/this.itemsPerPage)
}
Array.prototype.getCurrentPage = function(){
if(typeof this.pageNumber == "undefined"){
this.initalizePaging()
}
class ApplicationController < ActionController::Base
protect_from_forgery
# CUSTOM EXCEPTION HANDLING
rescue_from Exception do |e|
error(e)
end
def routing_error
@rohit9889
rohit9889 / mail_sender.rb
Created February 14, 2013 12:36
Change SMTP Settings at runtime in your rails application
class MailSender
def self.send_mail(smtp_settings, user, mail_subject, mail_body)
options = { :address => smtp_settings["address"],
:domain => smtp_settings["domain"],
:port => smtp_settings["port"].to_i,
:user_name => smtp_settings["user_name"],
:password => smtp_settings["password"],
:authentication => smtp_settings["authentication"],
:enable_starttls_auto => true,
:openssl_verify_mode => 'none'
@rohit9889
rohit9889 / directiveDemo.js
Created October 19, 2012 13:15
AngularJS Directive for Dynamic Template Generation
// My Application
var myApp = angular.module('myAngularApp', ['ui']);
// A Directive to generate Dynamic Template
myApp.directive('dynamicTemplate', function ($compile) {
return {
restrict: 'E',
scope: { form_generator_schema: '=data' },
link: function (scope, elm, attrs) {
console.log("Directive Called");
@rohit9889
rohit9889 / server_status.ru
Created September 8, 2012 09:31
Rack Application to check status of MySQL and MongoDB
require 'rack'
require 'rack/request'
require 'rack/response'
require 'rubygems'
require 'json'
require 'mysql2'
require 'mongo'
module Rack
class ServerStatus
@rohit9889
rohit9889 / cors-support.rb
Created September 6, 2012 09:52
cors in rails
before_filter :cors_preflight_check
after_filter :cors_set_access_control_headers
# For all responses in this controller, return the CORS access control headers.
def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
headers['Access-Control-Max-Age'] = "1728000"
end