Skip to content

Instantly share code, notes, and snippets.

View coffeeaddict's full-sized avatar

Hartog C. de Mik coffeeaddict

View GitHub Profile
@coffeeaddict
coffeeaddict / game.rb
Created October 25, 2010 10:29
Bulls and Cows - an attempt to get a sane solution for http://rosettacode.org/wiki/Bulls_and_cows/Player
class Game
attr_reader :secret
def initialize
@secret = Secret.new
end
def console_play
bulls = -1
while bulls != 4
class A
has_many :abcees
has_many :bees, :through => :abcees
has_many :cees, :through => :abcees
end
class Bee
end
class Cee
@coffeeaddict
coffeeaddict / login_controller.rb
Created December 7, 2010 14:52
modular controller
class LoginController < ApplicationController
skip_before_filter :check_login
skip_before_filter :check_rights
include LoginMethods
end
> LoginController.action_methods
=> #<Set: { ... }> # only action methods from ApplicationController show up here
@coffeeaddict
coffeeaddict / rails_test
Created December 14, 2010 14:54
Test a unit/functional file or test with little effort
#!/usr/bin/env ruby
#
# = USAGE
# rails_test (f|functional|u|unit|p|performance|i|integration) test_case_name test_name
#
# = SYNOPSIS
#
# rails_test f customers
# # will run functional/customers_controller.rb
#
class SomeController < ApplicationController
before_filter :check_from_invoice
before_filter :session_is_empty, :only => [:create]
def check_from_invoice
if some_condition
skip_before_filter :session_is_empty
end
end
Master::Application.routes.draw do
# ... bunch of resources
# resource-less controllers
{ 'statistics' => %w(select_period filter drop_filter apply_filters
add_filter show get_filter_values
),
'warehouse' => %w(delivery delivery_with_po delivery_without_po
add_product_field shipment_csv_line shipment_put
shipment_csv
@coffeeaddict
coffeeaddict / guarded.rb
Created December 22, 2010 07:53
Kijk mama! Ik heb een infinite loop gemaakt!
# ...
def sema_key; "stock-observer-semaphores"; end
# make sure the stock off all coupled products is always equal
def update_coupled_stock(stock)
# get the current list of semaphores
semaphores = Rails.cache.read(sema_key) || []
# when this stocks is already being updated, leave now
@coffeeaddict
coffeeaddict / gist:906371
Created April 6, 2011 19:47
callbacks dont invalidate records
class Member < ActiveRecord::Base
SALT = "some lengthy secret"
before_create :hash_password
# in rails 2 this would break the callback chain and set the error message
# in rails 3 it doesnt
#
def hash_password
if password == password_confirm
@coffeeaddict
coffeeaddict / logger_mp.rb
Created April 12, 2011 08:17
Monkey patch to get strawberry flavored errors
module ActiveSupport
# log errors in red
class BufferedLogger
alias_method :orig_error, :error
def error msg
if Rails.env != :production
msg = "\e[1;31m#{msg}\e[0m"
end
orig_error msg
#!/bin/sh
# shell scripting is easy.
#
# just do what you would normaly do on the shell to get those directories
# present
mkdir tmp
mkdir -p log/pids