Skip to content

Instantly share code, notes, and snippets.

View humanzz's full-sized avatar
🤹‍♂️

Ahmed Kamel humanzz

🤹‍♂️
  • Edinburgh, United Kingdom
View GitHub Profile
@humanzz
humanzz / caches_page_with_expire_after.rb
Created July 18, 2010 10:40
Modified version of caches_page to allow specifiying an :expire_after such that the page will be expired via a background job
class ActionController::Base
# Modified version of caches_page to allow specifiying an :expire_after
# such that the page will be expired via a background job
# Example:
# class PostsController < ApplicationController
# time_caches_page :index, :expire_after => 5.minutes
# .
# .
# end
def self.time_caches_page(*actions)
@humanzz
humanzz / javascripts_helper.rb
Created July 8, 2010 16:20
A rails helper to help you organize javascripts in your views
module JavascriptsHelper
# This helper yeilds contents to 2 places that should be available
# in the application layout. One for including javascript files,
# the other is for including initialization codes. The layout should have
# something as follows:
# <%= yield :javascripts %>
# <script type="text/javascript">
# $(function({
# <%= yield :javascripts_ready %>
# }));
@humanzz
humanzz / constants.rb
Created February 20, 2010 15:50
A simple class trying to solve the issue of defining constants in a ruby application
# A simple class trying to solve the issue of defining constants in a ruby application.
# I have always tended to define constants I use as Hashes as follows
# PersonTypes = {:good_guy => 1, :super_hero => 2}
# But the moment I needed to add more attributes, things Got Ugly
# PersonTypesInfo = {:good_guy => {:id => 1, :name => "Mr Good Guy"},
# :super_hero => {:id => 2, :name => "Mr Hero"}}
# So, here's a simple class that initializes constants that hold attributes
# and enable you to retreive them in different ways like getting all of them
# or oredring them by one of the attributes
# Example usage
@humanzz
humanzz / countries_migration.rb
Created January 19, 2009 09:26
A RoR migration for creating Countries table
# obtained from http://snippets.dzone.com/posts/show/1727
# for the complete list (http://en.wikipedia.org/wiki/ISO_3166-1)
class CreateCountries < ActiveRecord::Migration
def self.up
create_table :countries do |t|
t.column :iso, :string, :size => 2
t.column :name, :string, :size => 80
t.column :printable_name, :string, :size => 80
@humanzz
humanzz / Mercator.rb
Created October 29, 2008 13:07
Ruby implementation for GMercatorProjection
# This is a port of the javascript code found in the following link
#http://groups.google.com/group/Google-Maps-API/browse_thread/thread/a45947d72c27cc73
class Mercator
#offset is defined at zoom level 21 which means that this Mercator is valid for 21 zoom levels
OFFSET = 268435456
RADIUS = OFFSET / Math::PI
def self.lng_to_x(lng, zoom)
(OFFSET + RADIUS * lng * Math::PI / 180).to_i >> (21 - zoom)