Skip to content

Instantly share code, notes, and snippets.

View kalleth's full-sized avatar

Tom Russell kalleth

View GitHub Profile
@kalleth
kalleth / simple_delegator_delegation_chain.rb
Created July 1, 2014 09:57
delegation_chain for SimpleDelegator objects to allow easy inspection of their delegation chain
class SimpleDelegator
def delegation_chain
[self].tap do |output|
object = self
while object.respond_to?("__getobj__")
object = object.__getobj__
output << object
end
end
end
@kalleth
kalleth / deploy.rb
Created December 24, 2014 15:00
Tag remote in capistrano
namespace :deploy do
before :restart, :tag do
on roles(:app) do
within "#{deploy_to}/repo" do
execute :git, "tag -f deployed-to-#{fetch(:stage)}"
begin
execute :git, "remote add upstream #{fetch(:repo_url)}"
rescue
# The remote might already exist. We don't care if it does.
end
@kalleth
kalleth / table_matcher.rb
Created May 1, 2015 13:58
matcher for table rows containing
require 'rspec/matchers'
module Suite
class Table < Struct.new(:table)
def rows
@rows ||= table.all('tbody tr').map do |tr|
tr.all('td').map(&:text)
end
end
end
require 'date'
require 'twitter'
require 'dotenv'
Dotenv.load
class TwitterSearch
def fetch(query)
client.search("#{query} since:#{Date.today}").collect do |tweet|
Tweet.new(tweet)
end
class Account < ActiveRecord::Base
has_many :users
accepts_nested_attributes_for :users
serialize :account #If you want to store the recurly account, you need to serialise the object
validates_presence_of :company, :on => :create, :message => "can't be blank"
# Before_create will be called once only
before_create :create_external_account
class Foo < ActiveRecord::Base
after_initialize :do_stuff
def do_stuff
self.parameter = "string" if self.parameter.nil?
end
end
@kalleth
kalleth / gist:1211109
Created September 12, 2011 12:05
PHP WTF
trussell@whopper:~/Projects/app/$ php -a
Interactive shell
php > $foo = "16.83";
php > echo $foo;
16.83
php > $bar = (float) $foo;
php > echo $bar;
16.83
php > $baz = $bar * 100;
@kalleth
kalleth / gist:1559867
Created January 4, 2012 12:37
new contactcontroller for Znow
class ContactController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new(params[:message])
if @message.valid?
Contact.contact(@message).deliver
@kalleth
kalleth / contact.rb
Created January 4, 2012 12:42
For Znow
# Contact Mailer
class Contact < ActionMailer::Base
#default :to => "contact@advicecapital.dk"
def contact(message, sender)
@message = message
@sender = sender
mail(
:from => sender,
:to => "znowm4n@gmail.com",
class CreateLoginRecords < ActiveRecord::Migration
def self.up
create_table :login_records do |t|
t.integer :user_id
t.datetime :logged_in
t.timestamps
end
add_index(:login_records, :user_id)
add_index(:login_records, :logged_in)