geokit-rails 2.5.0 → 2.5.1

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.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +80 -0
  3. data/.gitignore +44 -37
  4. data/CHANGELOG.md +106 -101
  5. data/CONFIG.markdown +67 -67
  6. data/Gemfile +3 -3
  7. data/MIT-LICENSE +19 -19
  8. data/README.markdown +726 -726
  9. data/Rakefile +17 -17
  10. data/gemfiles/rails7.1.gemfile +11 -0
  11. data/gemfiles/{rails5.gemfile → rails7.2.gemfile} +7 -7
  12. data/gemfiles/{rails6.0.gemfile → rails8.0.gemfile} +7 -8
  13. data/gemfiles/{rails6.1.gemfile → rails8.1.gemfile} +7 -8
  14. data/geokit-rails.gemspec +34 -34
  15. data/lib/generators/geokit_rails/install_generator.rb +13 -13
  16. data/lib/generators/templates/geokit_config.rb +100 -100
  17. data/lib/geokit-rails/acts_as_mappable/class_methods.rb +299 -297
  18. data/lib/geokit-rails/acts_as_mappable/glue.rb +54 -54
  19. data/lib/geokit-rails/acts_as_mappable.rb +78 -78
  20. data/lib/geokit-rails/adapters/abstract.rb +30 -30
  21. data/lib/geokit-rails/adapters/mysql.rb +21 -21
  22. data/lib/geokit-rails/adapters/mysql2.rb +8 -8
  23. data/lib/geokit-rails/adapters/mysql2spatial.rb +8 -8
  24. data/lib/geokit-rails/adapters/oracleenhanced.rb +32 -32
  25. data/lib/geokit-rails/adapters/postgresql.rb +21 -21
  26. data/lib/geokit-rails/adapters/sqlite.rb +53 -53
  27. data/lib/geokit-rails/adapters/sqlserver.rb +45 -45
  28. data/lib/geokit-rails/distance_collection.rb +8 -8
  29. data/lib/geokit-rails/geocoder_control.rb +19 -19
  30. data/lib/geokit-rails/ip_geocode_lookup.rb +44 -45
  31. data/lib/geokit-rails/railtie.rb +37 -37
  32. data/lib/geokit-rails/version.rb +3 -3
  33. data/lib/geokit-rails.rb +11 -11
  34. data/test/acts_as_mappable_test.rb +486 -478
  35. data/test/adapter_resolution_test.rb +46 -0
  36. data/test/boot.rb +40 -32
  37. data/test/database.yml +31 -28
  38. data/test/distance_collection_test.rb +41 -41
  39. data/test/dummy/Rakefile +6 -6
  40. data/test/dummy/app/assets/stylesheets/application.css +1 -1
  41. data/test/dummy/app/channels/application_cable/channel.rb +4 -4
  42. data/test/dummy/app/channels/application_cable/connection.rb +4 -4
  43. data/test/dummy/app/controllers/application_controller.rb +3 -3
  44. data/test/dummy/app/controllers/location_aware_controller.rb +42 -42
  45. data/test/dummy/app/helpers/application_helper.rb +2 -2
  46. data/test/dummy/app/jobs/application_job.rb +7 -7
  47. data/test/dummy/app/mailers/application_mailer.rb +4 -4
  48. data/test/dummy/app/models/application_record.rb +3 -3
  49. data/test/dummy/app/views/layouts/application.html.erb +15 -15
  50. data/test/dummy/app/views/layouts/mailer.html.erb +13 -13
  51. data/test/dummy/app/views/layouts/mailer.text.erb +1 -1
  52. data/test/dummy/bin/rails +4 -4
  53. data/test/dummy/bin/rake +4 -4
  54. data/test/dummy/bin/setup +33 -33
  55. data/test/dummy/config/application.rb +22 -22
  56. data/test/dummy/config/boot.rb +5 -5
  57. data/test/dummy/config/cable.yml +10 -10
  58. data/test/dummy/config/database.yml +25 -25
  59. data/test/dummy/config/environment.rb +5 -5
  60. data/test/dummy/config/environments/development.rb +68 -68
  61. data/test/dummy/config/environments/production.rb +87 -87
  62. data/test/dummy/config/environments/test.rb +60 -60
  63. data/test/dummy/config/initializers/content_security_policy.rb +25 -25
  64. data/test/dummy/config/initializers/filter_parameter_logging.rb +8 -8
  65. data/test/dummy/config/initializers/inflections.rb +16 -16
  66. data/test/dummy/config/initializers/permissions_policy.rb +11 -11
  67. data/test/dummy/config/locales/en.yml +33 -33
  68. data/test/dummy/config/puma.rb +43 -43
  69. data/test/dummy/config/routes.rb +6 -6
  70. data/test/dummy/config/storage.yml +34 -34
  71. data/test/dummy/config.ru +6 -6
  72. data/test/dummy/public/404.html +67 -67
  73. data/test/dummy/public/422.html +67 -67
  74. data/test/dummy/public/500.html +66 -66
  75. data/test/fixtures/companies.yml +9 -9
  76. data/test/fixtures/custom_locations.yml +61 -61
  77. data/test/fixtures/locations.yml +61 -61
  78. data/test/fixtures/mock_addresses.yml +16 -16
  79. data/test/fixtures/mock_families.yml +2 -2
  80. data/test/fixtures/mock_houses.yml +8 -8
  81. data/test/fixtures/mock_organizations.yml +4 -4
  82. data/test/fixtures/mock_people.yml +4 -4
  83. data/test/ip_geocode_lookup_test.rb +52 -48
  84. data/test/models/company.rb +2 -2
  85. data/test/models/custom_location.rb +11 -11
  86. data/test/models/location.rb +3 -3
  87. data/test/models/mock_address.rb +3 -3
  88. data/test/models/mock_family.rb +4 -4
  89. data/test/models/mock_house.rb +2 -2
  90. data/test/models/mock_organization.rb +5 -5
  91. data/test/models/mock_person.rb +6 -6
  92. data/test/models/store.rb +2 -2
  93. data/test/schema.rb +61 -61
  94. data/test/tasks.rake +37 -37
  95. data/test/test_helper.rb +83 -73
  96. metadata +13 -15
  97. data/.travis.yml +0 -55
  98. data/gemfiles/rails3.gemfile +0 -8
  99. data/gemfiles/rails4.gemfile +0 -8
@@ -1,297 +1,299 @@
1
- module Geokit
2
- module ActsAsMappable
3
-
4
- # Class methods included in models when +acts_as_mappable+ is called
5
- module ClassMethods
6
-
7
- # A proxy to an instance of a finder adapter, inferred from the connection's adapter.
8
- def geokit_finder_adapter
9
- @geokit_finder_adapter ||= begin
10
- unless Adapters.const_defined?(connection.adapter_name.camelcase)
11
- filename = connection.adapter_name.downcase
12
- require File.join("geokit-rails", "adapters", filename)
13
- end
14
- klass = Adapters.const_get(connection.adapter_name.camelcase)
15
- if klass.class == Module
16
- # For some reason Mysql2 adapter was defined in Adapters.constants but was Module instead of a Class
17
- filename = connection.adapter_name.downcase
18
- require File.join("geokit-rails", "adapters", filename)
19
- # Re-init the klass after require
20
- klass = Adapters.const_get(connection.adapter_name.camelcase)
21
- end
22
- klass.load(self) unless klass.loaded || skip_loading
23
- klass.new(self)
24
- rescue LoadError
25
- raise UnsupportedAdapter, "`#{connection.adapter_name.downcase}` is not a supported adapter."
26
- end
27
- end
28
-
29
- def within(distance, options = {})
30
- options[:within] = distance
31
- # Add bounding box to speed up SQL request.
32
- bounds = formulate_bounds_from_distance(
33
- options,
34
- normalize_point_to_lat_lng(options[:origin]),
35
- options[:units] || default_units)
36
- with_latlng.where(bound_conditions(bounds)).
37
- where(distance_conditions(options))
38
- end
39
- alias inside within
40
-
41
- def beyond(distance, options = {})
42
- options[:beyond] = distance
43
- #geo_scope(options)
44
- where(distance_conditions(options))
45
- end
46
- alias outside beyond
47
-
48
- def in_range(range, options = {})
49
- options[:range] = range
50
- #geo_scope(options)
51
- where(distance_conditions(options))
52
- end
53
-
54
- def in_bounds(bounds, options = {})
55
- inclusive = options.delete(:inclusive) || false
56
- options[:bounds] = bounds
57
- #geo_scope(options)
58
- #where(distance_conditions(options))
59
- bounds = extract_bounds_from_options(options)
60
- where(bound_conditions(bounds, inclusive))
61
- end
62
-
63
- def by_distance(options = {})
64
- origin = extract_origin_from_options(options)
65
- units = extract_units_from_options(options)
66
- formula = extract_formula_from_options(options)
67
- distance_column_name = distance_sql(origin, units, formula)
68
- with_latlng.order(
69
- Arel.sql(distance_column_name).send(options[:reverse] ? 'desc' : 'asc')
70
- )
71
- end
72
-
73
- def with_latlng
74
- where("#{qualified_lat_column_name} IS NOT NULL AND #{qualified_lng_column_name} IS NOT NULL")
75
- end
76
-
77
- def closest(options = {})
78
- by_distance(options).limit(1)
79
- end
80
- alias nearest closest
81
-
82
- def farthest(options = {})
83
- by_distance({:reverse => true}.merge(options)).limit(1)
84
- end
85
-
86
- #def geo_scope(options = {})
87
- # arel = self.is_a?(ActiveRecord::Relation) ? self : self.scoped
88
-
89
- # origin = extract_origin_from_options(options)
90
- # units = extract_units_from_options(options)
91
- # formula = extract_formula_from_options(options)
92
- # bounds = extract_bounds_from_options(options)
93
-
94
- # if origin || bounds
95
- # bounds = formulate_bounds_from_distance(options, origin, units) unless bounds
96
-
97
- # if origin
98
- # arel.distance_formula = distance_sql(origin, units, formula)
99
- #
100
- # if arel.select_values.blank?
101
- # star_select = Arel::Nodes::SqlLiteral.new(arel.quoted_table_name + '.*')
102
- # arel = arel.select(star_select)
103
- # end
104
- # end
105
-
106
- # if bounds
107
- # bound_conditions = bound_conditions(bounds)
108
- # arel = arel.where(bound_conditions) if bound_conditions
109
- # end
110
-
111
- # distance_conditions = distance_conditions(options)
112
- # arel = arel.where(distance_conditions) if distance_conditions
113
-
114
- # if self.through
115
- # arel = arel.includes(self.through)
116
- # end
117
- # end
118
-
119
- # arel
120
- #end
121
-
122
- # Returns the distance calculation to be used as a display column or a condition. This
123
- # is provide for anyone wanting access to the raw SQL.
124
- def distance_sql(origin, units=default_units, formula=default_formula)
125
- case formula
126
- when :sphere
127
- sql = sphere_distance_sql(origin, units)
128
- when :flat
129
- sql = flat_distance_sql(origin, units)
130
- end
131
- sql
132
- end
133
-
134
- private
135
-
136
- # Override ActiveRecord::Base.relation to return an instance of Geokit::ActsAsMappable::Relation.
137
- # TODO: Do we need to override JoinDependency#relation too?
138
- #def relation
139
- # # NOTE: This cannot be @relation as ActiveRecord already uses this to
140
- # # cache *its* Relation object
141
- # @_geokit_relation ||= Relation.new(self, arel_table)
142
- # finder_needs_type_condition? ? @_geokit_relation.where(type_condition) : @_geokit_relation
143
- #end
144
-
145
- # If it's a :within query, add a bounding box to improve performance.
146
- # This only gets called if a :bounds argument is not otherwise supplied.
147
- def formulate_bounds_from_distance(options, origin, units)
148
- distance = options[:within] if options.has_key?(:within) && options[:within].is_a?(Numeric)
149
- distance = options[:range].last-(options[:range].exclude_end?? 1 : 0) if options.has_key?(:range)
150
- if distance
151
- Geokit::Bounds.from_point_and_radius(origin,distance,:units=>units)
152
- else
153
- nil
154
- end
155
- end
156
-
157
- def distance_conditions(options)
158
- origin = extract_origin_from_options(options)
159
- units = extract_units_from_options(options)
160
- formula = extract_formula_from_options(options)
161
- distance_column_name = distance_sql(origin, units, formula)
162
-
163
- if options.has_key?(:within)
164
- Arel.sql(distance_column_name).lteq(options[:within])
165
- elsif options.has_key?(:beyond)
166
- Arel.sql(distance_column_name).gt(options[:beyond])
167
- elsif options.has_key?(:range)
168
- min_condition = Arel.sql(distance_column_name).gteq(options[:range].begin)
169
- max_condition = if options[:range].exclude_end?
170
- Arel.sql(distance_column_name).lt(options[:range].end)
171
- else
172
- Arel.sql(distance_column_name).lteq(options[:range].end)
173
- end
174
- min_condition.and(max_condition)
175
- end
176
- end
177
-
178
- def bound_conditions(bounds, inclusive = false)
179
- return nil unless bounds
180
- if inclusive
181
- lt_operator = :lteq
182
- gt_operator = :gteq
183
- else
184
- lt_operator = :lt
185
- gt_operator = :gt
186
- end
187
- sw,ne = bounds.sw, bounds.ne
188
- lat, lng = Arel.sql(qualified_lat_column_name), Arel.sql(qualified_lng_column_name)
189
- lat.send(gt_operator, sw.lat).and(lat.send(lt_operator, ne.lat)).and(
190
- if bounds.crosses_meridian?
191
- lng.send(lt_operator, ne.lng).or(lng.send(gt_operator, sw.lng))
192
- else
193
- lng.send(gt_operator, sw.lng).and(lng.send(lt_operator, ne.lng))
194
- end
195
- )
196
- end
197
-
198
- # Extracts the origin instance out of the options if it exists and returns
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
201
- # option keys from the hash.
202
- def extract_origin_from_options(options)
203
- origin = options.delete(:origin)
204
- res = normalize_point_to_lat_lng(origin) if origin
205
- res
206
- end
207
-
208
- # Extract the units out of the options if it exists and returns it. If
209
- # there is no :units key, it uses the default. The side effect of the
210
- # method is to remove the :units key from the options hash.
211
- def extract_units_from_options(options)
212
- units = options[:units] || default_units
213
- options.delete(:units)
214
- units
215
- end
216
-
217
- # Extract the formula out of the options if it exists and returns it. If
218
- # there is no :formula key, it uses the default. The side effect of the
219
- # method is to remove the :formula key from the options hash.
220
- def extract_formula_from_options(options)
221
- formula = options[:formula] || default_formula
222
- options.delete(:formula)
223
- formula
224
- end
225
-
226
- def extract_bounds_from_options(options)
227
- bounds = options.delete(:bounds)
228
- bounds = Geokit::Bounds.normalize(bounds) if bounds
229
- end
230
-
231
- # Geocode IP address.
232
- def geocode_ip_address(origin)
233
- geo_location = Geokit::Geocoders::MultiGeocoder.geocode(origin)
234
- return geo_location if geo_location.success
235
- raise Geokit::Geocoders::GeocodeError
236
- end
237
-
238
- # Given a point in a variety of (an address to geocode,
239
- # an array of [lat,lng], or an object with appropriate lat/lng methods, an IP addres)
240
- # this method will normalize it into a Geokit::LatLng instance. The only thing this
241
- # method adds on top of LatLng#normalize is handling of IP addresses
242
- def normalize_point_to_lat_lng(point)
243
- res = geocode_ip_address(point) if point.is_a?(String) && /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/.match(point)
244
- res = Geokit::LatLng.normalize(point) unless res
245
- res
246
- end
247
-
248
- # Looks for the distance column and replaces it with the distance sql. If an origin was not
249
- # passed in and the distance column exists, we leave it to be flagged as bad SQL by the database.
250
- # Conditions are either a string or an array. In the case of an array, the first entry contains
251
- # the condition.
252
- def substitute_distance_in_where_values(arel, origin, units=default_units, formula=default_formula)
253
- pattern = Regexp.new("\\b#{distance_column_name}\\b")
254
- value = distance_sql(origin, units, formula)
255
- arel.where_values.map! do |where_value|
256
- if where_value.is_a?(String)
257
- where_value.gsub(pattern, value)
258
- else
259
- where_value
260
- end
261
- end
262
- arel
263
- end
264
-
265
- # Returns the distance SQL using the spherical world formula (Haversine). The SQL is tuned
266
- # to the database in use.
267
- def sphere_distance_sql(origin, units)
268
- # "origin" can be a Geokit::LatLng (with :lat and :lng methods), e.g.
269
- # when using geo_scope or it can be an ActsAsMappable with customized
270
- # latitude and longitude methods, e.g. when using distance_sql.
271
- lat = deg2rad(get_lat(origin))
272
- lng = deg2rad(get_lng(origin))
273
- multiplier = units_sphere_multiplier(units)
274
- geokit_finder_adapter.sphere_distance_sql(lat, lng, multiplier) if geokit_finder_adapter
275
- end
276
-
277
- # Returns the distance SQL using the flat-world formula (Phythagorean Theory). The SQL is tuned
278
- # to the database in use.
279
- def flat_distance_sql(origin, units)
280
- lat_degree_units = units_per_latitude_degree(units)
281
- lng_degree_units = units_per_longitude_degree(get_lat(origin), units)
282
- geokit_finder_adapter.flat_distance_sql(origin, lat_degree_units, lng_degree_units)
283
- end
284
-
285
- def get_lat(origin)
286
- origin.respond_to?(:lat) ? origin.lat \
287
- : origin.send(:"#{lat_column_name}")
288
- end
289
-
290
- def get_lng(origin)
291
- origin.respond_to?(:lng) ? origin.lng \
292
- : origin.send(:"#{lng_column_name}")
293
- end
294
-
295
- end # ClassMethods
296
- end
297
- end
1
+ module Geokit
2
+ module ActsAsMappable
3
+
4
+ # Class methods included in models when +acts_as_mappable+ is called
5
+ module ClassMethods
6
+
7
+ # A proxy to an instance of a finder adapter, inferred from the connection's adapter.
8
+ def geokit_finder_adapter
9
+ @geokit_finder_adapter ||= begin
10
+ adapter_name = connection.adapter_name
11
+ klass = load_adapter(adapter_name)
12
+ klass.load(self) unless klass.loaded || skip_loading
13
+ klass.new(self)
14
+ rescue LoadError
15
+ raise UnsupportedAdapter, "`#{connection.adapter_name.downcase}` is not a supported adapter."
16
+ end
17
+ end
18
+
19
+ def within(distance, options = {})
20
+ options[:within] = distance
21
+ # Add bounding box to speed up SQL request.
22
+ bounds = formulate_bounds_from_distance(
23
+ options,
24
+ normalize_point_to_lat_lng(options[:origin]),
25
+ options[:units] || default_units)
26
+ with_latlng.where(bound_conditions(bounds)).
27
+ where(distance_conditions(options))
28
+ end
29
+ alias inside within
30
+
31
+ def beyond(distance, options = {})
32
+ options[:beyond] = distance
33
+ #geo_scope(options)
34
+ where(distance_conditions(options))
35
+ end
36
+ alias outside beyond
37
+
38
+ def in_range(range, options = {})
39
+ options[:range] = range
40
+ #geo_scope(options)
41
+ where(distance_conditions(options))
42
+ end
43
+
44
+ def in_bounds(bounds, options = {})
45
+ inclusive = options.delete(:inclusive) || false
46
+ options[:bounds] = bounds
47
+ #geo_scope(options)
48
+ #where(distance_conditions(options))
49
+ bounds = extract_bounds_from_options(options)
50
+ where(bound_conditions(bounds, inclusive))
51
+ end
52
+
53
+ def by_distance(options = {})
54
+ origin = extract_origin_from_options(options)
55
+ units = extract_units_from_options(options)
56
+ formula = extract_formula_from_options(options)
57
+ distance_column_name = distance_sql(origin, units, formula)
58
+ with_latlng.order(
59
+ Arel.sql(distance_column_name).send(options[:reverse] ? 'desc' : 'asc')
60
+ )
61
+ end
62
+
63
+ def with_latlng
64
+ where(arel_table[lat_column_name].not_eq(nil).and(arel_table[lng_column_name].not_eq(nil)))
65
+ end
66
+
67
+ def closest(options = {})
68
+ by_distance(options).limit(1)
69
+ end
70
+ alias nearest closest
71
+
72
+ def farthest(options = {})
73
+ by_distance({:reverse => true}.merge(options)).limit(1)
74
+ end
75
+
76
+ #def geo_scope(options = {})
77
+ # arel = self.is_a?(ActiveRecord::Relation) ? self : self.scoped
78
+
79
+ # origin = extract_origin_from_options(options)
80
+ # units = extract_units_from_options(options)
81
+ # formula = extract_formula_from_options(options)
82
+ # bounds = extract_bounds_from_options(options)
83
+
84
+ # if origin || bounds
85
+ # bounds = formulate_bounds_from_distance(options, origin, units) unless bounds
86
+
87
+ # if origin
88
+ # arel.distance_formula = distance_sql(origin, units, formula)
89
+ #
90
+ # if arel.select_values.blank?
91
+ # star_select = Arel::Nodes::SqlLiteral.new(arel.quoted_table_name + '.*')
92
+ # arel = arel.select(star_select)
93
+ # end
94
+ # end
95
+
96
+ # if bounds
97
+ # bound_conditions = bound_conditions(bounds)
98
+ # arel = arel.where(bound_conditions) if bound_conditions
99
+ # end
100
+
101
+ # distance_conditions = distance_conditions(options)
102
+ # arel = arel.where(distance_conditions) if distance_conditions
103
+
104
+ # if self.through
105
+ # arel = arel.includes(self.through)
106
+ # end
107
+ # end
108
+
109
+ # arel
110
+ #end
111
+
112
+ # Returns the distance calculation to be used as a display column or a condition. This
113
+ # is provide for anyone wanting access to the raw SQL.
114
+ def distance_sql(origin, units=default_units, formula=default_formula)
115
+ case formula
116
+ when :sphere
117
+ sql = sphere_distance_sql(origin, units)
118
+ when :flat
119
+ sql = flat_distance_sql(origin, units)
120
+ end
121
+ sql
122
+ end
123
+
124
+ private
125
+
126
+ # Returns the adapter class from Geokit::Adapters, requiring its file
127
+ # first if needed. Only Geokit::Adapters is checked, so a top-level
128
+ # constant with the same name, such as the mysql2 gem's Mysql2 module,
129
+ # isn't mistaken for the adapter.
130
+ def load_adapter(adapter_name)
131
+ class_name = adapter_name.camelcase
132
+ unless Adapters.const_defined?(class_name, false)
133
+ require File.join('geokit-rails', 'adapters', adapter_name.downcase)
134
+ end
135
+ Adapters.const_get(class_name, false)
136
+ end
137
+
138
+ # Override ActiveRecord::Base.relation to return an instance of Geokit::ActsAsMappable::Relation.
139
+ # TODO: Do we need to override JoinDependency#relation too?
140
+ #def relation
141
+ # # NOTE: This cannot be @relation as ActiveRecord already uses this to
142
+ # # cache *its* Relation object
143
+ # @_geokit_relation ||= Relation.new(self, arel_table)
144
+ # finder_needs_type_condition? ? @_geokit_relation.where(type_condition) : @_geokit_relation
145
+ #end
146
+
147
+ # If it's a :within query, add a bounding box to improve performance.
148
+ # This only gets called if a :bounds argument is not otherwise supplied.
149
+ def formulate_bounds_from_distance(options, origin, units)
150
+ distance = options[:within] if options.has_key?(:within) && options[:within].is_a?(Numeric)
151
+ distance = options[:range].last-(options[:range].exclude_end?? 1 : 0) if options.has_key?(:range)
152
+ if distance
153
+ Geokit::Bounds.from_point_and_radius(origin,distance,:units=>units)
154
+ else
155
+ nil
156
+ end
157
+ end
158
+
159
+ def distance_conditions(options)
160
+ origin = extract_origin_from_options(options)
161
+ units = extract_units_from_options(options)
162
+ formula = extract_formula_from_options(options)
163
+ distance_column_name = distance_sql(origin, units, formula)
164
+
165
+ if options.has_key?(:within)
166
+ Arel.sql(distance_column_name).lteq(options[:within])
167
+ elsif options.has_key?(:beyond)
168
+ Arel.sql(distance_column_name).gt(options[:beyond])
169
+ elsif options.has_key?(:range)
170
+ min_condition = Arel.sql(distance_column_name).gteq(options[:range].begin)
171
+ max_condition = if options[:range].exclude_end?
172
+ Arel.sql(distance_column_name).lt(options[:range].end)
173
+ else
174
+ Arel.sql(distance_column_name).lteq(options[:range].end)
175
+ end
176
+ min_condition.and(max_condition)
177
+ end
178
+ end
179
+
180
+ def bound_conditions(bounds, inclusive = false)
181
+ return nil unless bounds
182
+ if inclusive
183
+ lt_operator = :lteq
184
+ gt_operator = :gteq
185
+ else
186
+ lt_operator = :lt
187
+ gt_operator = :gt
188
+ end
189
+ sw,ne = bounds.sw, bounds.ne
190
+ lat, lng = Arel.sql(qualified_lat_column_name), Arel.sql(qualified_lng_column_name)
191
+ lat.send(gt_operator, sw.lat).and(lat.send(lt_operator, ne.lat)).and(
192
+ if bounds.crosses_meridian?
193
+ lng.send(lt_operator, ne.lng).or(lng.send(gt_operator, sw.lng))
194
+ else
195
+ lng.send(gt_operator, sw.lng).and(lng.send(lt_operator, ne.lng))
196
+ end
197
+ )
198
+ end
199
+
200
+ # Extracts the origin instance out of the options if it exists and returns
201
+ # it. If there is no origin, looks for latitude and longitude values to
202
+ # create an origin. The side-effect of the method is to remove these
203
+ # option keys from the hash.
204
+ def extract_origin_from_options(options)
205
+ origin = options.delete(:origin)
206
+ res = normalize_point_to_lat_lng(origin) if origin
207
+ res
208
+ end
209
+
210
+ # Extract the units out of the options if it exists and returns it. If
211
+ # there is no :units key, it uses the default. The side effect of the
212
+ # method is to remove the :units key from the options hash.
213
+ def extract_units_from_options(options)
214
+ units = options[:units] || default_units
215
+ options.delete(:units)
216
+ units
217
+ end
218
+
219
+ # Extract the formula out of the options if it exists and returns it. If
220
+ # there is no :formula key, it uses the default. The side effect of the
221
+ # method is to remove the :formula key from the options hash.
222
+ def extract_formula_from_options(options)
223
+ formula = options[:formula] || default_formula
224
+ options.delete(:formula)
225
+ formula
226
+ end
227
+
228
+ def extract_bounds_from_options(options)
229
+ bounds = options.delete(:bounds)
230
+ bounds = Geokit::Bounds.normalize(bounds) if bounds
231
+ end
232
+
233
+ # Geocode IP address.
234
+ def geocode_ip_address(origin)
235
+ geo_location = Geokit::Geocoders::MultiGeocoder.geocode(origin)
236
+ return geo_location if geo_location.success
237
+ raise Geokit::Geocoders::GeocodeError
238
+ end
239
+
240
+ # Given a point in a variety of (an address to geocode,
241
+ # an array of [lat,lng], or an object with appropriate lat/lng methods, an IP addres)
242
+ # this method will normalize it into a Geokit::LatLng instance. The only thing this
243
+ # method adds on top of LatLng#normalize is handling of IP addresses
244
+ def normalize_point_to_lat_lng(point)
245
+ res = geocode_ip_address(point) if point.is_a?(String) && /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/.match(point)
246
+ res = Geokit::LatLng.normalize(point) unless res
247
+ res
248
+ end
249
+
250
+ # Looks for the distance column and replaces it with the distance sql. If an origin was not
251
+ # passed in and the distance column exists, we leave it to be flagged as bad SQL by the database.
252
+ # Conditions are either a string or an array. In the case of an array, the first entry contains
253
+ # the condition.
254
+ def substitute_distance_in_where_values(arel, origin, units=default_units, formula=default_formula)
255
+ pattern = Regexp.new("\\b#{distance_column_name}\\b")
256
+ value = distance_sql(origin, units, formula)
257
+ arel.where_values.map! do |where_value|
258
+ if where_value.is_a?(String)
259
+ where_value.gsub(pattern, value)
260
+ else
261
+ where_value
262
+ end
263
+ end
264
+ arel
265
+ end
266
+
267
+ # Returns the distance SQL using the spherical world formula (Haversine). The SQL is tuned
268
+ # to the database in use.
269
+ def sphere_distance_sql(origin, units)
270
+ # "origin" can be a Geokit::LatLng (with :lat and :lng methods), e.g.
271
+ # when using geo_scope or it can be an ActsAsMappable with customized
272
+ # latitude and longitude methods, e.g. when using distance_sql.
273
+ lat = deg2rad(get_lat(origin))
274
+ lng = deg2rad(get_lng(origin))
275
+ multiplier = units_sphere_multiplier(units)
276
+ geokit_finder_adapter.sphere_distance_sql(lat, lng, multiplier) if geokit_finder_adapter
277
+ end
278
+
279
+ # Returns the distance SQL using the flat-world formula (Phythagorean Theory). The SQL is tuned
280
+ # to the database in use.
281
+ def flat_distance_sql(origin, units)
282
+ lat_degree_units = units_per_latitude_degree(units)
283
+ lng_degree_units = units_per_longitude_degree(get_lat(origin), units)
284
+ geokit_finder_adapter.flat_distance_sql(origin, lat_degree_units, lng_degree_units)
285
+ end
286
+
287
+ def get_lat(origin)
288
+ origin.respond_to?(:lat) ? origin.lat \
289
+ : origin.send(:"#{lat_column_name}")
290
+ end
291
+
292
+ def get_lng(origin)
293
+ origin.respond_to?(:lng) ? origin.lng \
294
+ : origin.send(:"#{lng_column_name}")
295
+ end
296
+
297
+ end # ClassMethods
298
+ end
299
+ end