geokit-rails3 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,22 +3,22 @@ require 'active_support/concern'
3
3
 
4
4
  module Geokit
5
5
  module ActsAsMappable
6
-
6
+
7
7
  class UnsupportedAdapter < StandardError ; end
8
-
8
+
9
9
  # Add the +acts_as_mappable+ method into ActiveRecord subclasses
10
10
  module Glue # :nodoc:
11
11
  extend ActiveSupport::Concern
12
-
12
+
13
13
  module ClassMethods # :nodoc:
14
14
  def acts_as_mappable(options = {})
15
15
  metaclass = (class << self; self; end)
16
-
16
+
17
17
  self.send :include, Geokit::ActsAsMappable
18
-
18
+
19
19
  cattr_accessor :through
20
20
  self.through = options[:through]
21
-
21
+
22
22
  if reflection = Geokit::ActsAsMappable.end_of_reflection_chain(self.through, self)
23
23
  metaclass.instance_eval do
24
24
  [ :distance_column_name, :default_units, :default_formula, :lat_column_name, :lng_column_name, :qualified_lat_column_name, :qualified_lng_column_name ].each do |method_name|
@@ -29,7 +29,7 @@ module Geokit
29
29
  end
30
30
  else
31
31
  cattr_accessor :distance_column_name, :default_units, :default_formula, :lat_column_name, :lng_column_name, :qualified_lat_column_name, :qualified_lng_column_name
32
-
32
+
33
33
  self.distance_column_name = options[:distance_column_name] || 'distance'
34
34
  self.default_units = options[:default_units] || Geokit::default_units
35
35
  self.default_formula = options[:default_formula] || Geokit::default_formula
@@ -37,14 +37,14 @@ module Geokit
37
37
  self.lng_column_name = options[:lng_column_name] || 'lng'
38
38
  self.qualified_lat_column_name = "#{table_name}.#{lat_column_name}"
39
39
  self.qualified_lng_column_name = "#{table_name}.#{lng_column_name}"
40
-
40
+
41
41
  if options.include?(:auto_geocode) && options[:auto_geocode]
42
42
  # if the form auto_geocode=>true is used, let the defaults take over by suppling an empty hash
43
- options[:auto_geocode] = {} if options[:auto_geocode] == true
43
+ options[:auto_geocode] = {} if options[:auto_geocode] == true
44
44
  cattr_accessor :auto_geocode_field, :auto_geocode_error_message
45
45
  self.auto_geocode_field = options[:auto_geocode][:field] || 'address'
46
46
  self.auto_geocode_error_message = options[:auto_geocode][:error_message] || 'could not locate address'
47
-
47
+
48
48
  # set the actual callback here
49
49
  before_validation :auto_geocode_address, :on => :create
50
50
  end
@@ -52,16 +52,16 @@ module Geokit
52
52
  end
53
53
  end
54
54
  end # Glue
55
-
55
+
56
56
  extend ActiveSupport::Concern
57
-
57
+
58
58
  included do
59
59
  include Geokit::Mappable
60
60
  end
61
-
61
+
62
62
  # Class methods included in models when +acts_as_mappable+ is called
63
63
  module ClassMethods
64
-
64
+
65
65
  # A proxy to an instance of a finder adapter, inferred from the connection's adapter.
66
66
  def adapter
67
67
  @adapter ||= begin
@@ -74,23 +74,38 @@ module Geokit
74
74
  end
75
75
  end
76
76
 
77
+ def where(clause)
78
+ g self
79
+ pattern = Regexp.new("\\b#{distance_column_name}\\b")
80
+ value = @distance_formula
81
+
82
+ g new_clause = if clause.is_a?(String)
83
+ clause.gsub!(pattern, value)
84
+ elsif clause.is_a?(Array)
85
+ clause.first.gsub!(pattern, value)
86
+ else
87
+ clause
88
+ end
89
+ super(new_clause)
90
+ end
91
+
77
92
  def within(distance, options = {})
78
93
  options[:within] = distance
79
94
  geo_scope(options)
80
95
  end
81
96
  alias inside within
82
-
97
+
83
98
  def beyond(distance, options = {})
84
99
  options[:beyond] = distance
85
100
  geo_scope(options)
86
101
  end
87
102
  alias outside beyond
88
-
103
+
89
104
  def in_range(range, options = {})
90
105
  options[:range] = range
91
106
  geo_scope(options)
92
107
  end
93
-
108
+
94
109
  def in_bounds(bounds, options = {})
95
110
  options[:bounds] = bounds
96
111
  geo_scope(options)
@@ -107,30 +122,32 @@ module Geokit
107
122
 
108
123
  def geo_scope(options = {})
109
124
  arel = self.is_a?(ActiveRecord::Relation) ? self : self.scoped
110
-
125
+
111
126
  origin = extract_origin_from_options(options)
112
127
  units = extract_units_from_options(options)
113
128
  formula = extract_formula_from_options(options)
114
129
  bounds = extract_bounds_from_options(options)
115
-
130
+
116
131
  if origin || bounds
117
132
  bounds = formulate_bounds_from_distance(options, origin, units) unless bounds
118
-
133
+
119
134
  if origin
120
- distance_formula = distance_sql(origin, units, formula)
135
+ @distance_formula = distance_sql(origin, units, formula)
121
136
  arel = arel.select('*') if arel.select_values.blank?
122
- arel = arel.select("#{distance_formula} AS #{distance_column_name}")
137
+ arel = arel.select("#{@distance_formula} AS #{distance_column_name}")
123
138
  end
124
139
 
125
140
  if bounds
126
141
  bound_conditions = bound_conditions(bounds)
127
142
  arel = arel.where(bound_conditions) if bound_conditions
128
143
  end
129
-
144
+
130
145
  distance_conditions = distance_conditions(options)
131
146
  arel = arel.where(distance_conditions) if distance_conditions
132
-
133
- arel = substitute_distance_in_where_values(arel, origin, units, formula)
147
+
148
+ if origin
149
+ arel = substitute_distance_in_where_values(arel, origin, units, formula)
150
+ end
134
151
  end
135
152
 
136
153
  arel
@@ -147,17 +164,17 @@ module Geokit
147
164
  end
148
165
  sql
149
166
  end
150
-
167
+
151
168
  private
152
-
169
+
153
170
  # If it's a :within query, add a bounding box to improve performance.
154
- # This only gets called if a :bounds argument is not otherwise supplied.
171
+ # This only gets called if a :bounds argument is not otherwise supplied.
155
172
  def formulate_bounds_from_distance(options, origin, units)
156
173
  distance = options[:within] if options.has_key?(:within)
157
174
  distance = options[:range].last-(options[:range].exclude_end?? 1 : 0) if options.has_key?(:range)
158
175
  if distance
159
176
  res=Geokit::Bounds.from_point_and_radius(origin,distance,:units=>units)
160
- else
177
+ else
161
178
  nil
162
179
  end
163
180
  end
@@ -177,47 +194,47 @@ module Geokit
177
194
  lng_sql = bounds.crosses_meridian? ? "(#{qualified_lng_column_name}<#{ne.lng} OR #{qualified_lng_column_name}>#{sw.lng})" : "#{qualified_lng_column_name}>#{sw.lng} AND #{qualified_lng_column_name}<#{ne.lng}"
178
195
  "#{qualified_lat_column_name}>#{sw.lat} AND #{qualified_lat_column_name}<#{ne.lat} AND #{lng_sql}"
179
196
  end
180
-
197
+
181
198
  # Extracts the origin instance out of the options if it exists and returns
182
- # it. If there is no origin, looks for latitude and longitude values to
183
- # create an origin. The side-effect of the method is to remove these
199
+ # it. If there is no origin, looks for latitude and longitude values to
200
+ # create an origin. The side-effect of the method is to remove these
184
201
  # option keys from the hash.
185
202
  def extract_origin_from_options(options)
186
203
  origin = options.delete(:origin)
187
204
  res = normalize_point_to_lat_lng(origin) if origin
188
205
  res
189
206
  end
190
-
207
+
191
208
  # Extract the units out of the options if it exists and returns it. If
192
- # there is no :units key, it uses the default. The side effect of the
209
+ # there is no :units key, it uses the default. The side effect of the
193
210
  # method is to remove the :units key from the options hash.
194
211
  def extract_units_from_options(options)
195
212
  units = options[:units] || default_units
196
213
  options.delete(:units)
197
214
  units
198
215
  end
199
-
216
+
200
217
  # Extract the formula out of the options if it exists and returns it. If
201
- # there is no :formula key, it uses the default. The side effect of the
218
+ # there is no :formula key, it uses the default. The side effect of the
202
219
  # method is to remove the :formula key from the options hash.
203
220
  def extract_formula_from_options(options)
204
221
  formula = options[:formula] || default_formula
205
222
  options.delete(:formula)
206
223
  formula
207
224
  end
208
-
225
+
209
226
  def extract_bounds_from_options(options)
210
227
  bounds = options.delete(:bounds)
211
228
  bounds = Geokit::Bounds.normalize(bounds) if bounds
212
229
  end
213
-
230
+
214
231
  # Geocode IP address.
215
232
  def geocode_ip_address(origin)
216
233
  geo_location = Geokit::Geocoders::MultiGeocoder.geocode(origin)
217
234
  return geo_location if geo_location.success
218
235
  raise Geokit::Geocoders::GeocodeError
219
236
  end
220
-
237
+
221
238
  # Given a point in a variety of (an address to geocode,
222
239
  # an array of [lat,lng], or an object with appropriate lat/lng methods, an IP addres)
223
240
  # this method will normalize it into a Geokit::LatLng instance. The only thing this
@@ -227,8 +244,8 @@ module Geokit
227
244
  res = Geokit::LatLng.normalize(point) unless res
228
245
  res
229
246
  end
230
-
231
- # Looks for the distance column and replaces it with the distance sql. If an origin was not
247
+
248
+ # Looks for the distance column and replaces it with the distance sql. If an origin was not
232
249
  # passed in and the distance column exists, we leave it to be flagged as bad SQL by the database.
233
250
  # Conditions are either a string or an array. In the case of an array, the first entry contains
234
251
  # the condition.
@@ -244,43 +261,43 @@ module Geokit
244
261
  end
245
262
  arel
246
263
  end
247
-
264
+
248
265
  # Returns the distance SQL using the spherical world formula (Haversine). The SQL is tuned
249
266
  # to the database in use.
250
267
  def sphere_distance_sql(origin, units)
251
268
  lat = deg2rad(origin.lat)
252
269
  lng = deg2rad(origin.lng)
253
270
  multiplier = units_sphere_multiplier(units)
254
-
271
+
255
272
  adapter.sphere_distance_sql(lat, lng, multiplier) if adapter
256
273
  end
257
-
274
+
258
275
  # Returns the distance SQL using the flat-world formula (Phythagorean Theory). The SQL is tuned
259
276
  # to the database in use.
260
277
  def flat_distance_sql(origin, units)
261
278
  lat_degree_units = units_per_latitude_degree(units)
262
279
  lng_degree_units = units_per_longitude_degree(origin.lat, units)
263
-
280
+
264
281
  adapter.flat_distance_sql(origin, lat_degree_units, lng_degree_units)
265
282
  end
266
-
283
+
267
284
  end # ClassMethods
268
285
 
269
286
  # this is the callback for auto_geocoding
270
287
  def auto_geocode_address
271
288
  address=self.send(auto_geocode_field).to_s
272
289
  geo=Geokit::Geocoders::MultiGeocoder.geocode(address)
273
-
290
+
274
291
  if geo.success
275
292
  self.send("#{lat_column_name}=", geo.lat)
276
293
  self.send("#{lng_column_name}=", geo.lng)
277
294
  else
278
- errors.add(auto_geocode_field, auto_geocode_error_message)
295
+ errors.add(auto_geocode_field, auto_geocode_error_message)
279
296
  end
280
-
297
+
281
298
  geo.success
282
299
  end
283
-
300
+
284
301
  def self.end_of_reflection_chain(through, klass)
285
302
  while through
286
303
  reflection = nil
@@ -289,17 +306,17 @@ module Geokit
289
306
  else
290
307
  association, through = through, nil
291
308
  end
292
-
309
+
293
310
  if reflection = klass.reflect_on_association(association)
294
311
  klass = reflection.klass
295
312
  else
296
313
  raise ArgumentError, "You gave #{association} in :through, but I could not find it on #{klass}."
297
314
  end
298
315
  end
299
-
316
+
300
317
  reflection
301
318
  end
302
-
319
+
303
320
  end # ActsAsMappable
304
321
  end # Geokit
305
322
 
@@ -3,7 +3,7 @@ require 'rails'
3
3
 
4
4
  module Geokit
5
5
 
6
- class Railtie < Rails::Railtie
6
+ class Railtie < ::Rails::Railtie
7
7
 
8
8
  config.geokit = ActiveSupport::OrderedOptions.new
9
9
  config.geokit.geocoders = ActiveSupport::OrderedOptions.new
@@ -1,3 +1,3 @@
1
1
  module GeokitRails3
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -3,9 +3,9 @@ require 'test_helper'
3
3
  Geokit::Geocoders::provider_order = [:google, :us]
4
4
 
5
5
  class ActsAsMappableTest < GeokitTestCase
6
-
6
+
7
7
  LOCATION_A_IP = "217.10.83.5"
8
-
8
+
9
9
  def setup
10
10
  @location_a = GeoKit::GeoLoc.new
11
11
  @location_a.lat = 32.918593
@@ -14,439 +14,494 @@ class ActsAsMappableTest < GeokitTestCase
14
14
  @location_a.state = "TX"
15
15
  @location_a.country_code = "US"
16
16
  @location_a.success = true
17
-
17
+
18
18
  @sw = GeoKit::LatLng.new(32.91663,-96.982841)
19
19
  @ne = GeoKit::LatLng.new(32.96302,-96.919495)
20
20
  @bounds_center=GeoKit::LatLng.new((@sw.lat+@ne.lat)/2,(@sw.lng+@ne.lng)/2)
21
-
21
+
22
22
  @starbucks = companies(:starbucks)
23
23
  @loc_a = locations(:a)
24
24
  @custom_loc_a = custom_locations(:a)
25
25
  @loc_e = locations(:e)
26
26
  @custom_loc_e = custom_locations(:e)
27
-
27
+
28
28
  @barnes_and_noble = mock_organizations(:barnes_and_noble)
29
29
  @address = mock_addresses(:address_barnes_and_noble)
30
30
  end
31
-
31
+
32
32
  def test_override_default_units_the_hard_way
33
33
  Location.default_units = :kms
34
- locations = Location.find(:all, :origin => @loc_a, :conditions => "distance < 3.97")
35
- assert_equal 5, locations.size
36
- locations = Location.count(:origin => @loc_a, :conditions => "distance < 3.97")
37
- assert_equal 5, locations
34
+ # locations = Location.find(:all, :origin => @loc_a, :conditions => "distance < 3.97")
35
+ locations = Location.geo_scope(:origin => @loc_a).where("distance < 3.97")
36
+ assert_equal 5, locations.all.size
37
+ # locations = Location.count(:origin => @loc_a, :conditions => "distance < 3.97")
38
+ assert_equal 5, locations.count
38
39
  Location.default_units = :miles
39
40
  end
40
-
41
+
41
42
  def test_include
42
- locations = Location.find(:all, :origin => @loc_a, :include => :company, :conditions => "company_id = 1")
43
+ locations = Location.geo_scope(:origin => @loc_a).includes(:company).where("company_id = 1").all
43
44
  assert !locations.empty?
44
45
  assert_equal 1, locations[0].company.id
45
46
  assert_equal 'Starbucks', locations[0].company.name
46
47
  end
47
-
48
+
48
49
  def test_distance_between_geocoded
49
50
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a)
50
51
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("San Francisco, CA").returns(@location_a)
51
- assert_equal 0, Location.distance_between("Irving, TX", "San Francisco, CA")
52
+ assert_equal 0, Location.distance_between("Irving, TX", "San Francisco, CA")
52
53
  end
53
-
54
+
54
55
  def test_distance_to_geocoded
55
56
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a)
56
- assert_equal 0, @custom_loc_a.distance_to("Irving, TX")
57
+ assert_equal 0, @custom_loc_a.distance_to("Irving, TX")
57
58
  end
58
-
59
+
59
60
  def test_distance_to_geocoded_error
60
61
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(GeoKit::GeoLoc.new)
61
62
  assert_raise(GeoKit::Geocoders::GeocodeError) { @custom_loc_a.distance_to("Irving, TX") }
62
63
  end
63
-
64
+
64
65
  def test_custom_attributes_distance_calculations
65
66
  assert_equal 0, @custom_loc_a.distance_to(@loc_a)
66
67
  assert_equal 0, CustomLocation.distance_between(@custom_loc_a, @loc_a)
67
68
  end
68
-
69
+
69
70
  def test_distance_column_in_select
70
- locations = Location.find(:all, :origin => @loc_a, :order => "distance ASC")
71
- assert_equal 6, locations.size
71
+ # locations = Location.find(:all, :origin => @loc_a, :order => "distance ASC")
72
+ locations = Location.geo_scope(:origin => @loc_a).order("distance ASC")
73
+ assert_equal 6, locations.all.size
72
74
  assert_equal 0, @loc_a.distance_to(locations.first)
73
75
  assert_in_delta 3.97, @loc_a.distance_to(locations.last, :units => :miles, :formula => :sphere), 0.01
74
76
  end
75
-
77
+
76
78
  def test_find_with_distance_condition
77
- locations = Location.find(:all, :origin => @loc_a, :conditions => "distance < 3.97")
78
- assert_equal 5, locations.size
79
- locations = Location.count(:origin => @loc_a, :conditions => "distance < 3.97")
80
- assert_equal 5, locations
81
- end
82
-
79
+ # locations = Location.find(:all, :origin => @loc_a, :conditions => "distance < 3.97")
80
+ locations = Location.geo_scope(:origin => @loc_a, :within => 3.97)
81
+ assert_equal 5, locations.all.size
82
+ # locations = Location.count(:origin => @loc_a, :conditions => "distance < 3.97")
83
+ assert_equal 5, locations.count
84
+ end
85
+
83
86
  def test_find_with_distance_condition_with_units_override
84
- locations = Location.find(:all, :origin => @loc_a, :units => :kms, :conditions => "distance < 6.387")
85
- assert_equal 5, locations.size
86
- locations = Location.count(:origin => @loc_a, :units => :kms, :conditions => "distance < 6.387")
87
- assert_equal 5, locations
87
+ # locations = Location.find(:all, :origin => @loc_a, :units => :kms, :conditions => "distance < 6.387")
88
+ locations = Location.geo_scope(:origin => @loc_a, :units => :kms, :within => 6.387)
89
+ assert_equal 5, locations.all.size
90
+ # locations = Location.count(:origin => @loc_a, :units => :kms, :conditions => "distance < 6.387")
91
+ assert_equal 5, locations.count
88
92
  end
89
-
93
+
90
94
  def test_find_with_distance_condition_with_formula_override
91
- locations = Location.find(:all, :origin => @loc_a, :formula => :flat, :conditions => "distance < 6.387")
92
- assert_equal 6, locations.size
93
- locations = Location.count(:origin => @loc_a, :formula => :flat, :conditions => "distance < 6.387")
94
- assert_equal 6, locations
95
+ # locations = Location.find(:all, :origin => @loc_a, :formula => :flat, :conditions => "distance < 6.387")
96
+ locations = Location.geo_scope(:origin => @loc_a, :formula => :flat, :within => 6.387)
97
+ assert_equal 6, locations.all.size
98
+ # locations = Location.count(:origin => @loc_a, :formula => :flat, :conditions => "distance < 6.387")
99
+ assert_equal 6, locations.count
95
100
  end
96
-
101
+
97
102
  def test_find_within
98
- locations = Location.find_within(3.97, :origin => @loc_a)
99
- assert_equal 5, locations.size
100
- locations = Location.count_within(3.97, :origin => @loc_a)
101
- assert_equal 5, locations
102
- end
103
-
104
- def test_find_within_with_token
105
- locations = Location.find(:all, :within => 3.97, :origin => @loc_a)
106
- assert_equal 5, locations.size
107
- locations = Location.count(:within => 3.97, :origin => @loc_a)
108
- assert_equal 5, locations
109
- end
110
-
103
+ locations = Location.within(3.97, :origin => @loc_a)
104
+ assert_equal 5, locations.all.size
105
+ # locations = Location.count_within(3.97, :origin => @loc_a)
106
+ assert_equal 5, locations.count
107
+ end
108
+
109
+ # def test_find_within_with_token
110
+ # locations = Location.find(:all, :within => 3.97, :origin => @loc_a)
111
+ # assert_equal 5, locations.size
112
+ # locations = Location.count(:within => 3.97, :origin => @loc_a)
113
+ # assert_equal 5, locations
114
+ # end
115
+
111
116
  def test_find_within_with_coordinates
112
- locations = Location.find_within(3.97, :origin =>[@loc_a.lat,@loc_a.lng])
113
- assert_equal 5, locations.size
114
- locations = Location.count_within(3.97, :origin =>[@loc_a.lat,@loc_a.lng])
115
- assert_equal 5, locations
117
+ locations = Location.within(3.97, :origin =>[@loc_a.lat,@loc_a.lng])
118
+ assert_equal 5, locations.all.size
119
+ # locations = Location.count_within(3.97, :origin =>[@loc_a.lat,@loc_a.lng])
120
+ assert_equal 5, locations.count
116
121
  end
117
-
122
+
118
123
  def test_find_with_compound_condition
119
- locations = Location.find(:all, :origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
120
- assert_equal 2, locations.size
121
- locations = Location.count(:origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
122
- assert_equal 2, locations
124
+ # locations = Location.find(:all, :origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
125
+ locations = Location.geo_scope(:origin => @loc_a).where("distance < 5 and city = 'Coppell'")
126
+ assert_equal 2, locations.all.size
127
+ # locations = Location.count(:origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
128
+ assert_equal 2, locations.count
123
129
  end
124
-
130
+
125
131
  def test_find_with_secure_compound_condition
126
- locations = Location.find(:all, :origin => @loc_a, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
127
- assert_equal 2, locations.size
128
- locations = Location.count(:origin => @loc_a, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
129
- assert_equal 2, locations
132
+ # locations = Location.find(:all, :origin => @loc_a, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
133
+ locations = Location.geo_scope(:origin => @loc_a).where(["distance < ? and city = ?", 5, 'Coppell'])
134
+ assert_equal 2, locations.all.size
135
+ # locations = Location.count(:origin => @loc_a, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
136
+ assert_equal 2, locations.count
130
137
  end
131
-
138
+
132
139
  def test_find_beyond
133
- locations = Location.find_beyond(3.95, :origin => @loc_a)
134
- assert_equal 1, locations.size
135
- locations = Location.count_beyond(3.95, :origin => @loc_a)
136
- assert_equal 1, locations
140
+ # locations = Location.find_beyond(3.95, :origin => @loc_a)
141
+ locations = Location.beyond(3.95, :origin => @loc_a)
142
+ assert_equal 1, locations.all.size
143
+ # locations = Location.count_beyond(3.95, :origin => @loc_a)
144
+ assert_equal 1, locations.count
137
145
  end
138
-
146
+
139
147
  def test_find_beyond_with_token
140
- locations = Location.find(:all, :beyond => 3.95, :origin => @loc_a)
141
- assert_equal 1, locations.size
142
- locations = Location.count(:beyond => 3.95, :origin => @loc_a)
143
- assert_equal 1, locations
148
+ # locations = Location.find(:all, :beyond => 3.95, :origin => @loc_a)
149
+ locations = Location.geo_scope(:beyond => 3.95, :origin => @loc_a)
150
+ assert_equal 1, locations.all.size
151
+ # locations = Location.count(:beyond => 3.95, :origin => @loc_a)
152
+ assert_equal 1, locations.count
144
153
  end
145
-
154
+
146
155
  def test_find_beyond_with_coordinates
147
- locations = Location.find_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
148
- assert_equal 1, locations.size
149
- locations = Location.count_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
150
- assert_equal 1, locations
156
+ # locations = Location.find_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
157
+ locations = Location.beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
158
+ assert_equal 1, locations.all.size
159
+ # locations = Location.count_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
160
+ assert_equal 1, locations.count
151
161
  end
152
-
162
+
153
163
  def test_find_range_with_token
154
- locations = Location.find(:all, :range => 0..10, :origin => @loc_a)
155
- assert_equal 6, locations.size
156
- locations = Location.count(:range => 0..10, :origin => @loc_a)
157
- assert_equal 6, locations
164
+ # locations = Location.find(:all, :range => 0..10, :origin => @loc_a)
165
+ locations = Location.geo_scope(:range => 0..10, :origin => @loc_a)
166
+ assert_equal 6, locations.all.size
167
+ # locations = Location.count(:range => 0..10, :origin => @loc_a)
168
+ assert_equal 6, locations.count
158
169
  end
159
-
170
+
160
171
  def test_find_range_with_token_with_conditions
161
- locations = Location.find(:all, :origin => @loc_a, :range => 0..10, :conditions => ["city = ?", 'Coppell'])
162
- assert_equal 2, locations.size
163
- locations = Location.count(:origin => @loc_a, :range => 0..10, :conditions => ["city = ?", 'Coppell'])
164
- assert_equal 2, locations
172
+ # locations = Location.find(:all, :origin => @loc_a, :range => 0..10, :conditions => ["city = ?", 'Coppell'])
173
+ locations = Location.geo_scope(:origin => @loc_a, :range => 0..10).where(["city = ?", 'Coppell'])
174
+ assert_equal 2, locations.all.size
175
+ # locations = Location.count(:origin => @loc_a, :range => 0..10, :conditions => ["city = ?", 'Coppell'])
176
+ assert_equal 2, locations.count
165
177
  end
166
-
178
+
167
179
  def test_find_range_with_token_with_hash_conditions
168
- locations = Location.find(:all, :origin => @loc_a, :range => 0..10, :conditions => {:city => 'Coppell'})
169
- assert_equal 2, locations.size
170
- locations = Location.count(:origin => @loc_a, :range => 0..10, :conditions => {:city => 'Coppell'})
171
- assert_equal 2, locations
180
+ # locations = Location.find(:all, :origin => @loc_a, :range => 0..10, :conditions => {:city => 'Coppell'})
181
+ locations = Location.geo_scope(:origin => @loc_a, :range => 0..10).where(:city => 'Coppell')
182
+ assert_equal 2, locations.all.size
183
+ # locations = Location.count(:origin => @loc_a, :range => 0..10, :conditions => {:city => 'Coppell'})
184
+ assert_equal 2, locations.count
172
185
  end
173
-
186
+
174
187
  def test_find_range_with_token_excluding_end
175
- locations = Location.find(:all, :range => 0...10, :origin => @loc_a)
176
- assert_equal 6, locations.size
177
- locations = Location.count(:range => 0...10, :origin => @loc_a)
178
- assert_equal 6, locations
188
+ # locations = Location.find(:all, :range => 0...10, :origin => @loc_a)
189
+ locations = Location.geo_scope(:range => 0...10, :origin => @loc_a)
190
+ assert_equal 6, locations.all.size
191
+ # locations = Location.count(:range => 0...10, :origin => @loc_a)
192
+ assert_equal 6, locations.count
179
193
  end
180
-
194
+
181
195
  def test_find_nearest
182
- assert_equal @loc_a, Location.find_nearest(:origin => @loc_a)
183
- end
184
-
185
- def test_find_nearest_through_find
186
- assert_equal @loc_a, Location.find(:nearest, :origin => @loc_a)
196
+ # assert_equal @loc_a, Location.find_nearest(:origin => @loc_a)
197
+ assert_equal @loc_a, Location.nearest(:origin => @loc_a).first
187
198
  end
188
-
199
+
200
+ # def test_find_nearest_through_find
201
+ # assert_equal @loc_a, Location.find(:nearest, :origin => @loc_a)
202
+ # end
203
+
189
204
  def test_find_nearest_with_coordinates
190
- assert_equal @loc_a, Location.find_nearest(:origin =>[@loc_a.lat, @loc_a.lng])
205
+ # assert_equal @loc_a, Location.find_nearest(:origin =>[@loc_a.lat, @loc_a.lng])
206
+ assert_equal @loc_a, Location.nearest(:origin =>[@loc_a.lat, @loc_a.lng]).first
191
207
  end
192
-
208
+
193
209
  def test_find_farthest
194
- assert_equal @loc_e, Location.find_farthest(:origin => @loc_a)
210
+ # assert_equal @loc_e, Location.find_farthest(:origin => @loc_a)
211
+ assert_equal @loc_e, Location.farthest(:origin => @loc_a).first
195
212
  end
196
-
197
- def test_find_farthest_through_find
198
- assert_equal @loc_e, Location.find(:farthest, :origin => @loc_a)
199
- end
200
-
213
+
214
+ # def test_find_farthest_through_find
215
+ # assert_equal @loc_e, Location.find(:farthest, :origin => @loc_a)
216
+ # end
217
+
201
218
  def test_find_farthest_with_coordinates
202
- assert_equal @loc_e, Location.find_farthest(:origin =>[@loc_a.lat, @loc_a.lng])
219
+ # assert_equal @loc_e, Location.find_farthest(:origin =>[@loc_a.lat, @loc_a.lng])
220
+ assert_equal @loc_e, Location.farthest(:origin =>[@loc_a.lat, @loc_a.lng]).first
203
221
  end
204
-
222
+
205
223
  def test_scoped_distance_column_in_select
206
- locations = @starbucks.locations.find(:all, :origin => @loc_a, :order => "distance ASC")
207
- assert_equal 5, locations.size
224
+ # locations = @starbucks.locations.find(:all, :origin => @loc_a, :order => "distance ASC")
225
+ locations = @starbucks.locations.geo_scope(:origin => @loc_a).order("distance ASC")
226
+ assert_equal 5, locations.all.size
208
227
  assert_equal 0, @loc_a.distance_to(locations.first)
209
228
  assert_in_delta 3.97, @loc_a.distance_to(locations.last, :units => :miles, :formula => :sphere), 0.01
210
229
  end
211
-
230
+
212
231
  def test_scoped_find_with_distance_condition
213
- locations = @starbucks.locations.find(:all, :origin => @loc_a, :conditions => "distance < 3.97")
214
- assert_equal 4, locations.size
215
- locations = @starbucks.locations.count(:origin => @loc_a, :conditions => "distance < 3.97")
216
- assert_equal 4, locations
217
- end
218
-
232
+ # locations = @starbucks.locations.find(:all, :origin => @loc_a, :conditions => "distance < 3.97")
233
+ locations = @starbucks.locations.geo_scope(:origin => @loc_a).where("distance < 3.97")
234
+ assert_equal 4, locations.all.size
235
+ # locations = @starbucks.locations.count(:origin => @loc_a, :conditions => "distance < 3.97")
236
+ assert_equal 4, locations.count
237
+ end
238
+
219
239
  def test_scoped_find_within
220
- locations = @starbucks.locations.find_within(3.97, :origin => @loc_a)
221
- assert_equal 4, locations.size
222
- locations = @starbucks.locations.count_within(3.97, :origin => @loc_a)
223
- assert_equal 4, locations
240
+ # locations = @starbucks.locations.find_within(3.97, :origin => @loc_a)
241
+ locations = @starbucks.locations.within(3.97, :origin => @loc_a)
242
+ assert_equal 4, locations.all.size
243
+ # locations = @starbucks.locations.count_within(3.97, :origin => @loc_a)
244
+ assert_equal 4, locations.count
224
245
  end
225
-
246
+
226
247
  def test_scoped_find_with_compound_condition
227
- locations = @starbucks.locations.find(:all, :origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
228
- assert_equal 2, locations.size
229
- locations = @starbucks.locations.count( :origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
230
- assert_equal 2, locations
248
+ # locations = @starbucks.locations.find(:all, :origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
249
+ locations = @starbucks.locations.geo_scope(:origin => @loc_a).where("distance < 5 and city = 'Coppell'")
250
+ assert_equal 2, locations.all.size
251
+ # locations = @starbucks.locations.count( :origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
252
+ assert_equal 2, locations.count
231
253
  end
232
-
254
+
233
255
  def test_scoped_find_beyond
234
- locations = @starbucks.locations.find_beyond(3.95, :origin => @loc_a)
235
- assert_equal 1, locations.size
236
- locations = @starbucks.locations.count_beyond(3.95, :origin => @loc_a)
237
- assert_equal 1, locations
256
+ # locations = @starbucks.locations.find_beyond(3.95, :origin => @loc_a)
257
+ locations = @starbucks.locations.beyond(3.95, :origin => @loc_a)
258
+ assert_equal 1, locations.all.size
259
+ # locations = @starbucks.locations.count_beyond(3.95, :origin => @loc_a)
260
+ assert_equal 1, locations.count
238
261
  end
239
-
262
+
240
263
  def test_scoped_find_nearest
241
- assert_equal @loc_a, @starbucks.locations.find_nearest(:origin => @loc_a)
264
+ # assert_equal @loc_a, @starbucks.locations.find_nearest(:origin => @loc_a).first
265
+ assert_equal @loc_a, @starbucks.locations.nearest(:origin => @loc_a).first
242
266
  end
243
-
267
+
244
268
  def test_scoped_find_farthest
245
- assert_equal @loc_e, @starbucks.locations.find_farthest(:origin => @loc_a)
246
- end
247
-
269
+ # assert_equal @loc_e, @starbucks.locations.find_farthest(:origin => @loc_a).first
270
+ assert_equal @loc_e, @starbucks.locations.farthest(:origin => @loc_a).first
271
+ end
272
+
248
273
  def test_ip_geocoded_distance_column_in_select
249
274
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
250
- locations = Location.find(:all, :origin => LOCATION_A_IP, :order => "distance ASC")
251
- assert_equal 6, locations.size
275
+ # locations = Location.find(:all, :origin => LOCATION_A_IP, :order => "distance ASC")
276
+ locations = Location.geo_scope(:origin => LOCATION_A_IP).order("distance ASC")
277
+ assert_equal 6, locations.all.size
252
278
  assert_equal 0, @loc_a.distance_to(locations.first)
253
279
  assert_in_delta 3.97, @loc_a.distance_to(locations.last, :units => :miles, :formula => :sphere), 0.01
254
280
  end
255
-
281
+
256
282
  def test_ip_geocoded_find_with_distance_condition
257
283
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
258
- locations = Location.find(:all, :origin => LOCATION_A_IP, :conditions => "distance < 3.97")
259
- assert_equal 5, locations.size
260
- GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
261
- locations = Location.count(:origin => LOCATION_A_IP, :conditions => "distance < 3.97")
262
- assert_equal 5, locations
263
- end
264
-
284
+ # locations = Location.find(:all, :origin => LOCATION_A_IP, :conditions => "distance < 3.97")
285
+ locations = Location.geo_scope(:origin => LOCATION_A_IP).where2("distance < 3.97")
286
+ assert_equal 5, locations.all.size
287
+ # GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
288
+ # locations = Location.count(:origin => LOCATION_A_IP, :conditions => "distance < 3.97")
289
+ assert_equal 5, locations.count
290
+ end
291
+
265
292
  def test_ip_geocoded_find_within
266
293
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
267
- locations = Location.find_within(3.97, :origin => LOCATION_A_IP)
268
- assert_equal 5, locations.size
269
- GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
270
- locations = Location.count_within(3.97, :origin => LOCATION_A_IP)
271
- assert_equal 5, locations
294
+ # locations = Location.find_within(3.97, :origin => LOCATION_A_IP)
295
+ locations = Location.within(3.97, :origin => LOCATION_A_IP)
296
+ assert_equal 5, locations.all.size
297
+ # GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
298
+ # locations = Location.count_within(3.97, :origin => LOCATION_A_IP)
299
+ assert_equal 5, locations.count
272
300
  end
273
-
301
+
274
302
  def test_ip_geocoded_find_with_compound_condition
275
303
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
276
- locations = Location.find(:all, :origin => LOCATION_A_IP, :conditions => "distance < 5 and city = 'Coppell'")
277
- assert_equal 2, locations.size
278
- GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
279
- locations = Location.count(:origin => LOCATION_A_IP, :conditions => "distance < 5 and city = 'Coppell'")
280
- assert_equal 2, locations
304
+ # locations = Location.find(:all, :origin => LOCATION_A_IP, :conditions => "distance < 5 and city = 'Coppell'")
305
+ locations = Location.geo_scope(:origin => LOCATION_A_IP).where("distance < 5 and city = 'Coppell'")
306
+ assert_equal 2, locations.all.size
307
+ # GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
308
+ # locations = Location.count(:origin => LOCATION_A_IP, :conditions => "distance < 5 and city = 'Coppell'")
309
+ assert_equal 2, locations.count
281
310
  end
282
-
311
+
283
312
  def test_ip_geocoded_find_with_secure_compound_condition
284
313
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
285
- locations = Location.find(:all, :origin => LOCATION_A_IP, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
286
- assert_equal 2, locations.size
287
- GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
288
- locations = Location.count(:origin => LOCATION_A_IP, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
289
- assert_equal 2, locations
314
+ # locations = Location.find(:all, :origin => LOCATION_A_IP, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
315
+ locations = Location.geo_scope(:origin => LOCATION_A_IP).where(["distance < ? and city = ?", 5, 'Coppell'])
316
+ assert_equal 2, locations.all.size
317
+ # GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
318
+ # locations = Location.count(:origin => LOCATION_A_IP, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
319
+ assert_equal 2, locations.count
290
320
  end
291
-
321
+
292
322
  def test_ip_geocoded_find_beyond
293
323
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
294
- locations = Location.find_beyond(3.95, :origin => LOCATION_A_IP)
295
- assert_equal 1, locations.size
296
- GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
297
- locations = Location.count_beyond(3.95, :origin => LOCATION_A_IP)
298
- assert_equal 1, locations
324
+ # locations = Location.find_beyond(3.95, :origin => LOCATION_A_IP)
325
+ locations = Location.beyond(3.95, :origin => LOCATION_A_IP)
326
+ assert_equal 1, locations.all.size
327
+ # GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
328
+ # locations = Location.count_beyond(3.95, :origin => LOCATION_A_IP)
329
+ assert_equal 1, locations.count
299
330
  end
300
-
331
+
301
332
  def test_ip_geocoded_find_nearest
302
333
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
303
- assert_equal @loc_a, Location.find_nearest(:origin => LOCATION_A_IP)
334
+ # assert_equal @loc_a, Location.find_nearest(:origin => LOCATION_A_IP)
335
+ assert_equal @loc_a, Location.nearest(:origin => LOCATION_A_IP).first
304
336
  end
305
-
337
+
306
338
  def test_ip_geocoded_find_farthest
307
339
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
308
- assert_equal @loc_e, Location.find_farthest(:origin => LOCATION_A_IP)
340
+ # assert_equal @loc_e, Location.find_farthest(:origin => LOCATION_A_IP)
341
+ assert_equal @loc_e, Location.farthest(:origin => LOCATION_A_IP).first
309
342
  end
310
-
343
+
311
344
  def test_ip_geocoder_exception
312
345
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with('127.0.0.1').returns(GeoKit::GeoLoc.new)
313
346
  assert_raises GeoKit::Geocoders::GeocodeError do
314
- Location.find_farthest(:origin => '127.0.0.1')
347
+ # Location.find_farthest(:origin => '127.0.0.1')
348
+ Location.farthest(:origin => '127.0.0.1').first
315
349
  end
316
350
  end
317
-
351
+
318
352
  def test_address_geocode
319
- GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with('Irving, TX').returns(@location_a)
320
- locations = Location.find(:all, :origin => 'Irving, TX', :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
321
- assert_equal 2, locations.size
353
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with('Irving, TX').returns(@location_a)
354
+ # locations = Location.find(:all, :origin => 'Irving, TX', :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
355
+ locations = Location.geo_scope(:origin => 'Irving, TX').where(["distance < ? and city = ?", 5, 'Coppell'])
356
+ assert_equal 2, locations.all.size
357
+ assert_equal 2, locations.count
322
358
  end
323
-
359
+
324
360
  def test_find_with_custom_distance_condition
325
- locations = CustomLocation.find(:all, :origin => @loc_a, :conditions => "dist < 3.97")
326
- assert_equal 5, locations.size
327
- locations = CustomLocation.count(:origin => @loc_a, :conditions => "dist < 3.97")
328
- assert_equal 5, locations
329
- end
330
-
361
+ # locations = CustomLocation.find(:all, :origin => @loc_a, :conditions => "dist < 3.97")
362
+ locations = CustomLocation.geo_scope(:origin => @loc_a).where("dist < 3.97")
363
+ assert_equal 5, locations.all.size
364
+ # locations = CustomLocation.count(:origin => @loc_a, :conditions => "dist < 3.97")
365
+ assert_equal 5, locations.count
366
+ end
367
+
331
368
  def test_find_with_custom_distance_condition_using_custom_origin
332
- locations = CustomLocation.find(:all, :origin => @custom_loc_a, :conditions => "dist < 3.97")
333
- assert_equal 5, locations.size
334
- locations = CustomLocation.count(:origin => @custom_loc_a, :conditions => "dist < 3.97")
335
- assert_equal 5, locations
369
+ # locations = CustomLocation.find(:all, :origin => @custom_loc_a, :conditions => "dist < 3.97")
370
+ locations = CustomLocation.geo_scope(:origin => @custom_loc_a).where("dist < 3.97")
371
+ assert_equal 5, locations.all.size
372
+ locations = CustomLocation.count(:origin => @custom_loc_a).where("dist < 3.97")
373
+ assert_equal 5, locations.count
336
374
  end
337
-
375
+
338
376
  def test_find_within_with_custom
339
- locations = CustomLocation.find_within(3.97, :origin => @loc_a)
340
- assert_equal 5, locations.size
341
- locations = CustomLocation.count_within(3.97, :origin => @loc_a)
342
- assert_equal 5, locations
377
+ # locations = CustomLocation.find_within(3.97, :origin => @loc_a)
378
+ locations = CustomLocation.within(3.97, :origin => @loc_a)
379
+ assert_equal 5, locations.all.size
380
+ # locations = CustomLocation.count_within(3.97, :origin => @loc_a)
381
+ assert_equal 5, locations.count
343
382
  end
344
-
383
+
345
384
  def test_find_within_with_coordinates_with_custom
346
- locations = CustomLocation.find_within(3.97, :origin =>[@loc_a.lat, @loc_a.lng])
347
- assert_equal 5, locations.size
348
- locations = CustomLocation.count_within(3.97, :origin =>[@loc_a.lat, @loc_a.lng])
349
- assert_equal 5, locations
385
+ # locations = CustomLocation.find_within(3.97, :origin =>[@loc_a.lat, @loc_a.lng])
386
+ locations = CustomLocation.within(3.97, :origin =>[@loc_a.lat, @loc_a.lng])
387
+ assert_equal 5, locations.all.size
388
+ # locations = CustomLocation.count_within(3.97, :origin =>[@loc_a.lat, @loc_a.lng])
389
+ assert_equal 5, locations.count
350
390
  end
351
-
391
+
352
392
  def test_find_with_compound_condition_with_custom
353
- locations = CustomLocation.find(:all, :origin => @loc_a, :conditions => "dist < 5 and city = 'Coppell'")
354
- assert_equal 1, locations.size
355
- locations = CustomLocation.count(:origin => @loc_a, :conditions => "dist < 5 and city = 'Coppell'")
356
- assert_equal 1, locations
393
+ # locations = CustomLocation.find(:all, :origin => @loc_a, :conditions => "dist < 5 and city = 'Coppell'")
394
+ locations = CustomLocation.geo_scope(:origin => @loc_a).where("dist < 5 and city = 'Coppell'")
395
+ assert_equal 1, locations.all.size
396
+ # locations = CustomLocation.count(:origin => @loc_a, :conditions => "dist < 5 and city = 'Coppell'")
397
+ assert_equal 1, locations.count
357
398
  end
358
-
399
+
359
400
  def test_find_with_secure_compound_condition_with_custom
360
- locations = CustomLocation.find(:all, :origin => @loc_a, :conditions => ["dist < ? and city = ?", 5, 'Coppell'])
361
- assert_equal 1, locations.size
362
- locations = CustomLocation.count(:origin => @loc_a, :conditions => ["dist < ? and city = ?", 5, 'Coppell'])
363
- assert_equal 1, locations
401
+ # locations = CustomLocation.find(:all, :origin => @loc_a, :conditions => ["dist < ? and city = ?", 5, 'Coppell'])
402
+ locations = CustomLocation.geo_scope(:origin => @loc_a).where(["dist < ? and city = ?", 5, 'Coppell'])
403
+ assert_equal 1, locations.all.size
404
+ # locations = CustomLocation.count(:origin => @loc_a, :conditions => ["dist < ? and city = ?", 5, 'Coppell'])
405
+ assert_equal 1, locations.count
364
406
  end
365
-
407
+
366
408
  def test_find_beyond_with_custom
367
- locations = CustomLocation.find_beyond(3.95, :origin => @loc_a)
368
- assert_equal 1, locations.size
369
- locations = CustomLocation.count_beyond(3.95, :origin => @loc_a)
370
- assert_equal 1, locations
409
+ # locations = CustomLocation.find_beyond(3.95, :origin => @loc_a)
410
+ locations = CustomLocation.beyond(3.95, :origin => @loc_a)
411
+ assert_equal 1, locations.all.size
412
+ # locations = CustomLocation.count_beyond(3.95, :origin => @loc_a)
413
+ assert_equal 1, locations.count
371
414
  end
372
-
415
+
373
416
  def test_find_beyond_with_coordinates_with_custom
374
- locations = CustomLocation.find_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
375
- assert_equal 1, locations.size
376
- locations = CustomLocation.count_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
377
- assert_equal 1, locations
417
+ # locations = CustomLocation.find_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
418
+ locations = CustomLocation.beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
419
+ assert_equal 1, locations.all.size
420
+ # locations = CustomLocation.count_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
421
+ assert_equal 1, locations.count
378
422
  end
379
-
423
+
380
424
  def test_find_nearest_with_custom
381
- assert_equal @custom_loc_a, CustomLocation.find_nearest(:origin => @loc_a)
425
+ # assert_equal @custom_loc_a, CustomLocation.find_nearest(:origin => @loc_a)
426
+ assert_equal @custom_loc_a, CustomLocation.nearest(:origin => @loc_a).first
382
427
  end
383
-
428
+
384
429
  def test_find_nearest_with_coordinates_with_custom
385
- assert_equal @custom_loc_a, CustomLocation.find_nearest(:origin =>[@loc_a.lat, @loc_a.lng])
430
+ # assert_equal @custom_loc_a, CustomLocation.find_nearest(:origin =>[@loc_a.lat, @loc_a.lng])
431
+ assert_equal @custom_loc_a, CustomLocation.nearest(:origin =>[@loc_a.lat, @loc_a.lng]).first
386
432
  end
387
-
433
+
388
434
  def test_find_farthest_with_custom
389
- assert_equal @custom_loc_e, CustomLocation.find_farthest(:origin => @loc_a)
435
+ # assert_equal @custom_loc_e, CustomLocation.find_farthest(:origin => @loc_a)
436
+ assert_equal @custom_loc_e, CustomLocation.farthest(:origin => @loc_a).first
390
437
  end
391
-
438
+
392
439
  def test_find_farthest_with_coordinates_with_custom
393
- assert_equal @custom_loc_e, CustomLocation.find_farthest(:origin =>[@loc_a.lat, @loc_a.lng])
440
+ # assert_equal @custom_loc_e, CustomLocation.find_farthest(:origin =>[@loc_a.lat, @loc_a.lng])
441
+ assert_equal @custom_loc_e, CustomLocation.farthest(:origin =>[@loc_a.lat, @loc_a.lng]).first
394
442
  end
395
-
443
+
396
444
  def test_find_with_array_origin
397
- locations = Location.find(:all, :origin =>[@loc_a.lat,@loc_a.lng], :conditions => "distance < 3.97")
398
- assert_equal 5, locations.size
399
- locations = Location.count(:origin =>[@loc_a.lat,@loc_a.lng], :conditions => "distance < 3.97")
400
- assert_equal 5, locations
445
+ # locations = Location.find(:all, :origin =>[@loc_a.lat,@loc_a.lng], :conditions => "distance < 3.97")
446
+ locations = Location.geo_scope(:origin =>[@loc_a.lat,@loc_a.lng]).where("distance < 3.97")
447
+ assert_equal 5, locations.all.size
448
+ # locations = Location.count(:origin =>[@loc_a.lat,@loc_a.lng], :conditions => "distance < 3.97")
449
+ assert_equal 5, locations.count
401
450
  end
402
-
403
-
451
+
452
+
404
453
  # Bounding box tests
405
-
454
+
406
455
  def test_find_within_bounds
407
- locations = Location.find_within_bounds([@sw,@ne])
408
- assert_equal 2, locations.size
409
- locations = Location.count_within_bounds([@sw,@ne])
410
- assert_equal 2, locations
456
+ # locations = Location.find_within_bounds([@sw,@ne])
457
+ locations = Location.in_bounds([@sw,@ne])
458
+ assert_equal 2, locations.all.size
459
+ # locations = Location.count_within_bounds([@sw,@ne])
460
+ assert_equal 2, locations.count
411
461
  end
412
-
462
+
413
463
  def test_find_within_bounds_ordered_by_distance
414
- locations = Location.find_within_bounds([@sw,@ne], :origin=>@bounds_center, :order=>'distance asc')
464
+ # locations = Location.find_within_bounds([@sw,@ne], :origin=>@bounds_center, :order=>'distance asc')
465
+ locations = Location.in_bounds([@sw,@ne], :origin=>@bounds_center).order('distance asc')
415
466
  assert_equal locations[0], locations(:d)
416
467
  assert_equal locations[1], locations(:a)
417
468
  end
418
-
469
+
419
470
  def test_find_within_bounds_with_token
420
- locations = Location.find(:all, :bounds=>[@sw,@ne])
421
- assert_equal 2, locations.size
422
- locations = Location.count(:bounds=>[@sw,@ne])
423
- assert_equal 2, locations
471
+ # locations = Location.find(:all, :bounds=>[@sw,@ne])
472
+ locations = Location.geo_scope(:bounds=>[@sw,@ne])
473
+ assert_equal 2, locations.all.size
474
+ # locations = Location.count(:bounds=>[@sw,@ne])
475
+ assert_equal 2, locations.count
424
476
  end
425
-
477
+
426
478
  def test_find_within_bounds_with_string_conditions
427
- locations = Location.find(:all, :bounds=>[@sw,@ne], :conditions=>"id !=#{locations(:a).id}")
428
- assert_equal 1, locations.size
479
+ # locations = Location.find(:all, :bounds=>[@sw,@ne], :conditions=>"id !=#{locations(:a).id}")
480
+ locations = Location.geo_scope(:bounds=>[@sw,@ne]).where("id !=#{locations(:a).id}")
481
+ assert_equal 1, locations.all.size
429
482
  end
430
-
483
+
431
484
  def test_find_within_bounds_with_array_conditions
432
- locations = Location.find(:all, :bounds=>[@sw,@ne], :conditions=>["id != ?", locations(:a).id])
433
- assert_equal 1, locations.size
485
+ # locations = Location.find(:all, :bounds=>[@sw,@ne], :conditions=>["id != ?", locations(:a).id])
486
+ locations = Location.geo_scope(:bounds=>[@sw,@ne]).where(["id != ?", locations(:a).id])
487
+ assert_equal 1, locations.all.size
434
488
  end
435
-
489
+
436
490
  def test_find_within_bounds_with_hash_conditions
437
- locations = Location.find(:all, :bounds=>[@sw,@ne], :conditions=>{:id => locations(:a).id})
438
- assert_equal 1, locations.size
491
+ # locations = Location.find(:all, :bounds=>[@sw,@ne], :conditions=>{:id => locations(:a).id})
492
+ locations = Location.geo_scope(:bounds=>[@sw,@ne]).where({:id => locations(:a).id})
493
+ assert_equal 1, locations.all.size
439
494
  end
440
-
495
+
441
496
  def test_auto_geocode
442
497
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a)
443
498
  store=Store.new(:address=>'Irving, TX')
444
499
  store.save
445
- assert_equal store.lat,@location_a.lat
500
+ assert_equal store.lat,@location_a.lat
446
501
  assert_equal store.lng,@location_a.lng
447
502
  assert_equal 0, store.errors.size
448
503
  end
449
-
504
+
450
505
  def test_auto_geocode_failure
451
506
  GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("BOGUS").returns(GeoKit::GeoLoc.new)
452
507
  store=Store.new(:address=>'BOGUS')
@@ -454,20 +509,23 @@ class ActsAsMappableTest < GeokitTestCase
454
509
  assert store.new_record?
455
510
  assert_equal 1, store.errors.size
456
511
  end
457
-
512
+
458
513
  # Test :through
459
-
514
+
460
515
  def test_find_with_through
461
- organizations = MockOrganization.find(:all, :origin => @location_a, :order => 'distance ASC')
462
- assert_equal 2, organizations.size
463
- organizations = MockOrganization.count(:origin => @location_a, :conditions => "distance < 3.97")
464
- assert_equal 1, organizations
516
+ # organizations = MockOrganization.find(:all, :origin => @location_a, :order => 'distance ASC')
517
+ organizations = MockOrganization.geo_scope(:origin => @location_a).order('distance ASC')
518
+ assert_equal 2, organizations.all.size
519
+ # organizations = MockOrganization.count(:origin => @location_a, :conditions => "distance < 3.97")
520
+ organizations = MockOrganization.geo_scope(:origin => @location_a).where("distance < 3.97")
521
+ assert_equal 1, organizations.count
465
522
  end
466
-
523
+
467
524
  def test_find_with_through_with_hash
468
- people = MockPerson.find(:all, :origin => @location_a, :order => 'distance ASC')
525
+ # people = MockPerson.find(:all, :origin => @location_a, :order => 'distance ASC')
526
+ people = MockPerson.geo_scope(:origin => @location_a).order('distance ASC')
469
527
  assert_equal 2, people.size
470
- people = MockPerson.count(:origin => @location_a, :conditions => "distance < 3.97")
528
+ # people = MockPerson.count(:origin => @location_a, :conditions => "distance < 3.97")
471
529
  assert_equal 2, people
472
- end
530
+ end
473
531
  end