Skip to content

Instantly share code, notes, and snippets.

View vparihar01's full-sized avatar

Vivek Parihar vparihar01

View GitHub Profile
require 'mail'
Mail.defaults do
delivery_method :smtp, { :address => "smtp.sendgrid.net",
:port => 587,
:domain => "xyz.com",
:user_name => "xyz@zyz.com",
:password => "xyz",
:authentication => 'plain',
:enable_starttls_auto => true }
end
@vparihar01
vparihar01 / setting_hostname_sendmail_in_ubuntu.md
Created June 27, 2013 11:55
For changing the mail hosts name from your local machine name to domain name.Setting the hostname in sendmail.

If you need to change the hostname that Sendmail announces itself as, just add the following to sendmail.mc:

define(`confDOMAIN_NAME', `mail.yourdomain.com')dnl

For example-: Your domain name is www.xyz.com, then

define(`confDOMAIN_NAME', `www.xyz.com')dnl
@vparihar01
vparihar01 / printingJavascriptObject.js
Created June 25, 2013 13:23
for printing down the javascript [object Object] value.This get all properties values of a Javascript Object (without knowing the keys).
for(var key in objects) {
var value = objects[key];
}
Object.values = function (obj) {
var vals = [];
for( var key in obj ) {
if ( obj.hasOwnProperty(key) ) {
vals.push(obj[key]);
}
require 'oauth_util.rb'
require 'net/http'
o = OauthUtil.new
o.consumer_key = 'examplek9SGJUTUpocjZ5QjBJmQ9WVdrOVVFNHdSR2x1TkhFbWNHbzlNQS0tJnM9Y29uc3VtkZXJzZWNyZXQmeD0yYg--';
o.consumer_secret = 'exampled88d4109c63e778dsadcdd5c1875814977';
url = 'http://query.yahooapis.com/v1/yql?q=select%20*%20from%20social.updates.search%20where%20query%3D%22search%20terms%22&diagnostics=true';

Polymorphic Associations reversed

It's pretty easy to do polymorphic associations in Rails: A Picture can belong to either a BlogPost or an Article. But what if you need the relationship the other way around? A Picture, a Text and a Video can belong to an Article, and that article can find all media by calling @article.media

This example shows how to create an ArticleElement join model that handles the polymorphic relationship. To add fields that are common to all polymorphic models, add fields to the join model.

1. An issue popups in Github. A bug or a feature request.
2. Developer starts coding after understanding the issue. He makes sure to add issue number in the form of #XXX - XXX is issue number - to the commit message. This links issue with the commit that was made. It's important to know 'why the commit was made'.
3. A CI listens to pushes to the Github repo and runs all the specs and optionally deploys the code to one of the staging servers.
4. After developer is done with coding, s/he assigns the issue to another developer for code review.
5. Code reviewer makes sure that all the tests are passing along with presence of code and test coverage. Code reviewer can assign issue back to the developer if there is some issue with the code. Developer will start at step 1 again.
6. Code reviewer assigns issue to QA. If QA is not happy, s/he adds issues that were found as comment to the original issue or possibly create new issues depending upon severity of the problem s/he found out.
7. If QA is happy, she assigns th
class YourModel
before_save :sanitize_name
private
def sanitize_name
self.name = CGI::escapeHTML(name)
end
end

Exposing an API

APIs are becoming an essential feature of modern web applications. Rails does a good job of helping your application provide an API using the same MVC structure you're accustomed to.

In the Controller

Let's work with the following example controller:

class ArticlesController < ApplicationController
@vparihar01
vparihar01 / gist:5575280
Created May 14, 2013 11:32
PERFORMANCE TUNING FOR PHUSION PASSENGER for production ready app.
# PassengerMaxPoolSize
# Default: 6
# For 2gb RAM: 30
# For 256 slice with MySQL running: 2
PassengerMaxPoolSize 2
# PassengerPoolIdleTime
# Recommended to be avg time per page * 2
# In Google Analytics... (Avg time on site / Avg page views) * 2
# Default: 300

ruby-1.9.3-p327 cumulative performance patch for rbenv

This installs a patched ruby 1.9.3-p327 with various performance improvements and a backported COW-friendly GC, all courtesy of funny-falcon.

Requirements

You will also need a C Compiler. If you're on Linux, you probably already have one or know how to install one. On OS X, you should install XCode, and brew install autoconf using homebrew.