acts_as_geocodable 2.0.3 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,84 @@
1
+ require "spec_helper"
2
+
3
+ describe Geocode do
4
+ describe "distance_to" do
5
+ before do
6
+ @washington_dc = FactoryGirl.create(:white_house_geocode)
7
+ @chicago = FactoryGirl.create(:chicago_geocode)
8
+ end
9
+
10
+ it "should properly calculate distance in default units" do
11
+ expect(@washington_dc.distance_to(@chicago)).to be_within(1.0).of(594.820)
12
+ end
13
+
14
+ it "should properly calculate distance in default miles" do
15
+ expect(@washington_dc.distance_to(@chicago, :miles)).to be_within(1.0).of(594.820)
16
+ end
17
+
18
+ it "should properly calculate distance in default kilometers" do
19
+ expect(@washington_dc.distance_to(@chicago, :kilometers)).to be_within(1.0).of(957.275)
20
+ end
21
+
22
+ it "should return nil with invalid geocode" do
23
+ expect(@chicago.distance_to(Geocode.new)).to be_nil
24
+ expect(@chicago.distance_to(nil)).to be_nil
25
+ end
26
+ end
27
+
28
+ describe "find_or_create_by_query" do
29
+ it "should finds existing geocode" do
30
+ existing = FactoryGirl.create(:holland_geocode)
31
+ expect(Geocode.find_or_create_by_query("Holland, MI")).to eq(existing)
32
+ end
33
+ end
34
+
35
+ describe "find_or_create_by_location" do
36
+ it "should find existing location" do
37
+ existing = FactoryGirl.create(:white_house_geocode)
38
+ location = Graticule::Location.new(postal_code: "20502",
39
+ street: "1600 Pennsylvania Ave NW",
40
+ locality: "Washington",
41
+ region: "DC")
42
+ expect(Geocode.find_or_create_by_location(location)).to eq(existing)
43
+ end
44
+
45
+ it "should return nil when location can't be geocoded" do
46
+ expect(Geocode.geocoder).to receive(:locate).and_raise(Graticule::Error)
47
+ expect {
48
+ expect(Geocode.find_or_create_by_location(Graticule::Location.new(street: "FAIL!"))).to be_nil
49
+ }.not_to change { Geocode.count }
50
+ end
51
+ end
52
+
53
+ describe "coordinates" do
54
+ it "should return longitude and latitude" do
55
+ geocode = FactoryGirl.create(:saugatuck_geocode)
56
+ expect(geocode.coordinates).to eq("-86.200722,42.654781")
57
+ end
58
+ end
59
+
60
+ describe "to_s" do
61
+ it "should return the coordinates" do
62
+ geocode = FactoryGirl.create(:saugatuck_geocode)
63
+ expect(geocode.to_s).to eq(geocode.coordinates)
64
+ end
65
+ end
66
+
67
+ describe "geocoded?" do
68
+ it "should be true with both a latitude and a longitude" do
69
+ expect(Geocode.new(latitude: 1, longitude: 1)).to be_geocoded
70
+ end
71
+
72
+ it "should be false when missing coordinates" do
73
+ expect(Geocode.new).not_to be_geocoded
74
+ end
75
+
76
+ it "should be false when missing a latitude" do
77
+ expect(Geocode.new(latitude: 1)).not_to be_geocoded
78
+ end
79
+
80
+ it "should be false when missing a longitude" do
81
+ expect(Geocode.new(latitude: 1)).not_to be_geocoded
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,31 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
4
+ require "acts_as_geocodable"
5
+
6
+ require "bundler"
7
+ Bundler.require(:test)
8
+
9
+ plugin_test_dir = File.dirname(__FILE__)
10
+ ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log")
11
+ ActiveRecord::Base.configurations = YAML::load(IO.read(plugin_test_dir + "/db/database.yml"))
12
+ ActiveRecord::Base.establish_connection((ENV["DB"] || "mysql").to_sym)
13
+ ActiveRecord::Migration.verbose = false
14
+ load(File.join(plugin_test_dir, "db", "schema.rb"))
15
+
16
+ require "support/geocoder"
17
+ require "support/models"
18
+ require "support/factories"
19
+
20
+ RSpec.configure do |config|
21
+ # Use database cleaner to remove factories
22
+ config.before(:suite) do
23
+ DatabaseCleaner.strategy = :truncation
24
+ end
25
+ config.before(:each) do
26
+ DatabaseCleaner.start
27
+ end
28
+ config.after(:each) do
29
+ DatabaseCleaner.clean
30
+ end
31
+ end
@@ -0,0 +1,110 @@
1
+ FactoryGirl.define do
2
+ factory :chicago, class: City do
3
+ zip ""
4
+ name "Chicago"
5
+ end
6
+
7
+ factory :holland, class: City do
8
+ zip 49423
9
+ name "Holland"
10
+ end
11
+
12
+ factory :nowhere, class: City do
13
+ zip ""
14
+ name "Nowhere"
15
+ end
16
+
17
+ factory :chicago_geocode, class: Geocode do
18
+ query "Chicago, IL"
19
+ longitude(-87.65)
20
+ locality "Chicago"
21
+ postal_code ""
22
+ latitude 41.85
23
+ region "IL"
24
+ end
25
+
26
+ factory :white_house_geocode, class: Geocode do
27
+ query "1600 Pennsylvania Ave NW\nWashington, DC 20502"
28
+ longitude(-77.037684)
29
+ locality "Washington"
30
+ street "1600 Pennsylvania Ave NW"
31
+ postal_code 20502
32
+ latitude 38.898748
33
+ region "DC"
34
+ end
35
+
36
+ factory :saugatuck_geocode, class: Geocode do
37
+ query "Saugatuck, MI"
38
+ longitude(-86.200722)
39
+ locality "Saugatuck"
40
+ latitude 42.654781
41
+ region "MI"
42
+ end
43
+
44
+ factory :douglas_geocode, class: Geocode do
45
+ query 49406
46
+ longitude(-86.2005)
47
+ locality "Douglas"
48
+ postal_code 49406
49
+ country "US"
50
+ latitude 42.6433
51
+ region "MI"
52
+ end
53
+
54
+ factory :beverly_hills, class: Geocode do
55
+ query "Beverly Hills, 90210"
56
+ longitude(-118.4098)
57
+ locality "Beverly Hills"
58
+ postal_code 90210
59
+ country "US"
60
+ latitude 34.0924
61
+ region "CA"
62
+ end
63
+
64
+ factory :holland_geocode, class: Geocode do
65
+ query "Holland, MI"
66
+ longitude(-86.109039)
67
+ locality "Holland"
68
+ postal_code ""
69
+ country "US"
70
+ latitude 42.787567
71
+ region "MI"
72
+ end
73
+
74
+ factory :chicago_geocoding, class: Geocoding do
75
+ geocode_id 3
76
+ geocodable_id 1
77
+ geocodable_type "City"
78
+ end
79
+
80
+ factory :white_house_geocoding, class: Geocoding do
81
+ geocode_id 2
82
+ geocodable_id 2
83
+ geocodable_type "Vacation"
84
+ end
85
+
86
+ factory :saugatuck_geocoding, class: Geocoding do
87
+ geocode
88
+ geocodable
89
+ end
90
+
91
+ factory :saugatuck, class: Vacation do
92
+ name "Saugatuck, Michigan"
93
+ locality "Saugatuck"
94
+ region "MI"
95
+ end
96
+
97
+ factory :whitehouse, class: Vacation do
98
+ locality "Washington"
99
+ street "1600 Pennsylvania Ave NW"
100
+ postal_code 20502
101
+ name "The White House"
102
+ region "DC"
103
+ end
104
+
105
+ factory :mystery_spot, class: Vacation do
106
+ street "150 Martin Lake Rd."
107
+ postal_code 49781
108
+ name "The Mystery Spot"
109
+ end
110
+ end
@@ -0,0 +1,52 @@
1
+ class Graticule::Geocoder::Canned
2
+ class_attribute :responses
3
+ self.responses = {}
4
+ class_attribute :default
5
+
6
+ def locate(query)
7
+ location = responses[query.to_s.strip]
8
+ raise %Q{No Location for query: "#{query.to_s.inspect}" Add it to spec/support/geocoder.rb} unless location
9
+ location
10
+ end
11
+ end
12
+
13
+ Geocode.geocoder = Graticule::Geocoder::Canned.new
14
+
15
+ saugatuck = Graticule::Location.new(
16
+ locality: "Saugatuck",
17
+ region: "MI",
18
+ country: "USA",
19
+ precision: :street,
20
+ latitude: 42.654781,
21
+ longitude: -86.200722,
22
+ postal_code: 49406
23
+ )
24
+
25
+ sf = Graticule::Location.new(
26
+ locality: "San Francisco",
27
+ region: "CA",
28
+ country: "USA",
29
+ precision: :street,
30
+ latitude: 37.775206,
31
+ longitude: -122.419209,
32
+ postal_code: 94110
33
+ )
34
+
35
+ Geocode.geocoder.responses = {
36
+ "San Francisco" => sf,
37
+ "San Francisco, CA" => sf,
38
+
39
+ "49406" => saugatuck,
40
+ "Saugatuck, MI" => saugatuck,
41
+ "Saugatuck, MI 49406" => saugatuck,
42
+
43
+ "1600 Pennsylvania Ave NW\nWashington, DC 20502" => Graticule::Location.new(
44
+ locality: "Washington",
45
+ region: "DC",
46
+ country: "USA",
47
+ precision: :street,
48
+ latitude: 38.898748,
49
+ longitude: -77.037684,
50
+ postal_code: "20502"
51
+ )
52
+ }
@@ -0,0 +1,35 @@
1
+ class Vacation < ActiveRecord::Base
2
+ acts_as_geocodable normalize_address: true
3
+ belongs_to :nearest_city, class_name: "City", foreign_key: "city_id"
4
+ end
5
+
6
+ class Staycation < ActiveRecord::Base
7
+ self.table_name = "vacations"
8
+
9
+ acts_as_geocodable
10
+ validates_as_geocodable(allow_nil: false) do |geocode|
11
+ ["USA", "US"].include?(geocode.country)
12
+ end
13
+ end
14
+
15
+ class City < ActiveRecord::Base
16
+ acts_as_geocodable address: { postal_code: :zip }
17
+ end
18
+
19
+ class ValidatedVacation < ActiveRecord::Base
20
+ acts_as_geocodable
21
+ validates_as_geocodable
22
+ end
23
+
24
+ class AddressBlobVacation < ActiveRecord::Base
25
+ acts_as_geocodable address: :address, normalize_address: true
26
+ end
27
+
28
+ class CallbackLocation < ActiveRecord::Base
29
+ acts_as_geocodable address: :address
30
+ set_callback :geocoding, :after, :done_geocoding
31
+
32
+ def done_geocoding
33
+ true
34
+ end
35
+ end
metadata CHANGED
@@ -1,92 +1,147 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: acts_as_geocodable
3
- version: !ruby/object:Gem::Version
4
- hash: 9
5
- prerelease:
6
- segments:
7
- - 2
8
- - 0
9
- - 3
10
- version: 2.0.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Daniel Morrison
14
8
  - Brandon Keepers
15
9
  - Brian Ryckbost
16
10
  autorequire:
17
11
  bindir: bin
18
12
  cert_chain: []
19
-
20
- date: 2011-11-15 00:00:00 Z
21
- dependencies:
22
- - !ruby/object:Gem::Dependency
23
- version_requirements: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 15
29
- segments:
30
- - 2
31
- - 0
32
- - 0
33
- version: 2.0.0
34
- requirement: *id001
13
+ date: 2014-06-10 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: graticule
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
35
22
  type: :runtime
36
23
  prerelease: false
37
- name: graticule
38
- description: Simple geocoding for Rails ActiveRecord models. See the README for more details.
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ version: '2.0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rails
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '3'
36
+ - - <
37
+ - !ruby/object:Gem::Version
38
+ version: '4.2'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '3'
46
+ - - <
47
+ - !ruby/object:Gem::Version
48
+ version: '4.2'
49
+ - !ruby/object:Gem::Dependency
50
+ name: bundler
51
+ requirement: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: '1.6'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '1.6'
63
+ - !ruby/object:Gem::Dependency
64
+ name: rake
65
+ requirement: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '10.3'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: '10.3'
77
+ description: Simple geocoding for Active Record models. See the README for more details.
39
78
  email: info@collectiveidea.com
40
79
  executables: []
41
-
42
80
  extensions: []
43
-
44
81
  extra_rdoc_files: []
45
-
46
- files:
82
+ files:
83
+ - .gitignore
84
+ - .rspec
85
+ - .travis.yml
86
+ - CHANGELOG.md
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - acts_as_geocodable.gemspec
92
+ - gemfiles/rails30.gemfile
93
+ - gemfiles/rails31.gemfile
94
+ - gemfiles/rails32.gemfile
95
+ - gemfiles/rails40.gemfile
96
+ - gemfiles/rails41.gemfile
97
+ - lib/acts_as_geocodable.rb
47
98
  - lib/acts_as_geocodable/geocode.rb
48
99
  - lib/acts_as_geocodable/geocoding.rb
49
100
  - lib/acts_as_geocodable/remote_location.rb
50
101
  - lib/acts_as_geocodable/version.rb
51
- - lib/acts_as_geocodable.rb
102
+ - lib/generators/acts_as_geocodable/USAGE
52
103
  - lib/generators/acts_as_geocodable/acts_as_geocodable_generator.rb
53
104
  - lib/generators/acts_as_geocodable/templates/migration.rb
54
- - lib/generators/acts_as_geocodable/USAGE
55
- - CHANGELOG
56
- - MIT-LICENSE
57
- - README.textile
58
- homepage: http://github.com/collectiveidea/acts_as_geocodable
59
- licenses: []
60
-
105
+ - spec/acts_as_geocodable_generator_spec.rb
106
+ - spec/acts_as_geocodable_spec.rb
107
+ - spec/db/database.yml.example
108
+ - spec/db/schema.rb
109
+ - spec/geocode_spec.rb
110
+ - spec/spec_helper.rb
111
+ - spec/support/factories.rb
112
+ - spec/support/geocoder.rb
113
+ - spec/support/models.rb
114
+ homepage: https://github.com/collectiveidea/acts_as_geocodable
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
61
118
  post_install_message:
62
- rdoc_options:
63
- - --charset=UTF-8
64
- require_paths:
119
+ rdoc_options: []
120
+ require_paths:
65
121
  - lib
66
- required_ruby_version: !ruby/object:Gem::Requirement
67
- none: false
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- hash: 3
72
- segments:
73
- - 0
74
- version: "0"
75
- required_rubygems_version: !ruby/object:Gem::Requirement
76
- none: false
77
- requirements:
78
- - - ">="
79
- - !ruby/object:Gem::Version
80
- hash: 3
81
- segments:
82
- - 0
83
- version: "0"
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
84
132
  requirements: []
85
-
86
133
  rubyforge_project:
87
- rubygems_version: 1.8.10
134
+ rubygems_version: 2.2.2
88
135
  signing_key:
89
- specification_version: 3
90
- summary: Simple geocoding for Rails ActiveRecord models
91
- test_files: []
92
-
136
+ specification_version: 4
137
+ summary: Simple geocoding for Active Record models
138
+ test_files:
139
+ - spec/acts_as_geocodable_generator_spec.rb
140
+ - spec/acts_as_geocodable_spec.rb
141
+ - spec/db/database.yml.example
142
+ - spec/db/schema.rb
143
+ - spec/geocode_spec.rb
144
+ - spec/spec_helper.rb
145
+ - spec/support/factories.rb
146
+ - spec/support/geocoder.rb
147
+ - spec/support/models.rb