Skip to content

Instantly share code, notes, and snippets.

View matthuhiggins's full-sized avatar

Matthew Higgins matthuhiggins

View GitHub Profile
@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 / 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 / post_controller.rb
Created November 26, 2010 23:19
killed application controller
class PostController < ActionController::Base
require_login
end
@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 / belongs_to.rb
Created November 9, 2010 05:42
can haz namespaces
belongs_to :session, :class_name => "Statistics::Session"
@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 / 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 / asset_host.rb
Created November 9, 2010 05:13
google ajax
google_paths = {
'prototype' => 'http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/',
'controls' => 'http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.1/',
'dragdrop' => 'http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.1/',
'effects' => 'http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.1/'
}
config.action_controller.asset_host = Proc.new do |source, request|
google_asset = google_paths.keys.detect { |asset| source.starts_with?("/javascripts/#{asset}") }
google_asset ? google_paths[google_asset] : "#{request.protocol}#{request.host_with_port}"
class Post < ActiveRecord::Base
def title=(value)
self[:title] = value.presence || 'Untitled'
end
end
class Post < ActiveRecord::Base
def title=(value)
self[:title] = value.present? ? value : 'Untitled'
end
end