Skip to content

Instantly share code, notes, and snippets.

View matthuhiggins's full-sized avatar

Matthew Higgins matthuhiggins

View GitHub Profile
@matthuhiggins
matthuhiggins / module.rb
Created November 9, 2010 05:17
Where to put the other files
module ActionControllerExtra
module UserLogger
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
# Logs that a user requested the current action:
#
# MyController < ApplicationController
@matthuhiggins
matthuhiggins / cache.rake
Created November 9, 2010 05:24
clear memcache
namespace :cache do
desc 'Clear memcache'
task :clear => :environment do
Rails.cache.clear
end
end
@matthuhiggins
matthuhiggins / belongs_to.rb
Created November 9, 2010 05:42
can haz namespaces
belongs_to :session, :class_name => "Statistics::Session"
@matthuhiggins
matthuhiggins / after.rb
Created November 9, 2010 17:05
matter of style
class ProfileController < ActionController::Base
private
def current_user
@current_user ||= User.find_by_username(session[:username]) if session[:username]
end
helper_method :current_user
end
@matthuhiggins
matthuhiggins / post_controller.rb
Created November 26, 2010 23:19
killed application controller
class PostController < ActionController::Base
require_login
end
@matthuhiggins
matthuhiggins / do_stuff.rb
Created November 26, 2010 23:23
ruby 1.8.7 Enumerator
def do_stuff(enumerator)
# stuff
end
do_stuff([1,3,5].reverse_each)
do_stuff([1,3,5].each)
@matthuhiggins
matthuhiggins / compact.js
Created November 26, 2010 23:51
in place prototype
Array.prototype._compact = function(iterator) {
var position = 0;
for (var index = 0, length = this.length; index < length; index++) {
if (this[index] != null)
this[position++] = this[index];
}
this.length = position;
return this;
};
@matthuhiggins
matthuhiggins / faster.sql
Created November 26, 2010 23:58
mysql_love_1
INSERT INTO tags(name, post_tags_count, created_at, updated_at) VALUES ('latin', utc_timestamp, utc_timestamp, 1) ON DUPLICATE KEY UPDATE updated_at = utc_timestamp, post_tags_count = post_tags_count + 1, id=LAST_INSERT_ID(id)
INSERT INTO tags(name, post_tags_count, created_at, updated_at) VALUES ('illegible', utc_timestamp, utc_timestamp, 1) ON DUPLICATE KEY UPDATE updated_at = utc_timestamp, post_tags_count = post_tags_count + 1, id=LAST_INSERT_ID(id)
INSERT INTO tags(name, post_tags_count, created_at, updated_at) VALUES ('rails', utc_timestamp, utc_timestamp, 1) ON DUPLICATE KEY UPDATE updated_at = utc_timestamp, post_tags_count = post_tags_count + 1, id=LAST_INSERT_ID(id)
SELECT * FROM `tags` WHERE (`tags`.`id` IN (1,2,3)
INSERT INTO `posts` (`created_at`, `updated_at`, `text`) VALUES('2008-07-04 23:17:20', '2008-07-04 23:17:20', 'Lorem ipsum dolor sit amet...')
INSERT INTO `post_tags` (`tag_id`, `post_id`) VALUES(1, 1)
INSERT INTO `post_tags` (`tag_id`, `post_id`) VALUES(2, 1)
INSERT INTO `post_tags` (`t
@matthuhiggins
matthuhiggins / faster.rb
Created November 27, 2010 00:02
mysql_love_3
def tag_list
self.tags.calculate(:group_concat, :name)
end
@matthuhiggins
matthuhiggins / create_table_1.rb
Created November 27, 2010 00:07
mysql_love_2
create_table :foo, :id => false do |t|
t.column :id, :manual_pk
t.column :bytes, :big_integer
end