Skip to content

Instantly share code, notes, and snippets.

View spickermann's full-sized avatar
💻
18 years of experience with Ruby on Rails

Martin Spickermann spickermann

💻
18 years of experience with Ruby on Rails
View GitHub Profile
@spickermann
spickermann / resque.rb
Created June 8, 2013 07:21
bash script to start and stop resque workers.
#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
require 'optparse'
require 'timeout'
require 'ostruct'
USAGE = "usage: script/resque command [options]"
COMMANDS = %w( start status stop )
TIMEOUT = 60
@spickermann
spickermann / dirty.rb
Last active December 17, 2015 21:49
Adds support for dirty attributes to a couchbase model. Only selected attributes will act dirty...
# Adds support for dirty attributes to a couchbase model.
#
# class Foo < Couchbase::Model
# include CouchbaseExt::Dirty
# acts_as_dirty_attribute :bar, :blub # only selected attributes will be dirty...
module CouchbaseExt
module Dirty
DIRTY_SUFFIXES = ['_changed?', '_change', '_will_change!', '_was']
@spickermann
spickermann / gist:4996428
Created February 20, 2013 15:38
CouchbaseModel#count Hässlich wie die Nacht...
class Counter < Struct.new(:value)
def self.wrap(bucket, result)
new(result['value'])
end
end
view :count_all, :include_docs => false, :wrapper_class => Counter
def self.count
count_all.first.present? ? count_all.first.value : 0
end
@spickermann
spickermann / disable_users.rake
Last active December 13, 2015 23:48
Identifiy and disable users that are in the list of users with invalid emails and do not have any purchases and are not active.
namespace :disable_users do
desc 'Generates a csv file containing candidates for deletion'
task :determine_candidates => :environment do
logger = Logger.new('tmp/candidates.csv')
users = 0
candidates = 0
{ :invalid => :invalid_emails, :dns_err => :unresolvable_emails }.each_pair do |reason, input_file|
# 0. Make sure you have Ruby 1.9.3 installed, and optionally RVM and PostgreSQL
# 0.2 If you are on the Mac, make sure you have a c compiler by installing XCode Command Line Tools or gcc4.2 with homebrew
# https://github.com/mxcl/homebrew/wiki/Custom-GCC-and-cross-compilers
# 0.5 Make sure you have bundler version ~> 1.2 as Rails depends on it
gem install bundler
# 1. Get edge Rails source (master branch)
git clone https://github.com/rails/rails.git
@spickermann
spickermann / validate_emails.rake
Last active December 13, 2015 16:49
rake task that generates a csv file containing invalid or unresolvable email addresses
desc 'Generates a csv file containing invalid or unresolvable email addresses'
task :check_emails => :environment do
logger = Logger.new('log/faulty_emails.csv')
Account.find_each do |account|
identifier = "#{account.uuid},#{[account.current_sign_in_at, account.updated_at].compact.max},#{account.email}"
if !EmailUtils::Validator.valid?(account.email)
logger.warn("invalid,#{identifier}")
#
# RFC822 Email Address Regex
# --------------------------
#
# Originally written by Cal Henderson
# c.f. http://iamcal.com/publish/articles/php/parsing_email/
#
# Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.
#
# Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
module ModelExt
module MasterSlaveDb
# This module allows you to send db queries directly to the slave database.
# Please do not use this on the user frontend at the moment - administration interface only.
#
# 1) The slave is weaker (CPU, RAM) than the master.
# 2) The slave will be down around lunch time for backup reasons.
#
# Usage:
@spickermann
spickermann / 19700101000001_set_database_defaults.rb
Last active February 12, 2025 06:24
Ruby On Rails - default charset migration
class SetDatabaseDefaults < ActiveRecord::Migration
def up
case ActiveRecord::Base.connection.adapter_name
when "MySQL", "Mysql2"
execute "ALTER DATABASE #{current_database} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci"
when "SQLite"
# do nothing since SQLite does not support changing the database encoding and only supports unicode charsets
when "PostgreSQL"
# do nothing since PostgreSQL does not support changing the database encoding