Skip to content

Instantly share code, notes, and snippets.

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

SengMing Tan tansengming

🏠
Working from home
View GitHub Profile
@tansengming
tansengming / .gitconfig
Last active April 25, 2025 11:15
Default gitconfig
[user]
name = SengMing Tan
email = email@example.com
[alias]
b = branch
c = commit .
co = checkout
stat = status
s = status
h = log --pretty=format:'%Cblue%h %Cgreen %ar%Creset %s'
@tansengming
tansengming / configure.rb
Created July 9, 2012 07:37
Ruby configure blocks
# How Clearance / Hoptoad does it
module Clearance
class << self
attr_accessor :configuration
end
def self.configure
self.configuration ||= Configuration.new
yield(configuration)
end
@tansengming
tansengming / spec_helper.rb
Created May 3, 2012 06:31
Default spec helper with Factory girl
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
@tansengming
tansengming / Rails Console Actionmailer test.rb
Created April 24, 2009 15:14
Rails Console Actionmailer test
# Copy and paste this to the rails console to test your email settings
class MyMailer < ActionMailer::Base
def test_email
@recipients = "someone@somewhere.com"
@from = "must_be_same_as_smpt_account_login@gmail.com"
@subject = "test from the Rails Console"
@body = "This is a test email"
end
end
@tansengming
tansengming / Rakefile
Last active February 11, 2020 22:22
Janky daily twitter daily feed
require 'irb'
require 'pathname'
require 'json'
require 'rake/clean'
require 'time'
class Tweet
attr_reader :tweet_id, :created_at
def initialize(row)
@tansengming
tansengming / ar.rb
Last active July 24, 2018 17:24
Programming Notes
# joins with time range
time_range = (Time.now.midnight - 1.day)..Time.now.midnight
Client.joins(:orders).where(orders: { created_at: time_range })
@tansengming
tansengming / crawl.rake
Created October 29, 2009 07:57
Rake task to crawl you rails app for broken links
# Use this to look for broken links in your app.
# crawls the development server http://localhost:3000
# Suggestion: Also check to make sure that intentional 404s
# are handled gracefully by app.
task :crawl => :environment do
require 'anemone'
root = 'http://localhost:3000'
options = {:discard_page_bodies => true, :verbose => true}
curl --silent https://raw.githubusercontent.com/cmusphinx/cmudict/master/cmudict.dict |
egrep '[a-z]+\s+[^AEIOU]*[AEIOU]+[^AEIOU]+[AEIOU]+[^AEIOU]*$' |
perl -pe 's/([a-z]+).*/\1/' |
comm --nocheck-order -12 - /usr/share/dict/words |
shuf -n 2
commnunity.users.each do |recipient|
sms_credits = community.sms_credits
if sms_credits > 0
# this chucks the job into a queue and returns immediately!
SmsSenderJob.perform_later recipient, 'Everything is fine.'
# take off a credit
community.sms_credits = sms_credits - 1
community.save
# Job 1
sms_credits = community.sms_credits # start with 100 credits
# Switch! -------------------------------------------------------------------
# Job 2
sms_credits = community.sms_credits # still 100 credits!!
if sms_credits > 0
send_sms recipient, message
# take off a credit
community.sms_credits = sms_credits - 1