geokit-rails 1.1.4 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of geokit-rails might be problematic. Click here for more details.

@@ -0,0 +1,22 @@
1
+ module Geokit
2
+ module Adapters
3
+ class Mysql2 < Abstract
4
+
5
+ def sphere_distance_sql(lat, lng, multiplier)
6
+ %|
7
+ (ACOS(least(1,COS(#{lat})*COS(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*COS(RADIANS(#{qualified_lng_column_name}))+
8
+ COS(#{lat})*SIN(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*SIN(RADIANS(#{qualified_lng_column_name}))+
9
+ SIN(#{lat})*SIN(RADIANS(#{qualified_lat_column_name}))))*#{multiplier})
10
+ |
11
+ end
12
+
13
+ def flat_distance_sql(origin, lat_degree_units, lng_degree_units)
14
+ %|
15
+ SQRT(POW(#{lat_degree_units}*(#{origin.lat}-#{qualified_lat_column_name}),2)+
16
+ POW(#{lng_degree_units}*(#{origin.lng}-#{qualified_lng_column_name}),2))
17
+ |
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,53 @@
1
+ module Geokit
2
+ module Adapters
3
+ class SQLite < Abstract
4
+
5
+ def self.add_numeric(name)
6
+ @@connection.create_function name, 1, :numeric do |func, *args|
7
+ func.result = yield(*args)
8
+ end
9
+ end
10
+
11
+ def self.add_math(name)
12
+ add_numeric name do |*n|
13
+ Math.send name, *n
14
+ end
15
+ end
16
+
17
+ class << self
18
+ def load(klass)
19
+ @@connection = klass.connection.raw_connection
20
+ # Define the functions needed
21
+ add_math 'sqrt'
22
+ add_math 'cos'
23
+ add_math 'acos'
24
+ add_math 'sin'
25
+
26
+ add_numeric('pow') { |n, m| n**m }
27
+ add_numeric('radians') { |n| n * Math::PI / 180 }
28
+ add_numeric('least') { |*args| args.min }
29
+ end
30
+ end
31
+
32
+ def sphere_distance_sql(lat, lng, multiplier)
33
+ %|
34
+ (CASE WHEN #{qualified_lat_column_name} IS NULL OR #{qualified_lng_column_name} IS NULL THEN NULL ELSE
35
+ (ACOS(least(1,COS(#{lat})*COS(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*COS(RADIANS(#{qualified_lng_column_name}))+
36
+ COS(#{lat})*SIN(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*SIN(RADIANS(#{qualified_lng_column_name}))+
37
+ SIN(#{lat})*SIN(RADIANS(#{qualified_lat_column_name}))))*#{multiplier})
38
+ END)
39
+ |
40
+ end
41
+
42
+ def flat_distance_sql(origin, lat_degree_units, lng_degree_units)
43
+ %|
44
+ (CASE WHEN #{qualified_lat_column_name} IS NULL OR #{qualified_lng_column_name} IS NULL THEN NULL ELSE
45
+ SQRT(POW(#{lat_degree_units}*(#{origin.lat}-#{qualified_lat_column_name}),2)+
46
+ POW(#{lng_degree_units}*(#{origin.lng}-#{qualified_lng_column_name}),2))
47
+ END)
48
+ |
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,10 @@
1
+ # Extend Array with a sort_by_distance method.
2
+ class Array
3
+ # This method creates a "distance" attribute on each object, calculates the
4
+ # distance from the passed origin, and finally sorts the array by the
5
+ # resulting distance.
6
+ def sort_by_distance_from(origin, opts={})
7
+ warn "[DEPRECATION] `Array#sort_by_distance_from(origin, opts)` is deprecated. Please use Array#sort_by{|e| e.distance_to(origin, opts)} instead which is not destructive"
8
+ self[0..-1] = sort_by{|e| e.distance_to(origin, opts)}
9
+ end
10
+ end
@@ -18,5 +18,4 @@ module Geokit
18
18
  end
19
19
  EOS
20
20
  end
21
- Geokit::Geocoders.logger = ActiveRecord::Base.logger
22
21
  end
@@ -1,16 +1,18 @@
1
+ require 'active_support/concern'
2
+
1
3
  module Geokit
2
4
  module GeocoderControl
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ if self.respond_to? :before_filter
9
+ self.send :before_filter, :set_geokit_domain
10
+ end
11
+ end
12
+
3
13
  def set_geokit_domain
4
14
  Geokit::Geocoders::domain = request.domain
5
15
  logger.debug("Geokit is using the domain: #{Geokit::Geocoders::domain}")
6
16
  end
7
-
8
- def self.included(base)
9
- if base.respond_to? :before_filter
10
- base.send :before_filter, :set_geokit_domain
11
- end
12
- end
13
17
  end
14
- end
15
-
16
- ActionController::Base.send(:include, Geokit::GeocoderControl) if defined?(ActionController::Base)
18
+ end
@@ -1,4 +1,5 @@
1
1
  require 'yaml'
2
+ require 'active_support/concern'
2
3
 
3
4
  module Geokit
4
5
  # Contains a class method geocode_ip_address which can be used to enable automatic geocoding
@@ -6,10 +7,7 @@ module Geokit
6
7
  # session to minimize web service calls. The point of the helper is to enable location-based
7
8
  # websites to have a best-guess for new visitors.
8
9
  module IpGeocodeLookup
9
- # Mix below class methods into ActionController.
10
- def self.included(base) # :nodoc:
11
- base.extend ClassMethods
12
- end
10
+ extend ActiveSupport::Concern
13
11
 
14
12
  # Class method to mix into active record.
15
13
  module ClassMethods # :nodoc:
@@ -0,0 +1,38 @@
1
+ require 'geokit-rails'
2
+ require 'rails'
3
+
4
+ module Geokit
5
+
6
+ class Railtie < ::Rails::Railtie
7
+
8
+ config.geokit = ActiveSupport::OrderedOptions.new
9
+ config.geokit.geocoders = ActiveSupport::OrderedOptions.new
10
+
11
+ initializer 'geokit-rails.insert_into_active_record' do
12
+ ActiveSupport.on_load :active_record do
13
+ ActiveRecord::Base.send(:include, Geokit::ActsAsMappable::Glue)
14
+ Geokit::Geocoders.logger = ActiveRecord::Base.logger
15
+ end
16
+ end
17
+
18
+ initializer 'geokit-rails.insert_into_action_controller' do
19
+ ActiveSupport.on_load :action_controller do
20
+ ActionController::Base.send(:include, Geokit::GeocoderControl)
21
+ ActionController::Base.send(:include, GeoKit::IpGeocodeLookup)
22
+ end
23
+ end
24
+
25
+ config.after_initialize do |app|
26
+ options = app.config.geokit
27
+ geocoders_options = options.delete(:geocoders)
28
+
29
+ options.each do |k,v|
30
+ Geokit::send("#{k}=", v)
31
+ end
32
+ geocoders_options.each do |k,v|
33
+ Geokit::Geocoders::send("#{k}=", v)
34
+ end
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,3 @@
1
+ module GeokitRails
2
+ VERSION = "2.0.0.rc1"
3
+ end
@@ -1,12 +1,11 @@
1
1
  require 'test_helper'
2
2
 
3
-
4
3
  Geokit::Geocoders::provider_order = [:google, :us]
5
4
 
6
5
  class ActsAsMappableTest < GeokitTestCase
7
-
6
+
8
7
  LOCATION_A_IP = "217.10.83.5"
9
-
8
+
10
9
  def setup
11
10
  @location_a = GeoKit::GeoLoc.new
12
11
  @location_a.lat = 32.918593
@@ -15,439 +14,396 @@ class ActsAsMappableTest < GeokitTestCase
15
14
  @location_a.state = "TX"
16
15
  @location_a.country_code = "US"
17
16
  @location_a.success = true
18
-
17
+
19
18
  @sw = GeoKit::LatLng.new(32.91663,-96.982841)
20
19
  @ne = GeoKit::LatLng.new(32.96302,-96.919495)
21
20
  @bounds_center=GeoKit::LatLng.new((@sw.lat+@ne.lat)/2,(@sw.lng+@ne.lng)/2)
22
-
21
+
23
22
  @starbucks = companies(:starbucks)
24
23
  @loc_a = locations(:a)
25
24
  @custom_loc_a = custom_locations(:a)
26
25
  @loc_e = locations(:e)
27
26
  @custom_loc_e = custom_locations(:e)
28
-
27
+
29
28
  @barnes_and_noble = mock_organizations(:barnes_and_noble)
30
29
  @address = mock_addresses(:address_barnes_and_noble)
31
30
  end
32
-
31
+
32
+ def test_sort_by_distance_from
33
+ locations = Location.all
34
+ unsorted = [locations(:a), locations(:b), locations(:c), locations(:d), locations(:e), locations(:f)]
35
+ sorted = [locations(:a), locations(:b), locations(:c), locations(:f), locations(:d), locations(:e)]
36
+ assert_equal unsorted, locations
37
+ assert_equal sorted, locations.sort_by{|l| l.distance_to(locations(:a))}
38
+ assert_equal sorted, locations.sort_by_distance_from(locations(:a))
39
+ assert_equal sorted, locations # last action desctructive
40
+ end
41
+
33
42
  def test_override_default_units_the_hard_way
34
43
  Location.default_units = :kms
35
- locations = Location.find(:all, :origin => @loc_a, :conditions => "distance < 3.97")
36
- assert_equal 5, locations.size
37
- locations = Location.count(:origin => @loc_a, :conditions => "distance < 3.97")
38
- assert_equal 5, locations
44
+ locations = Location.geo_scope(:origin => @loc_a).where("distance < 3.97")
45
+ assert_equal 5, locations.all.size
46
+ assert_equal 5, locations.count
39
47
  Location.default_units = :miles
40
48
  end
41
-
49
+
42
50
  def test_include
43
- locations = Location.find(:all, :origin => @loc_a, :include => :company, :conditions => "company_id = 1")
51
+ locations = Location.geo_scope(:origin => @loc_a).includes(:company).where("company_id = 1").all
44
52
  assert !locations.empty?
45
53
  assert_equal 1, locations[0].company.id
46
54
  assert_equal 'Starbucks', locations[0].company.name
47
55
  end
48
-
56
+
49
57
  def test_distance_between_geocoded
50
58
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a)
51
59
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("San Francisco, CA").returns(@location_a)
52
- assert_equal 0, Location.distance_between("Irving, TX", "San Francisco, CA")
60
+ assert_equal 0, Location.distance_between("Irving, TX", "San Francisco, CA")
53
61
  end
54
-
62
+
55
63
  def test_distance_to_geocoded
56
64
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a)
57
- assert_equal 0, @custom_loc_a.distance_to("Irving, TX")
65
+ assert_equal 0, @custom_loc_a.distance_to("Irving, TX")
58
66
  end
59
-
67
+
60
68
  def test_distance_to_geocoded_error
61
69
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(GeoKit::GeoLoc.new)
62
70
  assert_raise(GeoKit::Geocoders::GeocodeError) { @custom_loc_a.distance_to("Irving, TX") }
63
71
  end
64
-
72
+
65
73
  def test_custom_attributes_distance_calculations
66
74
  assert_equal 0, @custom_loc_a.distance_to(@loc_a)
67
75
  assert_equal 0, CustomLocation.distance_between(@custom_loc_a, @loc_a)
68
76
  end
69
-
77
+
70
78
  def test_distance_column_in_select
71
- locations = Location.find(:all, :origin => @loc_a, :order => "distance ASC")
72
- assert_equal 6, locations.size
79
+ locations = Location.geo_scope(:origin => @loc_a).order("distance ASC")
80
+ assert_equal 6, locations.all.size
73
81
  assert_equal 0, @loc_a.distance_to(locations.first)
74
82
  assert_in_delta 3.97, @loc_a.distance_to(locations.last, :units => :miles, :formula => :sphere), 0.01
75
83
  end
76
-
84
+
77
85
  def test_find_with_distance_condition
78
- locations = Location.find(:all, :origin => @loc_a, :conditions => "distance < 3.97")
79
- assert_equal 5, locations.size
80
- locations = Location.count(:origin => @loc_a, :conditions => "distance < 3.97")
81
- assert_equal 5, locations
82
- end
83
-
86
+ locations = Location.geo_scope(:origin => @loc_a, :within => 3.97)
87
+ assert_equal 5, locations.all.size
88
+ assert_equal 5, locations.count
89
+ end
90
+
84
91
  def test_find_with_distance_condition_with_units_override
85
- locations = Location.find(:all, :origin => @loc_a, :units => :kms, :conditions => "distance < 6.387")
86
- assert_equal 5, locations.size
87
- locations = Location.count(:origin => @loc_a, :units => :kms, :conditions => "distance < 6.387")
88
- assert_equal 5, locations
92
+ locations = Location.geo_scope(:origin => @loc_a, :units => :kms, :within => 6.387)
93
+ assert_equal 5, locations.all.size
94
+ assert_equal 5, locations.count
89
95
  end
90
-
96
+
91
97
  def test_find_with_distance_condition_with_formula_override
92
- locations = Location.find(:all, :origin => @loc_a, :formula => :flat, :conditions => "distance < 6.387")
93
- assert_equal 6, locations.size
94
- locations = Location.count(:origin => @loc_a, :formula => :flat, :conditions => "distance < 6.387")
95
- assert_equal 6, locations
98
+ locations = Location.geo_scope(:origin => @loc_a, :formula => :flat, :within => 6.387)
99
+ assert_equal 6, locations.all.size
100
+ assert_equal 6, locations.count
96
101
  end
97
-
102
+
98
103
  def test_find_within
99
- locations = Location.find_within(3.97, :origin => @loc_a)
100
- assert_equal 5, locations.size
101
- locations = Location.count_within(3.97, :origin => @loc_a)
102
- assert_equal 5, locations
103
- end
104
-
105
- def test_find_within_with_token
106
- locations = Location.find(:all, :within => 3.97, :origin => @loc_a)
107
- assert_equal 5, locations.size
108
- locations = Location.count(:within => 3.97, :origin => @loc_a)
109
- assert_equal 5, locations
110
- end
111
-
104
+ locations = Location.within(3.97, :origin => @loc_a)
105
+ assert_equal 5, locations.all.size
106
+ assert_equal 5, locations.count
107
+ end
108
+
112
109
  def test_find_within_with_coordinates
113
- locations = Location.find_within(3.97, :origin =>[@loc_a.lat,@loc_a.lng])
114
- assert_equal 5, locations.size
115
- locations = Location.count_within(3.97, :origin =>[@loc_a.lat,@loc_a.lng])
116
- assert_equal 5, locations
110
+ locations = Location.within(3.97, :origin =>[@loc_a.lat,@loc_a.lng])
111
+ assert_equal 5, locations.all.size
112
+ assert_equal 5, locations.count
117
113
  end
118
-
114
+
119
115
  def test_find_with_compound_condition
120
- locations = Location.find(:all, :origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
121
- assert_equal 2, locations.size
122
- locations = Location.count(:origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
123
- assert_equal 2, locations
116
+ locations = Location.geo_scope(:origin => @loc_a).where("distance < 5 and city = 'Coppell'")
117
+ assert_equal 2, locations.all.size
118
+ assert_equal 2, locations.count
124
119
  end
125
-
120
+
126
121
  def test_find_with_secure_compound_condition
127
- locations = Location.find(:all, :origin => @loc_a, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
128
- assert_equal 2, locations.size
129
- locations = Location.count(:origin => @loc_a, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
130
- assert_equal 2, locations
122
+ locations = Location.geo_scope(:origin => @loc_a).where(["distance < ? and city = ?", 5, 'Coppell'])
123
+ assert_equal 2, locations.all.size
124
+ assert_equal 2, locations.count
131
125
  end
132
-
126
+
133
127
  def test_find_beyond
134
- locations = Location.find_beyond(3.95, :origin => @loc_a)
135
- assert_equal 1, locations.size
136
- locations = Location.count_beyond(3.95, :origin => @loc_a)
137
- assert_equal 1, locations
128
+ locations = Location.beyond(3.95, :origin => @loc_a)
129
+ assert_equal 1, locations.all.size
130
+ assert_equal 1, locations.count
138
131
  end
139
-
132
+
140
133
  def test_find_beyond_with_token
141
- locations = Location.find(:all, :beyond => 3.95, :origin => @loc_a)
142
- assert_equal 1, locations.size
143
- locations = Location.count(:beyond => 3.95, :origin => @loc_a)
144
- assert_equal 1, locations
134
+ # locations = Location.find(:all, :beyond => 3.95, :origin => @loc_a)
135
+ locations = Location.geo_scope(:beyond => 3.95, :origin => @loc_a)
136
+ assert_equal 1, locations.all.size
137
+ assert_equal 1, locations.count
145
138
  end
146
-
139
+
147
140
  def test_find_beyond_with_coordinates
148
- locations = Location.find_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
149
- assert_equal 1, locations.size
150
- locations = Location.count_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
151
- assert_equal 1, locations
141
+ locations = Location.beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
142
+ assert_equal 1, locations.all.size
143
+ assert_equal 1, locations.count
152
144
  end
153
-
145
+
154
146
  def test_find_range_with_token
155
- locations = Location.find(:all, :range => 0..10, :origin => @loc_a)
156
- assert_equal 6, locations.size
157
- locations = Location.count(:range => 0..10, :origin => @loc_a)
158
- assert_equal 6, locations
147
+ locations = Location.geo_scope(:range => 0..10, :origin => @loc_a)
148
+ assert_equal 6, locations.all.size
149
+ assert_equal 6, locations.count
159
150
  end
160
-
151
+
161
152
  def test_find_range_with_token_with_conditions
162
- locations = Location.find(:all, :origin => @loc_a, :range => 0..10, :conditions => ["city = ?", 'Coppell'])
163
- assert_equal 2, locations.size
164
- locations = Location.count(:origin => @loc_a, :range => 0..10, :conditions => ["city = ?", 'Coppell'])
165
- assert_equal 2, locations
153
+ locations = Location.geo_scope(:origin => @loc_a, :range => 0..10).where(["city = ?", 'Coppell'])
154
+ assert_equal 2, locations.all.size
155
+ assert_equal 2, locations.count
166
156
  end
167
-
157
+
168
158
  def test_find_range_with_token_with_hash_conditions
169
- locations = Location.find(:all, :origin => @loc_a, :range => 0..10, :conditions => {:city => 'Coppell'})
170
- assert_equal 2, locations.size
171
- locations = Location.count(:origin => @loc_a, :range => 0..10, :conditions => {:city => 'Coppell'})
172
- assert_equal 2, locations
159
+ locations = Location.geo_scope(:origin => @loc_a, :range => 0..10).where(:city => 'Coppell')
160
+ assert_equal 2, locations.all.size
161
+ assert_equal 2, locations.count
173
162
  end
174
-
163
+
175
164
  def test_find_range_with_token_excluding_end
176
- locations = Location.find(:all, :range => 0...10, :origin => @loc_a)
177
- assert_equal 6, locations.size
178
- locations = Location.count(:range => 0...10, :origin => @loc_a)
179
- assert_equal 6, locations
165
+ locations = Location.geo_scope(:range => 0...10, :origin => @loc_a)
166
+ assert_equal 6, locations.all.size
167
+ assert_equal 6, locations.count
180
168
  end
181
-
169
+
182
170
  def test_find_nearest
183
- assert_equal @loc_a, Location.find_nearest(:origin => @loc_a)
171
+ assert_equal @loc_a, Location.nearest(:origin => @loc_a).first
184
172
  end
185
-
186
- def test_find_nearest_through_find
187
- assert_equal @loc_a, Location.find(:nearest, :origin => @loc_a)
188
- end
189
-
173
+
190
174
  def test_find_nearest_with_coordinates
191
- assert_equal @loc_a, Location.find_nearest(:origin =>[@loc_a.lat, @loc_a.lng])
175
+ assert_equal @loc_a, Location.nearest(:origin =>[@loc_a.lat, @loc_a.lng]).first
192
176
  end
193
-
177
+
194
178
  def test_find_farthest
195
- assert_equal @loc_e, Location.find_farthest(:origin => @loc_a)
196
- end
197
-
198
- def test_find_farthest_through_find
199
- assert_equal @loc_e, Location.find(:farthest, :origin => @loc_a)
179
+ assert_equal @loc_e, Location.farthest(:origin => @loc_a).first
200
180
  end
201
-
181
+
202
182
  def test_find_farthest_with_coordinates
203
- assert_equal @loc_e, Location.find_farthest(:origin =>[@loc_a.lat, @loc_a.lng])
183
+ assert_equal @loc_e, Location.farthest(:origin =>[@loc_a.lat, @loc_a.lng]).first
204
184
  end
205
-
185
+
206
186
  def test_scoped_distance_column_in_select
207
- locations = @starbucks.locations.find(:all, :origin => @loc_a, :order => "distance ASC")
208
- assert_equal 5, locations.size
187
+ locations = @starbucks.locations.geo_scope(:origin => @loc_a).order("distance ASC")
188
+ assert_equal 5, locations.all.size
209
189
  assert_equal 0, @loc_a.distance_to(locations.first)
210
190
  assert_in_delta 3.97, @loc_a.distance_to(locations.last, :units => :miles, :formula => :sphere), 0.01
211
191
  end
212
-
192
+
213
193
  def test_scoped_find_with_distance_condition
214
- locations = @starbucks.locations.find(:all, :origin => @loc_a, :conditions => "distance < 3.97")
215
- assert_equal 4, locations.size
216
- locations = @starbucks.locations.count(:origin => @loc_a, :conditions => "distance < 3.97")
217
- assert_equal 4, locations
218
- end
219
-
194
+ locations = @starbucks.locations.geo_scope(:origin => @loc_a).where("distance < 3.97")
195
+ assert_equal 4, locations.all.size
196
+ assert_equal 4, locations.count
197
+ end
198
+
220
199
  def test_scoped_find_within
221
- locations = @starbucks.locations.find_within(3.97, :origin => @loc_a)
222
- assert_equal 4, locations.size
223
- locations = @starbucks.locations.count_within(3.97, :origin => @loc_a)
224
- assert_equal 4, locations
200
+ locations = @starbucks.locations.within(3.97, :origin => @loc_a)
201
+ assert_equal 4, locations.all.size
202
+ assert_equal 4, locations.count
225
203
  end
226
-
204
+
227
205
  def test_scoped_find_with_compound_condition
228
- locations = @starbucks.locations.find(:all, :origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
229
- assert_equal 2, locations.size
230
- locations = @starbucks.locations.count( :origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
231
- assert_equal 2, locations
206
+ locations = @starbucks.locations.geo_scope(:origin => @loc_a).where("distance < 5 and city = 'Coppell'")
207
+ assert_equal 2, locations.all.size
208
+ assert_equal 2, locations.count
232
209
  end
233
-
210
+
234
211
  def test_scoped_find_beyond
235
- locations = @starbucks.locations.find_beyond(3.95, :origin => @loc_a)
236
- assert_equal 1, locations.size
237
- locations = @starbucks.locations.count_beyond(3.95, :origin => @loc_a)
238
- assert_equal 1, locations
212
+ locations = @starbucks.locations.beyond(3.95, :origin => @loc_a)
213
+ assert_equal 1, locations.all.size
214
+ assert_equal 1, locations.count
239
215
  end
240
-
216
+
241
217
  def test_scoped_find_nearest
242
- assert_equal @loc_a, @starbucks.locations.find_nearest(:origin => @loc_a)
218
+ assert_equal @loc_a, @starbucks.locations.nearest(:origin => @loc_a).first
243
219
  end
244
-
220
+
245
221
  def test_scoped_find_farthest
246
- assert_equal @loc_e, @starbucks.locations.find_farthest(:origin => @loc_a)
247
- end
248
-
222
+ assert_equal @loc_e, @starbucks.locations.farthest(:origin => @loc_a).first
223
+ end
224
+
249
225
  def test_ip_geocoded_distance_column_in_select
250
226
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
251
- locations = Location.find(:all, :origin => LOCATION_A_IP, :order => "distance ASC")
252
- assert_equal 6, locations.size
227
+ locations = Location.geo_scope(:origin => LOCATION_A_IP).order("distance ASC")
228
+ assert_equal 6, locations.all.size
253
229
  assert_equal 0, @loc_a.distance_to(locations.first)
254
230
  assert_in_delta 3.97, @loc_a.distance_to(locations.last, :units => :miles, :formula => :sphere), 0.01
255
231
  end
256
-
232
+
257
233
  def test_ip_geocoded_find_with_distance_condition
258
234
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
259
- locations = Location.find(:all, :origin => LOCATION_A_IP, :conditions => "distance < 3.97")
260
- assert_equal 5, locations.size
261
- GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
262
- locations = Location.count(:origin => LOCATION_A_IP, :conditions => "distance < 3.97")
263
- assert_equal 5, locations
264
- end
265
-
235
+ locations = Location.geo_scope(:origin => LOCATION_A_IP).where("distance < 3.97")
236
+ assert_equal 5, locations.all.size
237
+ assert_equal 5, locations.count
238
+ end
239
+
266
240
  def test_ip_geocoded_find_within
267
241
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
268
- locations = Location.find_within(3.97, :origin => LOCATION_A_IP)
269
- assert_equal 5, locations.size
270
- GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
271
- locations = Location.count_within(3.97, :origin => LOCATION_A_IP)
272
- assert_equal 5, locations
242
+ locations = Location.within(3.97, :origin => LOCATION_A_IP)
243
+ assert_equal 5, locations.all.size
244
+ assert_equal 5, locations.count
273
245
  end
274
-
246
+
275
247
  def test_ip_geocoded_find_with_compound_condition
276
248
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
277
- locations = Location.find(:all, :origin => LOCATION_A_IP, :conditions => "distance < 5 and city = 'Coppell'")
278
- assert_equal 2, locations.size
279
- GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
280
- locations = Location.count(:origin => LOCATION_A_IP, :conditions => "distance < 5 and city = 'Coppell'")
281
- assert_equal 2, locations
249
+ locations = Location.geo_scope(:origin => LOCATION_A_IP).where("distance < 5 and city = 'Coppell'")
250
+ assert_equal 2, locations.all.size
251
+ assert_equal 2, locations.count
282
252
  end
283
-
253
+
284
254
  def test_ip_geocoded_find_with_secure_compound_condition
285
255
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
286
- locations = Location.find(:all, :origin => LOCATION_A_IP, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
287
- assert_equal 2, locations.size
288
- GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
289
- locations = Location.count(:origin => LOCATION_A_IP, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
290
- assert_equal 2, locations
256
+ locations = Location.geo_scope(:origin => LOCATION_A_IP).where(["distance < ? and city = ?", 5, 'Coppell'])
257
+ assert_equal 2, locations.all.size
258
+ assert_equal 2, locations.count
291
259
  end
292
-
260
+
293
261
  def test_ip_geocoded_find_beyond
294
262
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
295
- locations = Location.find_beyond(3.95, :origin => LOCATION_A_IP)
296
- assert_equal 1, locations.size
297
- GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
298
- locations = Location.count_beyond(3.95, :origin => LOCATION_A_IP)
299
- assert_equal 1, locations
263
+ locations = Location.beyond(3.95, :origin => LOCATION_A_IP)
264
+ assert_equal 1, locations.all.size
265
+ assert_equal 1, locations.count
300
266
  end
301
-
267
+
302
268
  def test_ip_geocoded_find_nearest
303
269
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
304
- assert_equal @loc_a, Location.find_nearest(:origin => LOCATION_A_IP)
270
+ assert_equal @loc_a, Location.nearest(:origin => LOCATION_A_IP).first
305
271
  end
306
-
272
+
307
273
  def test_ip_geocoded_find_farthest
308
274
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
309
- assert_equal @loc_e, Location.find_farthest(:origin => LOCATION_A_IP)
275
+ assert_equal @loc_e, Location.farthest(:origin => LOCATION_A_IP).first
310
276
  end
311
-
277
+
312
278
  def test_ip_geocoder_exception
313
279
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with('127.0.0.1').returns(GeoKit::GeoLoc.new)
314
280
  assert_raises GeoKit::Geocoders::GeocodeError do
315
- Location.find_farthest(:origin => '127.0.0.1')
281
+ Location.farthest(:origin => '127.0.0.1').first
316
282
  end
317
283
  end
318
-
284
+
319
285
  def test_address_geocode
320
- GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with('Irving, TX').returns(@location_a)
321
- locations = Location.find(:all, :origin => 'Irving, TX', :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
322
- assert_equal 2, locations.size
286
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with('Irving, TX').returns(@location_a)
287
+ locations = Location.geo_scope(:origin => 'Irving, TX').where(["distance < ? and city = ?", 5, 'Coppell'])
288
+ assert_equal 2, locations.all.size
289
+ assert_equal 2, locations.count
323
290
  end
324
-
291
+
325
292
  def test_find_with_custom_distance_condition
326
- locations = CustomLocation.find(:all, :origin => @loc_a, :conditions => "dist < 3.97")
327
- assert_equal 5, locations.size
328
- locations = CustomLocation.count(:origin => @loc_a, :conditions => "dist < 3.97")
329
- assert_equal 5, locations
330
- end
331
-
293
+ locations = CustomLocation.geo_scope(:origin => @loc_a).where("dist < 3.97")
294
+ assert_equal 5, locations.all.size
295
+ assert_equal 5, locations.count
296
+ end
297
+
332
298
  def test_find_with_custom_distance_condition_using_custom_origin
333
- locations = CustomLocation.find(:all, :origin => @custom_loc_a, :conditions => "dist < 3.97")
334
- assert_equal 5, locations.size
335
- locations = CustomLocation.count(:origin => @custom_loc_a, :conditions => "dist < 3.97")
336
- assert_equal 5, locations
299
+ locations = CustomLocation.geo_scope(:origin => @custom_loc_a).where("dist < 3.97")
300
+ assert_equal 5, locations.all.size
301
+ assert_equal 5, locations.count
337
302
  end
338
-
303
+
339
304
  def test_find_within_with_custom
340
- locations = CustomLocation.find_within(3.97, :origin => @loc_a)
341
- assert_equal 5, locations.size
342
- locations = CustomLocation.count_within(3.97, :origin => @loc_a)
343
- assert_equal 5, locations
305
+ locations = CustomLocation.within(3.97, :origin => @loc_a)
306
+ assert_equal 5, locations.all.size
307
+ assert_equal 5, locations.count
344
308
  end
345
-
309
+
346
310
  def test_find_within_with_coordinates_with_custom
347
- locations = CustomLocation.find_within(3.97, :origin =>[@loc_a.lat, @loc_a.lng])
348
- assert_equal 5, locations.size
349
- locations = CustomLocation.count_within(3.97, :origin =>[@loc_a.lat, @loc_a.lng])
350
- assert_equal 5, locations
311
+ locations = CustomLocation.within(3.97, :origin =>[@loc_a.lat, @loc_a.lng])
312
+ assert_equal 5, locations.all.size
313
+ assert_equal 5, locations.count
351
314
  end
352
-
315
+
353
316
  def test_find_with_compound_condition_with_custom
354
- locations = CustomLocation.find(:all, :origin => @loc_a, :conditions => "dist < 5 and city = 'Coppell'")
355
- assert_equal 1, locations.size
356
- locations = CustomLocation.count(:origin => @loc_a, :conditions => "dist < 5 and city = 'Coppell'")
357
- assert_equal 1, locations
317
+ locations = CustomLocation.geo_scope(:origin => @loc_a).where("dist < 5 and city = 'Coppell'")
318
+ assert_equal 1, locations.all.size
319
+ assert_equal 1, locations.count
358
320
  end
359
-
321
+
360
322
  def test_find_with_secure_compound_condition_with_custom
361
- locations = CustomLocation.find(:all, :origin => @loc_a, :conditions => ["dist < ? and city = ?", 5, 'Coppell'])
362
- assert_equal 1, locations.size
363
- locations = CustomLocation.count(:origin => @loc_a, :conditions => ["dist < ? and city = ?", 5, 'Coppell'])
364
- assert_equal 1, locations
323
+ locations = CustomLocation.geo_scope(:origin => @loc_a).where(["dist < ? and city = ?", 5, 'Coppell'])
324
+ assert_equal 1, locations.all.size
325
+ assert_equal 1, locations.count
365
326
  end
366
-
327
+
367
328
  def test_find_beyond_with_custom
368
- locations = CustomLocation.find_beyond(3.95, :origin => @loc_a)
369
- assert_equal 1, locations.size
370
- locations = CustomLocation.count_beyond(3.95, :origin => @loc_a)
371
- assert_equal 1, locations
329
+ locations = CustomLocation.beyond(3.95, :origin => @loc_a)
330
+ assert_equal 1, locations.all.size
331
+ assert_equal 1, locations.count
372
332
  end
373
-
333
+
374
334
  def test_find_beyond_with_coordinates_with_custom
375
- locations = CustomLocation.find_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
376
- assert_equal 1, locations.size
377
- locations = CustomLocation.count_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
378
- assert_equal 1, locations
335
+ locations = CustomLocation.beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
336
+ assert_equal 1, locations.all.size
337
+ assert_equal 1, locations.count
379
338
  end
380
-
339
+
381
340
  def test_find_nearest_with_custom
382
- assert_equal @custom_loc_a, CustomLocation.find_nearest(:origin => @loc_a)
341
+ assert_equal @custom_loc_a, CustomLocation.nearest(:origin => @loc_a).first
383
342
  end
384
-
343
+
385
344
  def test_find_nearest_with_coordinates_with_custom
386
- assert_equal @custom_loc_a, CustomLocation.find_nearest(:origin =>[@loc_a.lat, @loc_a.lng])
345
+ assert_equal @custom_loc_a, CustomLocation.nearest(:origin =>[@loc_a.lat, @loc_a.lng]).first
387
346
  end
388
-
347
+
389
348
  def test_find_farthest_with_custom
390
- assert_equal @custom_loc_e, CustomLocation.find_farthest(:origin => @loc_a)
349
+ assert_equal @custom_loc_e, CustomLocation.farthest(:origin => @loc_a).first
391
350
  end
392
-
351
+
393
352
  def test_find_farthest_with_coordinates_with_custom
394
- assert_equal @custom_loc_e, CustomLocation.find_farthest(:origin =>[@loc_a.lat, @loc_a.lng])
353
+ assert_equal @custom_loc_e, CustomLocation.farthest(:origin =>[@loc_a.lat, @loc_a.lng]).first
395
354
  end
396
-
355
+
397
356
  def test_find_with_array_origin
398
- locations = Location.find(:all, :origin =>[@loc_a.lat,@loc_a.lng], :conditions => "distance < 3.97")
399
- assert_equal 5, locations.size
400
- locations = Location.count(:origin =>[@loc_a.lat,@loc_a.lng], :conditions => "distance < 3.97")
401
- assert_equal 5, locations
357
+ locations = Location.geo_scope(:origin =>[@loc_a.lat,@loc_a.lng]).where("distance < 3.97")
358
+ assert_equal 5, locations.all.size
359
+ assert_equal 5, locations.count
402
360
  end
403
-
404
-
361
+
362
+
405
363
  # Bounding box tests
406
-
364
+
407
365
  def test_find_within_bounds
408
- locations = Location.find_within_bounds([@sw,@ne])
409
- assert_equal 2, locations.size
410
- locations = Location.count_within_bounds([@sw,@ne])
411
- assert_equal 2, locations
366
+ locations = Location.in_bounds([@sw,@ne])
367
+ assert_equal 2, locations.all.size
368
+ assert_equal 2, locations.count
412
369
  end
413
-
370
+
414
371
  def test_find_within_bounds_ordered_by_distance
415
- locations = Location.find_within_bounds([@sw,@ne], :origin=>@bounds_center, :order=>'distance asc')
372
+ locations = Location.in_bounds([@sw,@ne], :origin=>@bounds_center).order('distance asc')
416
373
  assert_equal locations[0], locations(:d)
417
374
  assert_equal locations[1], locations(:a)
418
375
  end
419
-
376
+
420
377
  def test_find_within_bounds_with_token
421
- locations = Location.find(:all, :bounds=>[@sw,@ne])
422
- assert_equal 2, locations.size
423
- locations = Location.count(:bounds=>[@sw,@ne])
424
- assert_equal 2, locations
378
+ locations = Location.geo_scope(:bounds=>[@sw,@ne])
379
+ assert_equal 2, locations.all.size
380
+ assert_equal 2, locations.count
425
381
  end
426
-
382
+
427
383
  def test_find_within_bounds_with_string_conditions
428
- locations = Location.find(:all, :bounds=>[@sw,@ne], :conditions=>"id !=#{locations(:a).id}")
429
- assert_equal 1, locations.size
384
+ locations = Location.geo_scope(:bounds=>[@sw,@ne]).where("id !=#{locations(:a).id}")
385
+ assert_equal 1, locations.all.size
430
386
  end
431
-
387
+
432
388
  def test_find_within_bounds_with_array_conditions
433
- locations = Location.find(:all, :bounds=>[@sw,@ne], :conditions=>["id != ?", locations(:a).id])
434
- assert_equal 1, locations.size
389
+ locations = Location.geo_scope(:bounds=>[@sw,@ne]).where(["id != ?", locations(:a).id])
390
+ assert_equal 1, locations.all.size
435
391
  end
436
-
392
+
437
393
  def test_find_within_bounds_with_hash_conditions
438
- locations = Location.find(:all, :bounds=>[@sw,@ne], :conditions=>{:id => locations(:a).id})
439
- assert_equal 1, locations.size
394
+ locations = Location.geo_scope(:bounds=>[@sw,@ne]).where({:id => locations(:a).id})
395
+ assert_equal 1, locations.all.size
440
396
  end
441
-
397
+
442
398
  def test_auto_geocode
443
399
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a)
444
400
  store=Store.new(:address=>'Irving, TX')
445
401
  store.save
446
- assert_equal store.lat,@location_a.lat
402
+ assert_equal store.lat,@location_a.lat
447
403
  assert_equal store.lng,@location_a.lng
448
404
  assert_equal 0, store.errors.size
449
405
  end
450
-
406
+
451
407
  def test_auto_geocode_failure
452
408
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("BOGUS").returns(GeoKit::GeoLoc.new)
453
409
  store=Store.new(:address=>'BOGUS')
@@ -455,20 +411,19 @@ class ActsAsMappableTest < GeokitTestCase
455
411
  assert store.new_record?
456
412
  assert_equal 1, store.errors.size
457
413
  end
458
-
414
+
459
415
  # Test :through
460
-
416
+
461
417
  def test_find_with_through
462
- organizations = MockOrganization.find(:all, :origin => @location_a, :order => 'distance ASC')
463
- assert_equal 2, organizations.size
464
- organizations = MockOrganization.count(:origin => @location_a, :conditions => "distance < 3.97")
465
- assert_equal 1, organizations
418
+ organizations = MockOrganization.geo_scope(:origin => @location_a).order('distance ASC')
419
+ assert_equal 2, organizations.all.size
420
+ organizations = MockOrganization.geo_scope(:origin => @location_a).where("distance < 3.97")
421
+ assert_equal 1, organizations.count
466
422
  end
467
-
423
+
468
424
  def test_find_with_through_with_hash
469
- people = MockPerson.find(:all, :origin => @location_a, :order => 'distance ASC')
425
+ people = MockPerson.geo_scope(:origin => @location_a).order('distance ASC')
470
426
  assert_equal 2, people.size
471
- people = MockPerson.count(:origin => @location_a, :conditions => "distance < 3.97")
472
- assert_equal 2, people
473
- end
427
+ assert_equal 2, people.count
428
+ end
474
429
  end