Skip to content

Instantly share code, notes, and snippets.

View kalleth's full-sized avatar

Tom Russell kalleth

View GitHub Profile
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)
@kalleth
kalleth / vimrc.vim
Last active September 30, 2015 08:18
my vimrc
nnoremap <f2> :bnext<CR>
nnoremap <f1> :bprevious<CR>
set wildmode=list:longest,full "pretty finding
set noswapfile
set number
set smartindent
set tabstop=8
set shiftwidth=2
set expandtab
set softtabstop=2
@kalleth
kalleth / controllers.rb
Created May 17, 2012 09:28
Controllers for GreekFreak
class ApplicationController
private
def my_filter_method(myarg, data)
if myarg == "file"
@file = File.open(myarg, data)
elsif myarg == "url"
Net::HTTP.get(myarg, data)
end
@kalleth
kalleth / usage.rb
Created July 19, 2012 19:41
getting attrs
class MyClass
attr_accessor :at1, :at2, :at3
def initialize
@at1 = "one"
@at2 = "two"
@at3 = "three"
end
@kalleth
kalleth / classes.rb
Created October 20, 2012 00:11
hmm, polymorphism?
class DataCenter < ActiveRecord::Base
# has configuration_type, configuration_id attributes
def config
configuration_type.constantize.find(configuration_id)
end
def config=(config)
configuration_type = config.class.name
configuration_id = config.id
end