Skip to content

Instantly share code, notes, and snippets.

@ktkaushik
ktkaushik / client_spec.rb
Created March 27, 2012 17:05
Specs for testing MongoDB's Embedded and Referenced association with Mongoid.
require 'spec_helper'
describe Client do
context "With valid client info" do
context "Associations" do
it "should embed many projects" do
association = Client.relations['projects']
association.klass.should == Project
association.relation.should == Mongoid::Relations::Embedded::Many
end
@ktkaushik
ktkaushik / gist:2223776
Created March 28, 2012 04:59 — forked from rtdp/gist:742461
Importing Gmail Contacts list to Rails Application.
class ImportedContactsController << ApplicationController
require 'net/http'
require 'net/https'
require 'uri'
#THIS METHOD TO SEND USER TO THE GOOGLE AUTHENTICATION PAGE.
def authenticate
# initiate authentication w/ gmail
# create url with url-encoded params to initiate connection with contacts api
# next - The URL of the page that Google should redirect the user to after authentication.
# scope - Indicates that the application is requesting a token to access contacts feeds.
@ktkaushik
ktkaushik / mongoid.yml
Created April 9, 2012 04:53
Mongoid YAML file.
development:
host: localhost
database: quadmint_development
test:
host: localhost
database: quadmint_test
# set these environment variables on your prod server
production:
@ktkaushik
ktkaushik / puma_web_server_on_crud_operations.txt
Created April 10, 2012 04:44
Puma Web Server after regular CRUD operation on scaffold generator for Product Model.
Puma 1.1.1 starting...
* Min threads: 0, max threads: 16
* Listening on tcp://0.0.0.0:9292
Use Ctrl-C to stop
127.0.0.1 - - [10/Apr/2012 10:14:46] "GET /index.html HTTP/1.1" 304 - 2.5345
127.0.0.1 - - [10/Apr/2012 10:14:51] "GET /rails.png HTTP/1.1" 304 - 4.0353
127.0.0.1 - - [10/Apr/2012 10:14:51] "GET /favicon.ico HTTP/1.1" 304 - 0.0005
127.0.0.1 - - [10/Apr/2012 10:14:53] "GET /products HTTP/1.1" 304 - 0.8145
127.0.0.1 - - [10/Apr/2012 10:14:53] "GET /scaffolds.css?body=1 HTTP/1.1" 304 - 0.0055
127.0.0.1 - - [10/Apr/2012 10:14:53] "GET /products.css?body=1 HTTP/1.1" 304 - 0.0126
@ktkaushik
ktkaushik / status_header_setter
Created April 17, 2012 11:32
Set HTTP status headers with respond_with( object ) in rails.
1xx Informational
100 Continue :continue
101 Switching Protocols :switching_protocols
102 Processing :processing
2xx Success
200 OK :ok
@ktkaushik
ktkaushik / custom_logger.rb
Created April 22, 2012 04:56
Custom Logger in rails 3.2.x. Store logs in different logs based on environment as well, All in one file.
# If you wish to store everything in one logger file then do this.
class CustomLogger
LOGGERFILE = File.join("#{Rails.root}/log/category_clicks_#{Rails.env}_errors.log")
def self.log( message, severity = :info )
@category_click_logger ||= ActiveSupport::BufferedLogger.new( LOGGERFILE )
@model_log.send severity, "[#{Time.now.to_s(:db)}] [#{severity.to_s.capitalize}] #{message}\n"
end
end
@ktkaushik
ktkaushik / custom_logger.rb
Created April 22, 2012 05:02
Custom Logger to store logs based on class name or table name. A logger for each file. Create an initializer as cutom_logger.rb.
# This should be an initializer.
class CustomLogger
TABFILE = File.join("#{Rails.root}/log/tab_#{Rails.env}_errors.log")
SEARCHFILE = File.join("#{Rails.root}/log/searches_#{Rails.env}_errors.log")
VIDEOADFILE = File.join("#{Rails.root}/log/video_ads_#{Rails.env}_errors.log")
BANNERADFILE = File.join("#{Rails.root}/log/banner_ads_#{Rails.env}_errors.log")
CATEGORYFILE = File.join("#{Rails.root}/log/categories_#{Rails.env}_errors.log")
CONTENTPATHFILE = File.join("#{Rails.root}/log/content_paths_#{Rails.env}_errors.log")
CATEGORYCLICKFILE = File.join("#{Rails.root}/log/category_clicks_#{Rails.env}_errors.log")
@ktkaushik
ktkaushik / SomeController.rb
Created April 22, 2012 06:52
Using Prawn in your Rails App to download and attach the pdf in your email
require 'pdf_generator'
class TasksController < ApplicationController
include PdfGenerator
def some_action
pdf_generator( download = true )
end
end
@ktkaushik
ktkaushik / rspec-syntax-cheat-sheet.rb
Created April 24, 2012 08:34 — forked from irohiroki/rspec-syntax-cheat-sheet.rb
RSpec 2 syntax cheat sheet by example
# RSpec 2.0 syntax Cheet Sheet by http://ApproachE.com
# defining spec within a module will automatically pick Player::MovieList as a 'subject' (see below)
module Player
describe MovieList, "with optional description" do
it "is pending example, so that you can write ones quickly"
it "is already working example that we want to suspend from failing temporarily" do
pending("working on another feature that temporarily breaks this one")
@ktkaushik
ktkaushik / rspec-syntax-cheat-sheet.rb
Created April 24, 2012 08:34 — forked from dnagir/rspec-syntax-cheat-sheet.rb
RSpec 2 syntax cheat sheet by example
# RSpec 2.0 syntax Cheet Sheet by http://ApproachE.com
# defining spec within a module will automatically pick Player::MovieList as a 'subject' (see below)
module Player
describe MovieList, "with optional description" do
it "is pending example, so that you can write ones quickly"
it "is already working example that we want to suspend from failing temporarily" do
pending("working on another feature that temporarily breaks this one")