Skip to content

Instantly share code, notes, and snippets.

View bansalakhil's full-sized avatar

Akhil Bansal bansalakhil

View GitHub Profile
@bansalakhil
bansalakhil / gist:3181222
Created July 26, 2012 09:34
Calculating Sum
# I have seen people writing something like:
users.collect{|u| u.tasks.size}.sum
# This can also be written as
users.sum{|u| u.tasks.size}
@bansalakhil
bansalakhil / gist:3205304
Created July 30, 2012 06:24
Git log searching
Only show commit history earlier than 5 days ago:
git log --since=5.day.ago
Only show commit history older than 5 days ago:
git log --until=5.day.ago
@bansalakhil
bansalakhil / gist:3207071
Created July 30, 2012 13:59
Custom Flash Types in Edge Rails
# Rails Edge: Custom Flash Types
#
#Rails just got some syntactic sugar for dealing with flash messages in
#your controllers and views.
#
#You can now declare the following in your controllers:
#
class ApplicationController
add_flash_types :error, :warning
@bansalakhil
bansalakhil / gist:3401574
Created August 20, 2012 06:34
Edge Rails accepts a polymorphic modifier for references in migration generators
Allow scaffold/model/migration generators to accept a polymorphic modifier for references/belongs_to, for instance
rails g model Product supplier:references{polymorphic}
will generate the model with belongs_to :supplier, polymorphic: true association and appropriate migration.
@bansalakhil
bansalakhil / gist:3402488
Created August 20, 2012 09:01
Edge rails: Now create HABTM join tables easily using migration helper
# Edge rails you can do:
create_join_table :products, :categories
# Earlier:
create_table :categories_products, :id => false do |td|
td.integer :product_id, :null => false
td.integer :category_id, :null => false
@bansalakhil
bansalakhil / gist:3433947
Created August 23, 2012 07:51
cattr_accessor vs class_attibute
# cattr_accessor
class Base
cattr_accessor :settings
# def self.settings
# @@settings
# end
# def self.settings=(value)
@bansalakhil
bansalakhil / gist:3550776
Created August 31, 2012 09:29
Responsive Web Designs
Beginner’s Guide to Responsive Web Design
http://blog.teamtreehouse.com/beginners-guide-to-responsive-web-design
Responsive Web Design, by- ETHAN MARCOTTE
http://www.alistapart.com/articles/responsive-web-design/
E-Book on Responsive web design, by - ETHAN MARCOTTE
http://www.abookapart.com/products/responsive-web-design
A Presentation on Responsive Design:
@bansalakhil
bansalakhil / gist:3720549
Created September 14, 2012 07:37
Custom error pages in Rails3.2
# In rails 3.2, it's creating custom error pages is much more easier:
#
# Add this to config/application.rb:
config.exceptions_app = self.routes
# That causes errors to be routed via the router. Then you just add to config/routes.rb:
match "/404", :to => "errors#not_found"
@bansalakhil
bansalakhil / gist:4117298
Created November 20, 2012 11:02
Writing Faster Rails Tests
Hey there! And welcome to my little mailing list about writing faster Rails tests.
My goal is to send you only focused, actionable info that will help you speed up your test suites right away. Let's get started!
The first thing we'll cover is the leading cause of test slowness in Rails apps: bloated fixtures.
Here's an example of a test that will run far slower than it needs to (using factory_girl (a great tool, despite its involvement in this test's slowness)):
describe "#full_name" do
@bansalakhil
bansalakhil / power_raise.exs
Last active December 8, 2015 13:48
Elixir macro implementation of power raise
defmodule Power do
defmacro raise_n(x) do
method_name = :"raise_#{x}"
quote do
def unquote(method_name)(val) do
:math.pow(val, unquote(x) )
end
end
end
end