This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 :-) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ... | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |