Skip to content

Instantly share code, notes, and snippets.

View karthiks's full-sized avatar
😄
Playing with GraphQL

Karthik Sirasanagandla karthiks

😄
Playing with GraphQL
View GitHub Profile
@karthiks
karthiks / gem_bundler_name_error
Created June 20, 2011 09:59
When you see Gem error like - "'<class:UI>': uninitialized constant Gem::SilentUI (NameError)"
karthik@cloud:~/MyRubyProjects/beach_projects/dashboard$ bundle install
/home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/gems/bundler-1.0.7/lib/bundler/ui.rb:56:in '<class:UI>': uninitialized constant Gem::SilentUI (NameError)
from /home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/gems/bundler-1.0.7/lib/bundler/ui.rb:2:in `<module:Bundler>'
from /home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/gems/bundler-1.0.7/lib/bundler/ui.rb:1:in `<top (required)>'
from /home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/gems/bundler-1.0.7/lib/bundler/cli.rb:16:in `initialize'
from /home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/gems/bundler-1.0.7/lib/bundler/vendor/thor.rb:246:in `new'
from /home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/gems/bundler-1.0.7/lib/bundler/vendor/thor.rb:246:in `dispatch'
from /home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/gems/bundler-1.0.7/lib/bundler/vendor/thor/base.rb:389:in `start'
from /home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/gems/bundler-1.0.7/bin/bundle:13:in `<top (required)>'
from /home/karthik/.
@karthiks
karthiks / understanding_the_method_method.rb
Created June 27, 2011 18:27
Finding the Ruby method definition - one that is invoked at runtime.
class Fixnum
def crime
end
end
p 2.method(:crime)
#<Method: Fixnum#crime> #crime() defined in Fixnum class is invoked
module Loser
def crime
end
@karthiks
karthiks / application.rb
Created September 6, 2011 08:52
Rails 3 - Filtering Sensitive Parameters From Being Logged
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)
module MyRails3App
class Application < Rails::Application
@karthiks
karthiks / rspec-syntax-cheat-sheet.rb
Created September 9, 2011 18:19 — 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")
@karthiks
karthiks / shoulda_cant_find_first.rb
Created September 21, 2011 15:28
Can't find first MyFreakingActiveRecordModel....
### Terminal Output showing spec failure ###
Failures:
1) CurrencyConverter#validations
Failure/Error: it { should validate_uniqueness_of(:from).scoped_to(:to) }
Can't find first CurrencyConverter
# ./spec/models/currency_converter_spec.rb:4:in `block (3 levels) in <top (required)>'
###########################################
### The Solution ###
@karthiks
karthiks / my_good_bad_sqli_model.rb
Created October 21, 2011 16:33
Custom finders and SQL Injections
################################################################################
################################## Yucky code ##################################
find_by_name name
User.all( :conditions => "first_name LIKE #{name}% OR last_name LIKE #{name}%") #prone to SQL injection. Imagine the parameter name = "1; drop table users;"
end
############################################################################################################################
# For Active Record to sanitize the input parameters from SQL Injection of sorts, you may adopt one of the following styles:
@karthiks
karthiks / address_spec.rb
Created October 21, 2011 17:00
leveraging Subject in RSpec - 1
describe Address do
describe "#validations" do
it "must have a city" do
a = Address.new
a.should_not be_valid
a.errors_on(:city).should_not be_nil
end
it "must have a state" do
a = Address.new
@karthiks
karthiks / address_spec.rb
Created October 21, 2011 17:00
leveraging Subject in RSpec - 1
describe Address do
describe "#validations" do
it "must have a city" do
a = Address.new
a.should_not be_valid
a.errors_on(:city).should_not be_nil
end
it "must have a state" do
a = Address.new
@karthiks
karthiks / address_spec.rb
Created October 21, 2011 17:17
leveraging Subject in RSpec - 2
describe Address do
describe "#validations" do
before(:each) do
@a = Address.new
@a.should_not be_valid
end
it "must have a city" do
@a.errors_on(:city).should_not be_nil
end
@karthiks
karthiks / address_spec.rb
Created October 21, 2011 17:21
leveraging Subject in RSpec - 3
describe Address do
describe "#validations" do
before(:all) do
@a = Address.new
@a.should_not be_valid
end
%w(:city :state :country).each do |attr|
it "must have a #{attr}" do
@a.errors_on(attr).should_not be_nil