Skip to content

Instantly share code, notes, and snippets.

View amitpatelx's full-sized avatar

Amit Patel amitpatelx

View GitHub Profile
@amitpatelx
amitpatelx / let_spec.rb
Created March 16, 2012 09:15
use let to define memoized helper method
$count = 0
describe "let" do
let(:count) { $count += 1 }
it "memoizes the value" do
count.should eq(1)
count.should eq(1)
end
it "is not cached across examples" do
@amitpatelx
amitpatelx / diff_before_and_let_spec.rb
Created March 16, 2012 09:55
Show usage difference between before and let helper of RSpec
# Using before method
before do
@customer = Customer.new(:name => "Amazon")
end
it "should be valid" { @customer.should be_valid }
# Using let method
let(:customer) { Customer.new(:name => "Amazon") }
@amitpatelx
amitpatelx / stack_spec.rb
Created March 16, 2012 10:37
This snippet(from The RSpec Book) shows how to define example group in RSpec.
require "rspec/expectations"
class Thing
def widgets
@widgets ||= []
end
end
describe Thing do
before(:all) do
# Renders the template located in [TEMPLATE_ROOT]/weblog/show.r(html|xml) (in Rails, app/views/weblog/show.erb)
render :template => "weblog/show"
# Renders the template with a local variable
render :template => "weblog/show", :locals => {:customer => Customer.new}
Rails::Initializer.run do |config|
config.time_zone = "UTC"
end
@amitpatelx
amitpatelx / gist:2310060
Created April 5, 2012 11:23
Get timezone offset in browser using javascript
function getTimezone(){
var current_date_time = new Date();
currentTimeZoneOffsetInHours = (-1) * current_date_time.getTimezoneOffset()/60;
return currentTimeZoneOffsetInHours;
}
@amitpatelx
amitpatelx / gist:2567922
Created May 1, 2012 13:33
PostgreSQL : Query to get all values of an enum
SELECT e.enumlabel
FROM pg_enum e
JOIN pg_type t ON e.enumtypid = t.oid
WHERE t.typname = 'yourenumname'
@amitpatelx
amitpatelx / gist:2763585
Created May 21, 2012 17:54
Sort 2D array
1.9.3-p125 :007 > x=[["2010-01-10", 2], ["2010-01-09", 5], ["2009-12-11", 3], ["2009-12-12", 12], ["2009-12-13", 0]]
=> [["2010-01-10", 2], ["2010-01-09", 5], ["2009-12-11", 3], ["2009-12-12", 12], ["2009-12-13", 0]]
1.9.3-p125 :008 > x.sort_by{|k|k[1]}
=> [["2009-12-13", 0], ["2010-01-10", 2], ["2009-12-11", 3], ["2010-01-09", 5], ["2009-12-12", 12]]
1.9.3-p125 :009 > x.sort_by{|k|k[0]}
=> [["2009-12-11", 3], ["2009-12-12", 12], ["2009-12-13", 0], ["2010-01-09", 5], ["2010-01-10", 2]]
@amitpatelx
amitpatelx / gist:2763786
Created May 21, 2012 18:28
Remove element of an array conditionally
1.9.3-p125 :002 > [1,2,3,4,5].delete_if{|i| i%2==0}
=> [1, 3, 5]
@amitpatelx
amitpatelx / omniauth_callbacks_controller.rb
Created July 10, 2012 05:53
OmniAuth callback controller with twitter handler
#app/controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def twitter
# You need to implement the method below in your model
auth = env["omniauth.auth"]
@user = user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.new
if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success"
sign_in_and_redirect @user, :event => :authentication
else