Skip to content

Instantly share code, notes, and snippets.

View kalleth's full-sized avatar

Tom Russell kalleth

View GitHub Profile
@kalleth
kalleth / idea.rb
Created January 15, 2013 11:37
valids
field :name
accessible
validates :presence do
message "please provide a name"
end
end
field :email
accessible
validates :format do
@kalleth
kalleth / checking.rb
Created November 20, 2012 11:14
Refactor attempt following SRP
def do_bulk_creation # Controller action called from request
prize = Prize.find_by_id(params[:code][:prize_id])
codes = params[:code][:codes].split("\r\n").each{ |c| c.strip }
if bulk_creation_valid?(prize, codes)
perform_bulk_creation(prize, codes)
else
set_bulk_creation_error(prize, codes)
@bulk = Code.new(params[:code])
render :action => :bulk_create
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
@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 / 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 / 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
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 / 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",
@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 / 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;