Skip to content

Instantly share code, notes, and snippets.

@curt
curt / uri_query_params.rb
Created February 9, 2010 02:04
Return URI query parameters as hash
class URI::Generic
def query_params
Hash[(query || '').split('&').collect{ |a| a.split('=') }.collect{ |b| [URI.unescape(b[0]), b[1].nil? ? nil : URI.unescape(b[1])] }]
end
alias_method :query_parameters, :query_params
end
@curt
curt / random_attributes.rb
Created February 9, 2010 14:41
Create unique random attributes in ActiveRecord
module RandomAttributes
def self.included(controller)
controller.send :extend, ClassMethods
controller.send :include, InstanceMethods
end
module ClassMethods
def random_attribute(attr_sym, key_length = 10)
cattr_accessor :random_attributes
validates_presence_of attr_sym
@curt
curt / minion_enqueue.rb
Created February 9, 2010 14:43
Minion enqueue_or_log in Rails
module Minion
def enqueue_or_log(key, args)
enqueue(key, args)
rescue
Rails.logger.error "Unable to enqueue '#{key}' with args '#{args.inspect}': #{$!}" if Rails.logger
end
end
@curt
curt / background_control.rb
Created February 9, 2010 14:49
Daemonize background process in Rails
#!/usr/bin/env ruby
#
# Usage: ruby background_control.rb [start|stop|restart]
#
require 'rubygems'
require 'daemons'
background_file = File.join(File.dirname(__FILE__), 'lib', 'background.rb')
@curt
curt / app_background.monitrc
Created February 9, 2010 16:05
Monit service entry for Rails background process
check process app_background
with pidfile "/path/to/app/shared/pids/background.pid"
start program = "/usr/bin/sudo -u user RAILS_ENV=production /usr/local/bin/ruby /path/to/app/current/background_control.rb start"
stop program = "/usr/bin/sudo -u user RAILS_ENV=production /usr/local/bin/ruby /path/to/app/current/background_control.rb stop"
@curt
curt / rails_schedules_hourly.crontab
Created February 9, 2010 16:16
Crontab entry for hourly Rails task
1 * * * * /usr/local/bin/rake -f /path/to/app/current/Rakefile RAILS_ENV=production schedules:hourly
@curt
curt / capistrano_monit_rails_background_restart.rb
Created February 9, 2010 17:54
Capistrano recipe for restarting Rails background process with Monit
namespace :background do
task :restart do
run "[ -f #{shared_path}/pids/background.pid ] && kill `cat #{shared_path}/pids/background.pid`"
sudo "monit start app_background"
end
end
after "deploy", "background:restart"
@curt
curt / email_valid.rb
Created March 18, 2010 15:46
Valid e-mail address in Ruby
require 'tmail'
def email_valid?(email)
TMail::Address.parse(email)
rescue TMail::SyntaxError
false
else
true
end
@curt
curt / README.markdown
Last active July 3, 2018 00:29
WordPress plugin that replaces http: with https: in any content containing the home URL wherever found in content requested via https.
@curt
curt / unzip-wordpress-into-different-folder.sh
Last active April 24, 2017 12:25
Download and install latest WordPress into a target folder other than `wordpress`. In this example, the target folder is called `blog`. This may be helpful if the target folder already exists and has files or subfolders. The folder `wordpress` must not already exist.
wget --no-check-certificate https://wordpress.org/latest.tar.gz
ln -s blog wordpress
tar -xzvf latest.tar.gz
unlink wordpress