Skip to content

Instantly share code, notes, and snippets.

View mech's full-sized avatar
🏠
Working from home

mech mech

🏠
Working from home
  • Career Pacific / Jobline
  • Singapore, Tampines
View GitHub Profile
@jimeh
jimeh / delayed_job.rb
Created March 26, 2010 22:42
delayed_job.rb: Capistrano tasks to "properly" start/stop/restart the delayed_job daemon.
#
# NOTICE: The stop/restart tasks won't work properly due to a bug in the daemons gem
# unless you use the ghazel-daemons gem by putting this in your environment.rb file:
#
# config.gem "ghazel-daemons", :lib => "daemons"
# gem "ghazel-daemons"
# require "daemons"
#
# This will force-load the 'ghazel-daemons' gem and make sure it's used instead of
# the 'daemons' gem. It works even with the 'daemons' gem installed, so you won't
require 'action_view'
require 'active_support'
require 'mustache'
class Mustache
# Remember to use {{{yield}}} (3 mustaches) to skip escaping HTML
# Using {{{tag}}} will skip escaping HTML so if your mustache methods return
# HTML, be sure to interpolate them using 3 mustaches.
def genitivize(name)
name.strip.sub(/(s)?$/i) { |s| s.downcase == 's' ? "#{s}’" : '’s' }
end
module Archives
module Callbacks
def self.included(base)
base.send :include, Archives::Callbacks::InstanceMethods
base.before_create :do_something_awesome
end
module InstanceMethods
private
@visnup
visnup / Screen shot 2011-02-07 at 4.37.04 PM.png
Created February 8, 2011 00:37
# trippy mongodb colorized logging by statement type
Screen shot 2011-02-07 at 4.37.04 PM.png
@sco
sco / gist:832499
Created February 17, 2011 19:48
Using XAuth to exchange username/password credentials for a OAuth access token
// UITextFieldDelegate method, called when the "Return" key is pressed.
// Either advance the cursor to the next empty field, or submit the form.
//
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if([usernameField.text length] == 0) {
[usernameField becomeFirstResponder];
} else if ([passwordField.text length] == 0) {
[passwordField becomeFirstResponder];
} else {
@tomafro
tomafro / instrumentation.rb
Created February 18, 2011 09:13
Experimental Mongo Instrumentation for Rails 3
module Mongo
module Instrumentation
def self.instrument(clazz, *methods)
clazz.module_eval do
methods.each do |m|
class_eval %{def #{m}_with_instrumentation(*args, &block)
ActiveSupport::Notifications.instrumenter.instrument "mongo.mongo", :name => "#{m}" do
#{m}_without_instrumentation(*args, &block)
end
end
module RequireLoggedUser
protected
def require_logged_user!
before_filter do
return true if current_user.present?
# if the method hasn't returned, there is no user logged in!
# flash a warning and redirect the user to the log in page
flash[:warning] = "You must log in before proceeding!"
redirect_to new_user_session_path and return
end
@pogodan
pogodan / Gemfile
Created April 24, 2011 14:10
edge rails/3.1 Gemfile
source :rubygems
gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'arel', :git => 'git://github.com/rails/arel.git'
gem 'rack', :git => 'git://github.com/rack/rack.git'
gem 'sprockets', :git => 'git://github.com/sstephenson/sprockets.git'
gem 'sqlite3'
# Asset template engines
@babelian
babelian / gist:982801
Created May 20, 2011 12:23
dynanic mass assign
class Project < ActiveRecord::Base
class << self
def always_attributes
%w{attributes anybody can edit}
end
def draft_attributes
%w{available only while project is being written}