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 / 20110210144658_create_entity_jobs.rb
Created July 13, 2011 09:06
Get (some) control over your Delayed::Job's
class CreateEntityJobs < ActiveRecord::Migration
def self.up
create_table :entity_jobs do |t|
t.references :entity, :polymorphic => true
t.references :delayed_job
t.timestamps
end
add_index(:entity_jobs, :entity_id)
@coffeeaddict
coffeeaddict / alL_any.rb
Created July 11, 2011 07:16
Didnt know
[2,4].any? { |n| n % 2 == 0 }
[2,4].all? { |n| n % 2 == 0 }
[2,4].empty? { |n| n % 2 == 0 }
# yield: true, true, false
# I never knew or suspected any? or empty? could be given a block - this makes live pleasant :-)
@coffeeaddict
coffeeaddict / humidex.rb
Created June 28, 2011 13:19
Calculate the humidex
#!/usr/bin/env ruby
# estimate the dewpoint temperature
def dew_point(temp, rv)
e = 6.1121 * (10 ** (7.502 * temp / (237.7 + temp)))
p = (rv * e) / 100.0
dp = (-430.22 + 237.7 * Math.log(p))/(-(Math.log(p)) + 19.08);
end
# estimate the wet bulb temperature
@coffeeaddict
coffeeaddict / thing.rb
Created May 31, 2011 07:28
Scope for each state in the pluginaweek/state_machine
class Thing < ActiveRecord::Base
state_machine :initial => :pending do
# when published, the thing has to be removed
after_transition :on => :done_publishing, :do => :destroy
event :publish do
transition :pending => :publishing, :unless => :not_ready?
end
event :done_publishing do
@coffeeaddict
coffeeaddict / factories.rb
Created May 27, 2011 13:50
Factories with randomness
require 'rufus-mnemo'
Factory.sequence :word do |n|
Rufus::Mnemo::from_integer(10000000 + n)
end
Factory.define :company do |r|
r.name { Factory.next :word }
end
#!/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
@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
@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 / 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
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