Skip to content

Instantly share code, notes, and snippets.

View bensie's full-sized avatar

James Miller bensie

View GitHub Profile
require 'right_aws'
namespace :utils do
namespace :attachments do
task :initialize_s3 => :environment do
s3_config = YAML.load_file(File.join(File.dirname(__FILE__), '/../../config/amazon_s3.yml'))
s3_config = s3_config[RAILS_ENV].to_options
@s3 = RightAws::S3.new(s3_config[:access_key_id], s3_config[:secret_access_key])
end
@bensie
bensie / gist:58877
Created February 5, 2009 18:00 — forked from rails/gist:58761
var DateHelper = {
// Takes the format of "Jan 15, 2007 15:45:00 GMT" and converts it to a relative time
// Ruby strftime: %b %d, %Y %H:%M:%S GMT
time_ago_in_words_with_parsing: function(from) {
var date = new Date;
date.setTime(Date.parse(from));
return this.time_ago_in_words(date);
},
time_ago_in_words: function(from) {
@bensie
bensie / gist:58882
Created February 5, 2009 18:01 — forked from mislav/gist:58799
document.observe("dom:loaded", function() {
function updateElementsWithTime() {
$$("span[time]").invoke('updateTime')
}
// update immediately
updateElementsWithTime()
// continue updating every 2 minutes
new PeriodicalExecuter(updateElementsWithTime, 60 * 2)
})
#/usr/bin/ruby
name = 1
300.times do
`time dd if=/dev/zero of=#{name}.bin bs=100000 count=1`
name += 1
end
# controllers/application.rb
def current_cart
if session[:cart_id]
@current_cart ||= Cart.find(session[:cart_id])
session[:cart_id] = nil if @current_cart.purchased_at
end
if session[:cart_id].nil?
@current_cart = Cart.create!
session[:cart_id] = @current_cart.id
end
@bensie
bensie / environment.rb
Created February 18, 2009 15:10
Rails sanitize() method usage
# Add safe tags to sanitize method
Rails::Initializer.run do |config|
config.action_view.sanitized_allowed_tags = 'table', 'tr', 'td'
config.action_view.sanitized_allowed_attributes = 'id', 'class', 'style'
end
# If cached file exists, serve it and stop processing
RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}%{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ /cache/%{HTTP_HOST}$1 [L]
# other redirection (imgs, js, css, ...)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !.*/mephisto/.*
RewriteCond %{REQUEST_URI} !.*html
RewriteCond %{REQUEST_URI} !^/cache
RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}/$1 -f
@bensie
bensie / gist:67784
Created February 20, 2009 23:45
Safari - open new window links in a new tab
# How to set Safari to open links targeted for a new window in a new tab
defaults write com.apple.Safari TargetedClicksCreateTabs -bool true
@bensie
bensie / deploy.rb
Created February 21, 2009 02:05 — forked from defunkt/deploy.rb
# deploy to staging from your current topic branch, with ease
set :branch, "origin/#{`git branch`.scan(/^\* (\S+)/)}"
@bensie
bensie / birthday.sql
Created February 23, 2009 19:00
Get upcoming birthdays with SQL (discarding the year)
SELECT users.`firstname` AS first,
users.`lastname` AS last,
user_details.`birthday` AS birthday
FROM users
LEFT OUTER JOIN user_details ON user_details.user_id = users.id
WHERE
STR_TO_DATE(DATE_FORMAT(birthday,'%m-%d'),'%m-%d') >= STR_TO_DATE(DATE_FORMAT(now(),'%m-%d'),'%m-%d')
AND
STR_TO_DATE(DATE_FORMAT(birthday,'%m-%d'),'%m-%d') < STR_TO_DATE(DATE_FORMAT(now()+INTERVAL 7 DAY,'%m-%d'),'%m-%d');