geokit-rails 2.5.0 → 2.5.2

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