has_a_location – the Latitude Longitude – Rails Plugin

Easy handling of latitude/longitude information, build on top of acts_as_mappable (aka geokit).

has_a_location Installation instructions

Usage

class User < ActiveRecord::Base
  has_a_location
end

@user.location = [11.101,22.121]# latitude , longitude
@user.in_radius(100) #find in 100 miles radius

#this location will not be stored since it is the "default location"
@user.location = [0,0] 

show_map if @user.location

All options and Readme

The Black White Tree Testing Method

snow tree by plain ethos

TDD often is well understood, but seldom put to good use. Spikes grow larger, hard to test aspects are skipped and sooner or later your test coverage looks like this.

.

Therefore i want to show you the Black-White-Tree testing method, which is easy to adopt and results in full C1(path) coverage with easy to maintain, independent tests.

.

.

ice tree by serendipitypeace2007

ice tree by serendipitypeace2007

The principle is simple:

When designing a new method build Black-Box tests for it, often 2-3 are sufficient if they exersice all paths within this method(not necessarily its sub-methods), represented by the trunk and the black branches.

describe :price do
  it "sums prices and applies discounts" do
    Order.new(:items=>items,:discount=>20).price.should == 22.5
  end
  it "costs nothing if it is free" do
    Order.new(:items=>items,:free=>true,:discount=>10).price.should == 0
  end
end

Then write White Box tests, for the public method, mocking everything out with forged return values to verify that every method is called and the call-results are used logically.

describe :price do
  ...
  it "uses sum_price and apply_discount" do
    order = Order.new(:items=>items,:discount=>10)
    order.expects(:sum_prices).returns 100
    order.expects(:apply_discount).with(10,100).returns 20
    order.price.should == 20
  end
end

Then build the method, making all White Box and some of the Black Box tests pass.
Repeat for every sub-method.

Numbers for Humans – Humanize for Numeric

number_with_precision(result.round(2)) — BE GONE!

Usage
1.humanize == “1”
1000000.humanize == “1.000.000”
1000.12345.humanize == “1.000,12”

Install

#config/initializers/numeric_humanize.rb
class Numeric
  def humanize(rounding=2,delimiter=',',separator='.')
    value = respond_to?(:round_with_precision) ? round(rounding) : self
    
    #see number with delimeter
    parts = value.to_s.split('.')
    parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
    parts.join separator
  end
end