Skip to content

Instantly share code, notes, and snippets.

@oliyoung
oliyoung / gist:1975
Created July 24, 2008 00:55
form helper to display tags from acts_as_taggable_on
class ActionView::Helpers::FormBuilder
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::FormTagHelper
def tag_list(method, options={})
return false unless options[:tags]
links = options[:tags].map do |tag|
link_to_function( tag.to_s, "toggle_tag('#{tag}', '#{@object.class.to_s.downcase}_#{method.to_s.pluralize.downcase}')", :class => ["tag", (@object.send(method).map(&:name).include?(tag.to_s) ? "selected" : "")].compact.join(" "), :id => tag )
end
@oliyoung
oliyoung / gist:1976
Created July 24, 2008 00:57
convert a string into a URL safe slug
class String
def to_param
self.downcase.strip.gsub(/[\/ ]+/, "-").gsub(/[^a-z0-9\-\_]+/i, "").gsub(/\-+/,"-")
end
end
@oliyoung
oliyoung / gist:1977
Created July 24, 2008 01:01
Capistrano deploy script with mongrel specifics
set :application, "application.url.com"
set :repository, "http://svn.#{application}/trunk/"
set :runner, ENV['LOGNAME']
set :deploy_to, "/var/www/#{application}"
role :app, application
role :web, application
role :db, application, :primary => true
task :after_update_code, :roles => :app do
run "ln -s #{deploy_to}/shared/system/mongrel_cluster.yml #{release_path}/config/mongrel_cluster.yml"
@oliyoung
oliyoung / gist:1978
Created July 24, 2008 01:02
show an animated gif whenever an AJAX called is made
Ajax.Responders.register({
onCreate: function() {
if($('busy') && Ajax.activeRequestCount>0)
Effect.Appear('busy',{duration:0.5,queue:'end'});
},
onComplete: function() {
if($('busy') && Ajax.activeRequestCount==0)
Effect.Fade('busy',{duration:0.5,queue:'end'});
}
});
class Date
def at_some_point(options = {})
start_hour = options[:between]
end_hour = options[:and]
if start_hour && end_hour
hours = rand(end_hour) + start_hour
else
hours = rand(24)
end
@oliyoung
oliyoung / gist:2844
Created July 28, 2008 06:02
backup database and copy it up to a server
#/usr/bin/ruby
require 'net/ftp'
@filename = "date-"+Time.now.strftime("%Y%m%d-%H%M")+".sql"
@db_user = "db6663"
@db_password = "PASSWORD"
@db_host = "my.database-server.com"
@database = "database"
@backupdir = "/path/to/backup/directory"
@oliyoung
oliyoung / gist:3547
Created August 1, 2008 00:16
dump database into usefully named sql file
mysqldump --user=USER --password=PASSWORD DATABASE > ~/`date +%Y-%m-%d_%H%M`.sql
@oliyoung
oliyoung / starling.sh
Created August 8, 2008 03:02
install, deploy and execute starling
sudo gem install starling memcache-client
sudo mkdir /var/spool/starling
sudo /usr/sbin/adduser --shell /sbin/nologin starling
sudo mkdir -p /var/spool/starling
sudo mkdir -p /var/run/starling
sudo chown starling:starling /var/spool/starling
sudo chown starling:starling /var/run/starling
sudo -u starling /usr/bin/starling --log /var/log/starling.log -v --queue_path /var/spool/starling --host 127.0.0.1 --port 15151 --pid /var/run/starling/starling.pid -d
/path/to/rails/app/script/workling_starling_client start
@oliyoung
oliyoung / gist:5198
Created August 13, 2008 05:23
take a design yaml document and convert it to spec files
desc "Push design.yml to spec files"
task :design do
require 'yaml'
specs = YAML::load_file './spec/design.yml'
specs.each_pair do |describe, specs_array|
spec_file = File.new("./spec/#{describe.downcase.gsub(/ /, '_')}_spec.rb", 'w')
spec_file.puts "
require File.dirname(__FILE__) + '/spec_helper'
describe \"#{describe}\" do
@oliyoung
oliyoung / RSpec Stories.rake
Created August 14, 2008 23:50
Rake tasks for creating and running rspec stories
namespace :stories do
desc "Create Story Scaffolds"
task :scaffold do
Dir.glob("./stories/*_story.txt").each do |file|
filename = File.basename(file, "txt").split("_")[0].downcase.gsub(/ /, '_')
File.open("./stories/#{filename}_story.rb", 'w') do |f|
f.puts "require File.dirname(__FILE__) + '/helper'\n"
f.puts "with_steps_for(:#{filename}) do"
f.puts "\ttell '#{filename}_story', :type => RailsStory"
f.puts "end"