acts_as_geocodable 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,60 @@
1
+ ActiveRecord::Schema.define(:version => 1) do
2
+
3
+ create_table "geocodes", :force => true do |t|
4
+ t.column "latitude", :decimal, :precision => 15, :scale => 12
5
+ t.column "longitude", :decimal, :precision => 15, :scale => 12
6
+ t.column "query", :string
7
+ t.column "street", :string
8
+ t.column "locality", :string
9
+ t.column "region", :string
10
+ t.column "postal_code", :string
11
+ t.column "country", :string
12
+ end
13
+
14
+ add_index "geocodes", ["query"], :name => "geocodes_query_index", :unique => true
15
+ add_index "geocodes", ["latitude"], :name => "geocodes_latitude_index"
16
+ add_index "geocodes", ["longitude"], :name => "geocodes_longitude_index"
17
+
18
+ create_table "geocodings", :force => true do |t|
19
+ t.column "geocodable_id", :integer
20
+ t.column "geocode_id", :integer
21
+ t.column "geocodable_type", :string
22
+ end
23
+
24
+ add_index "geocodings", ["geocodable_id"], :name => "geocodings_geocodable_id_index"
25
+ add_index "geocodings", ["geocode_id"], :name => "geocodings_geocode_id_index"
26
+ add_index "geocodings", ["geocodable_type"], :name => "geocodings_geocodable_type_index"
27
+
28
+ create_table "vacations", :force => true do |t|
29
+ t.column "name", :string
30
+ t.column "street", :string
31
+ t.column "locality", :string
32
+ t.column "region", :string
33
+ t.column "postal_code", :string
34
+ t.column "city_id", :integer
35
+ end
36
+
37
+ create_table "validated_vacations", :force => true do |t|
38
+ t.column "name", :string
39
+ t.column "street", :string
40
+ t.column "locality", :string
41
+ t.column "region", :string
42
+ t.column "postal_code", :string
43
+ end
44
+
45
+ create_table "address_blob_vacations", :force => true do |t|
46
+ t.column "name", :string
47
+ t.column "address", :string
48
+ end
49
+
50
+ create_table "callback_locations", :force => true do |t|
51
+ t.column "name", :string
52
+ t.column "address", :string
53
+ end
54
+
55
+ create_table "cities", :force => true do |t|
56
+ t.column "name", :string
57
+ t.column "zip", :string
58
+ end
59
+
60
+ end
@@ -0,0 +1,12 @@
1
+ holland:
2
+ id: 1
3
+ name: Holland
4
+ zip: 49423
5
+ chicago:
6
+ id: 2
7
+ name: Chicago
8
+ zip:
9
+ nowhere:
10
+ id: 3
11
+ name: Nowhere
12
+ zip: ""
@@ -0,0 +1,51 @@
1
+ saugatuck_geocode:
2
+ id: 1
3
+ latitude: 42.654781
4
+ longitude: -86.200722
5
+ query: "Saugatuck, MI"
6
+ locality: Saugatuck
7
+ region: MI
8
+ white_house_geocode:
9
+ id: 2
10
+ latitude: 38.898748
11
+ longitude: -77.037684
12
+ query: "1600 Pennsylvania Ave NW\nWashington, DC 20502"
13
+ street: 1600 Pennsylvania Ave NW
14
+ locality: Washington
15
+ region: DC
16
+ postal_code: 20502
17
+ chicago_geocode:
18
+ id: 3
19
+ latitude: 41.85
20
+ longitude: -87.65
21
+ query: Chicago, IL
22
+ locality: Chicago
23
+ region: IL
24
+ postal_code:
25
+ douglas:
26
+ id: 4
27
+ query: "49406"
28
+ longitude: -86.2005
29
+ latitude: 42.6433
30
+ locality: Douglas
31
+ region: MI
32
+ postal_code: 49406
33
+ country: US
34
+ holland:
35
+ id: 5
36
+ query: Holland, MI
37
+ longitude: -86.109039
38
+ latitude: 42.787567
39
+ locality: Holland
40
+ postal_code:
41
+ region: MI
42
+ country: US
43
+ beverly_hills:
44
+ id: 6
45
+ query: Beverly Hills, 90210
46
+ latitude: 34.092400000000
47
+ longitude: -118.409800000000
48
+ locality: Beverly Hills
49
+ region: CA
50
+ country: US
51
+ postal_code: 90210
@@ -0,0 +1,15 @@
1
+ saugatuck_geocoding:
2
+ id: 1
3
+ geocodable_id: 1
4
+ geocode_id: 1
5
+ geocodable_type: Vacation
6
+ white_house_geocoding:
7
+ id: 2
8
+ geocodable_id: 2
9
+ geocode_id: 2
10
+ geocodable_type: Vacation
11
+ chicago_geocoding:
12
+ id: 3
13
+ geocodable_id: 1
14
+ geocode_id: 3
15
+ geocodable_type: City
@@ -0,0 +1,15 @@
1
+ saugatuck:
2
+ id: 1
3
+ name: Saugatuck, Michigan
4
+ whitehouse:
5
+ id: 2
6
+ name: The White House
7
+ street: 1600 Pennsylvania Ave NW
8
+ locality: Washington
9
+ region: DC
10
+ postal_code: 20502
11
+ mystery_spot:
12
+ id: 3
13
+ name: The Mystery Spot
14
+ street: 150 Martin Lake Rd.
15
+ postal_code: 49781
@@ -0,0 +1,97 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class GeocodeTest < ActiveSupport::TestCase
4
+ fixtures :geocodes
5
+
6
+ context 'distance_to' do
7
+ setup do
8
+ @washington_dc = geocodes(:white_house_geocode)
9
+ @chicago = geocodes(:chicago_geocode)
10
+ end
11
+
12
+ should 'properly calculate distance in default units' do
13
+ @washington_dc.distance_to(@chicago).should be_close(594.820, 1.0)
14
+ end
15
+
16
+ should 'properly calculate distance in default miles' do
17
+ @washington_dc.distance_to(@chicago, :miles).should be_close(594.820, 1.0)
18
+ end
19
+
20
+ should 'properly calculate distance in default kilometers' do
21
+ @washington_dc.distance_to(@chicago, :kilometers).should be_close(957.275, 1.0)
22
+ end
23
+
24
+ should 'return nil with invalid geocode' do
25
+ @chicago.distance_to(Geocode.new).should be(nil)
26
+ @chicago.distance_to(nil).should be(nil)
27
+ end
28
+ end
29
+
30
+ context 'find_or_create_by_query' do
31
+ should 'finds existing geocode' do
32
+ Geocode.find_or_create_by_query('Holland, MI').should == geocodes(:holland)
33
+ end
34
+ end
35
+
36
+ context "find_or_create_by_location" do
37
+ should "find existing location" do
38
+ location = Graticule::Location.new(:postal_code => "20502",
39
+ :street => "1600 Pennsylvania Ave NW",
40
+ :locality => "Washington",
41
+ :region => "DC")
42
+ Geocode.find_or_create_by_location(location).should == geocodes(:white_house_geocode)
43
+ end
44
+
45
+ should "return nil when location can't be geocoded" do
46
+ Geocode.geocoder.expects(:locate).raises(Graticule::Error)
47
+ assert_no_difference 'Geocode.count' do
48
+ Geocode.find_or_create_by_location(Graticule::Location.new(:street => 'FAIL!')).should be(nil)
49
+ end
50
+ end
51
+
52
+ end
53
+
54
+ context 'coordinates' do
55
+ should 'return longitude and latitude' do
56
+ geocodes(:saugatuck_geocode).coordinates.should == "-86.200722,42.654781"
57
+ end
58
+ end
59
+
60
+ context 'to_s' do
61
+ should 'return the coordinates' do
62
+ geocodes(:saugatuck_geocode).to_s.should == geocodes(:saugatuck_geocode).coordinates
63
+ end
64
+ end
65
+
66
+ context 'geocoded?' do
67
+ should 'be true with both a latitude and a longitude' do
68
+ assert Geocode.new(:latitude => 1, :longitude => 1).geocoded?
69
+ end
70
+
71
+ should 'be false when missing coordinates' do
72
+ assert !Geocode.new.geocoded?
73
+ end
74
+
75
+ should 'be false when missing a latitude' do
76
+ assert !Geocode.new(:longitude => 1).geocoded?
77
+ end
78
+
79
+ should 'be false when missing a longitude' do
80
+ assert !Geocode.new(:latitude => 1).geocoded?
81
+ end
82
+ end
83
+
84
+ end
85
+
86
+ __END__
87
+
88
+
89
+
90
+ def test_to_location
91
+ location = geocodes(:white_house_geocode).to_location
92
+
93
+ assert_kind_of Graticule::Location, location
94
+ [:street, :locality, :region, :postal_code, :latitude, :longitude].each do |attr|
95
+ assert_not_nil location.send(attr)
96
+ end
97
+ end
@@ -0,0 +1,46 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+ plugin_test_dir = File.dirname(__FILE__)
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'multi_rails_init'
7
+ require 'active_support'
8
+ require 'active_record'
9
+ require 'action_controller'
10
+ require 'active_record/fixtures'
11
+ require 'shoulda'
12
+ require 'matchy'
13
+ require 'mocha'
14
+
15
+ require plugin_test_dir + '/../init.rb'
16
+
17
+ ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log")
18
+
19
+ ActiveRecord::Base.configurations = YAML::load(IO.read(plugin_test_dir + "/db/database.yml"))
20
+ ActiveRecord::Base.establish_connection(ENV["DB"] || "mysql")
21
+ ActiveRecord::Migration.verbose = false
22
+ load(File.join(plugin_test_dir, "db", "schema.rb"))
23
+
24
+ Geocode.geocoder ||= Graticule.service(:bogus).new
25
+
26
+ class ActiveSupport::TestCase #:nodoc:
27
+ include ActiveRecord::TestFixtures if ActiveRecord.const_defined?(:TestFixtures)
28
+
29
+ self.fixture_path = File.dirname(__FILE__) + "/fixtures/"
30
+
31
+ # Turn off transactional fixtures if you're working with MyISAM tables in MySQL
32
+ self.use_transactional_fixtures = true
33
+
34
+ # Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david)
35
+ self.use_instantiated_fixtures = false
36
+
37
+ def assert_geocode_result(result)
38
+ assert_not_nil result
39
+ assert result.latitude.is_a?(BigDecimal) || result.latitude.is_a?(Float), "latitude is a #{result.latitude.class.name}"
40
+ assert result.longitude.is_a?(BigDecimal) || result.longitude.is_a?(Float)
41
+
42
+ # Depending on the geocoder, we'll get slightly different results
43
+ assert_in_delta 42.787567, result.latitude, 0.001
44
+ assert_in_delta -86.109039, result.longitude, 0.001
45
+ end
46
+ end
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_geocodable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Morrison
8
+ - Brandon Keepers
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-10-21 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Simple geocoding for Rails ActiveRecord models. See the README for more details.
18
+ email: info@collectiveidea.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - README
25
+ files:
26
+ - .gitignore
27
+ - CHANGELOG
28
+ - MIT-LICENSE
29
+ - README
30
+ - Rakefile
31
+ - VERSION
32
+ - about.yml
33
+ - acts_as_geocodable.gemspec
34
+ - generators/geocodable_migration/USAGE
35
+ - generators/geocodable_migration/geocodable_migration_generator.rb
36
+ - generators/geocodable_migration/templates/migration.rb
37
+ - install.rb
38
+ - lib/acts_as_geocodable.rb
39
+ - lib/acts_as_geocodable/geocode.rb
40
+ - lib/acts_as_geocodable/geocoding.rb
41
+ - lib/acts_as_geocodable/remote_location.rb
42
+ - lib/acts_as_geocodable/tasks/acts_as_geocodable_tasks.rake
43
+ - rails/init.rb
44
+ - test/acts_as_geocodable_test.rb
45
+ - test/db/database.yml
46
+ - test/db/schema.rb
47
+ - test/fixtures/cities.yml
48
+ - test/fixtures/geocodes.yml
49
+ - test/fixtures/geocodings.yml
50
+ - test/fixtures/vacations.yml
51
+ - test/geocode_test.rb
52
+ - test/test_helper.rb
53
+ - uninstall.rb
54
+ has_rdoc: true
55
+ homepage: http://github.com/collectiveidea/acts_as_geocodable
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --charset=UTF-8
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.3.5
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Simple geocoding for Rails ActiveRecord models
82
+ test_files:
83
+ - test/acts_as_geocodable_test.rb
84
+ - test/db/schema.rb
85
+ - test/geocode_test.rb
86
+ - test/test_helper.rb