geokit-rails 2.3.2 → 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 (110) 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 -96
  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/{rails3.gemfile → rails7.2.gemfile} +7 -7
  12. data/gemfiles/{rails4.gemfile → rails8.0.gemfile} +7 -8
  13. data/gemfiles/{rails5.gemfile → rails8.1.gemfile} +7 -8
  14. data/geokit-rails.gemspec +34 -33
  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 -0
  18. data/lib/geokit-rails/acts_as_mappable/glue.rb +54 -0
  19. data/lib/geokit-rails/acts_as_mappable.rb +78 -425
  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/{core_extensions.rb → 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 -48
  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 -9
  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 +42 -0
  39. data/test/dummy/Rakefile +6 -0
  40. data/test/dummy/app/assets/images/.keep +0 -0
  41. data/test/dummy/app/assets/stylesheets/application.css +1 -0
  42. data/test/dummy/app/channels/application_cable/channel.rb +4 -0
  43. data/test/dummy/app/channels/application_cable/connection.rb +4 -0
  44. data/test/dummy/app/controllers/application_controller.rb +3 -0
  45. data/test/dummy/app/controllers/concerns/.keep +0 -0
  46. data/test/dummy/app/controllers/location_aware_controller.rb +43 -0
  47. data/test/dummy/app/helpers/application_helper.rb +2 -0
  48. data/test/dummy/app/jobs/application_job.rb +7 -0
  49. data/test/dummy/app/mailers/application_mailer.rb +4 -0
  50. data/test/dummy/app/models/application_record.rb +3 -0
  51. data/test/dummy/app/models/concerns/.keep +0 -0
  52. data/test/dummy/app/views/layouts/application.html.erb +15 -0
  53. data/test/dummy/app/views/layouts/mailer.html.erb +13 -0
  54. data/test/dummy/app/views/layouts/mailer.text.erb +1 -0
  55. data/test/dummy/bin/rails +4 -0
  56. data/test/dummy/bin/rake +4 -0
  57. data/test/dummy/bin/setup +33 -0
  58. data/test/dummy/config/application.rb +22 -0
  59. data/test/dummy/config/boot.rb +5 -0
  60. data/test/dummy/config/cable.yml +10 -0
  61. data/test/dummy/config/database.yml +25 -0
  62. data/test/dummy/config/environment.rb +5 -0
  63. data/test/dummy/config/environments/development.rb +68 -0
  64. data/test/dummy/config/environments/production.rb +87 -0
  65. data/test/dummy/config/environments/test.rb +60 -0
  66. data/test/dummy/config/initializers/content_security_policy.rb +25 -0
  67. data/test/dummy/config/initializers/filter_parameter_logging.rb +8 -0
  68. data/test/dummy/config/initializers/inflections.rb +16 -0
  69. data/test/dummy/config/initializers/permissions_policy.rb +11 -0
  70. data/test/dummy/config/locales/en.yml +33 -0
  71. data/test/dummy/config/puma.rb +43 -0
  72. data/test/dummy/config/routes.rb +6 -0
  73. data/test/dummy/config/storage.yml +34 -0
  74. data/test/dummy/config.ru +6 -0
  75. data/test/dummy/lib/assets/.keep +0 -0
  76. data/test/dummy/log/.keep +0 -0
  77. data/test/dummy/public/404.html +67 -0
  78. data/test/dummy/public/422.html +67 -0
  79. data/test/dummy/public/500.html +66 -0
  80. data/test/dummy/public/apple-touch-icon-precomposed.png +0 -0
  81. data/test/dummy/public/apple-touch-icon.png +0 -0
  82. data/test/dummy/public/favicon.ico +0 -0
  83. data/test/dummy/storage/.keep +0 -0
  84. data/test/dummy/tmp/.keep +0 -0
  85. data/test/dummy/tmp/pids/.keep +0 -0
  86. data/test/dummy/tmp/storage/.keep +0 -0
  87. data/test/fixtures/companies.yml +9 -9
  88. data/test/fixtures/custom_locations.yml +61 -61
  89. data/test/fixtures/locations.yml +61 -61
  90. data/test/fixtures/mock_addresses.yml +16 -16
  91. data/test/fixtures/mock_families.yml +2 -2
  92. data/test/fixtures/mock_houses.yml +8 -8
  93. data/test/fixtures/mock_organizations.yml +4 -4
  94. data/test/fixtures/mock_people.yml +4 -4
  95. data/test/ip_geocode_lookup_test.rb +53 -0
  96. data/test/models/company.rb +2 -2
  97. data/test/models/custom_location.rb +11 -11
  98. data/test/models/location.rb +3 -3
  99. data/test/models/mock_address.rb +3 -3
  100. data/test/models/mock_family.rb +4 -4
  101. data/test/models/mock_house.rb +2 -2
  102. data/test/models/mock_organization.rb +5 -5
  103. data/test/models/mock_person.rb +6 -6
  104. data/test/models/store.rb +2 -2
  105. data/test/schema.rb +61 -61
  106. data/test/tasks.rake +37 -37
  107. data/test/test_helper.rb +83 -56
  108. metadata +137 -37
  109. data/.travis.yml +0 -29
  110. data/test/ip_geocode_lookup_test.disabled.rb +0 -82
@@ -1,425 +1,78 @@
1
- require 'active_record'
2
- require 'active_support/concern'
3
-
4
- module Geokit
5
- module ActsAsMappable
6
-
7
- class UnsupportedAdapter < StandardError ; end
8
-
9
- # Add the +acts_as_mappable+ method into ActiveRecord subclasses
10
- module Glue # :nodoc:
11
- extend ActiveSupport::Concern
12
-
13
- module ClassMethods # :nodoc:
14
- OPTION_SYMBOLS = [ :distance_column_name, :default_units, :default_formula, :lat_column_name, :lng_column_name, :qualified_lat_column_name, :qualified_lng_column_name, :skip_loading ]
15
-
16
- def acts_as_mappable(options = {})
17
- metaclass = (class << self; self; end)
18
-
19
- include Geokit::ActsAsMappable
20
-
21
- cattr_accessor :through
22
- self.through = options[:through]
23
-
24
- if reflection = Geokit::ActsAsMappable.end_of_reflection_chain(self.through, self)
25
- metaclass.instance_eval do
26
- OPTION_SYMBOLS.each do |method_name|
27
- define_method method_name do
28
- reflection.klass.send(method_name)
29
- end
30
- end
31
- end
32
- else
33
- cattr_accessor *OPTION_SYMBOLS
34
-
35
- self.distance_column_name = options[:distance_column_name] || 'distance'
36
- self.default_units = options[:default_units] || Geokit::default_units
37
- self.default_formula = options[:default_formula] || Geokit::default_formula
38
- self.lat_column_name = options[:lat_column_name] || 'lat'
39
- self.lng_column_name = options[:lng_column_name] || 'lng'
40
- self.skip_loading = options[:skip_loading]
41
- self.qualified_lat_column_name = "#{table_name}.#{lat_column_name}"
42
- self.qualified_lng_column_name = "#{table_name}.#{lng_column_name}"
43
-
44
- if options.include?(:auto_geocode) && options[:auto_geocode]
45
- # if the form auto_geocode=>true is used, let the defaults take over by suppling an empty hash
46
- options[:auto_geocode] = {} if options[:auto_geocode] == true
47
- cattr_accessor :auto_geocode_field, :auto_geocode_error_message
48
- self.auto_geocode_field = options[:auto_geocode][:field] || 'address'
49
- self.auto_geocode_error_message = options[:auto_geocode][:error_message] || 'could not locate address'
50
-
51
- # set the actual callback here
52
- before_validation :auto_geocode_address, :on => :create
53
- end
54
- end
55
- end
56
- end
57
- end # Glue
58
-
59
- #class Relation < ActiveRecord::Relation
60
- # attr_accessor :distance_formula
61
-
62
- # def where(opts, *rest)
63
- # return self if opts.blank?
64
- # relation = clone
65
- # where_values = build_where(opts, rest)
66
- # relation.where_values += substitute_distance_in_values(where_values)
67
- # relation
68
- # end
69
-
70
- # def order(*args)
71
- # return self if args.blank?
72
- # relation = clone
73
- # order_values = args.flatten
74
- # relation.order_values += substitute_distance_in_values(order_values)
75
- # relation
76
- # end
77
-
78
- #private
79
- # def substitute_distance_in_values(values)
80
- # return values unless @distance_formula
81
- # # substitute distance with the actual distance equation
82
- # pattern = Regexp.new("\\b#{@klass.distance_column_name}\\b")
83
- # values.map {|value| value.is_a?(String) ? value.gsub(pattern, @distance_formula) : value }
84
- # end
85
- #end
86
-
87
- extend ActiveSupport::Concern
88
-
89
- included do
90
- include Geokit::Mappable
91
- end
92
-
93
- # Class methods included in models when +acts_as_mappable+ is called
94
- module ClassMethods
95
-
96
- # A proxy to an instance of a finder adapter, inferred from the connection's adapter.
97
- def geokit_finder_adapter
98
- @geokit_finder_adapter ||= begin
99
- unless Adapters.const_defined?(connection.adapter_name.camelcase)
100
- filename = connection.adapter_name.downcase
101
- require File.join("geokit-rails", "adapters", filename)
102
- end
103
- klass = Adapters.const_get(connection.adapter_name.camelcase)
104
- if klass.class == Module
105
- # For some reason Mysql2 adapter was defined in Adapters.constants but was Module instead of a Class
106
- filename = connection.adapter_name.downcase
107
- require File.join("geokit-rails", "adapters", filename)
108
- # Re-init the klass after require
109
- klass = Adapters.const_get(connection.adapter_name.camelcase)
110
- end
111
- klass.load(self) unless klass.loaded || skip_loading
112
- klass.new(self)
113
- rescue LoadError
114
- raise UnsupportedAdapter, "`#{connection.adapter_name.downcase}` is not a supported adapter."
115
- end
116
- end
117
-
118
- def within(distance, options = {})
119
- options[:within] = distance
120
- # Add bounding box to speed up SQL request.
121
- bounds = formulate_bounds_from_distance(
122
- options,
123
- normalize_point_to_lat_lng(options[:origin]),
124
- options[:units] || default_units)
125
- with_latlng.where(bound_conditions(bounds)).
126
- where(distance_conditions(options))
127
- end
128
- alias inside within
129
-
130
- def beyond(distance, options = {})
131
- options[:beyond] = distance
132
- #geo_scope(options)
133
- where(distance_conditions(options))
134
- end
135
- alias outside beyond
136
-
137
- def in_range(range, options = {})
138
- options[:range] = range
139
- #geo_scope(options)
140
- where(distance_conditions(options))
141
- end
142
-
143
- def in_bounds(bounds, options = {})
144
- inclusive = options.delete(:inclusive) || false
145
- options[:bounds] = bounds
146
- #geo_scope(options)
147
- #where(distance_conditions(options))
148
- bounds = extract_bounds_from_options(options)
149
- where(bound_conditions(bounds, inclusive))
150
- end
151
-
152
- def by_distance(options = {})
153
- origin = extract_origin_from_options(options)
154
- units = extract_units_from_options(options)
155
- formula = extract_formula_from_options(options)
156
- distance_column_name = distance_sql(origin, units, formula)
157
- with_latlng.order(
158
- Arel.sql(distance_column_name).send(options[:reverse] ? 'desc' : 'asc')
159
- )
160
- end
161
-
162
- def with_latlng
163
- where("#{qualified_lat_column_name} IS NOT NULL AND #{qualified_lng_column_name} IS NOT NULL")
164
- end
165
-
166
- def closest(options = {})
167
- by_distance(options).limit(1)
168
- end
169
- alias nearest closest
170
-
171
- def farthest(options = {})
172
- by_distance({:reverse => true}.merge(options)).limit(1)
173
- end
174
-
175
- #def geo_scope(options = {})
176
- # arel = self.is_a?(ActiveRecord::Relation) ? self : self.scoped
177
-
178
- # origin = extract_origin_from_options(options)
179
- # units = extract_units_from_options(options)
180
- # formula = extract_formula_from_options(options)
181
- # bounds = extract_bounds_from_options(options)
182
-
183
- # if origin || bounds
184
- # bounds = formulate_bounds_from_distance(options, origin, units) unless bounds
185
-
186
- # if origin
187
- # arel.distance_formula = distance_sql(origin, units, formula)
188
- #
189
- # if arel.select_values.blank?
190
- # star_select = Arel::Nodes::SqlLiteral.new(arel.quoted_table_name + '.*')
191
- # arel = arel.select(star_select)
192
- # end
193
- # end
194
-
195
- # if bounds
196
- # bound_conditions = bound_conditions(bounds)
197
- # arel = arel.where(bound_conditions) if bound_conditions
198
- # end
199
-
200
- # distance_conditions = distance_conditions(options)
201
- # arel = arel.where(distance_conditions) if distance_conditions
202
-
203
- # if self.through
204
- # arel = arel.includes(self.through)
205
- # end
206
- # end
207
-
208
- # arel
209
- #end
210
-
211
- # Returns the distance calculation to be used as a display column or a condition. This
212
- # is provide for anyone wanting access to the raw SQL.
213
- def distance_sql(origin, units=default_units, formula=default_formula)
214
- case formula
215
- when :sphere
216
- sql = sphere_distance_sql(origin, units)
217
- when :flat
218
- sql = flat_distance_sql(origin, units)
219
- end
220
- sql
221
- end
222
-
223
- private
224
-
225
- # Override ActiveRecord::Base.relation to return an instance of Geokit::ActsAsMappable::Relation.
226
- # TODO: Do we need to override JoinDependency#relation too?
227
- #def relation
228
- # # NOTE: This cannot be @relation as ActiveRecord already uses this to
229
- # # cache *its* Relation object
230
- # @_geokit_relation ||= Relation.new(self, arel_table)
231
- # finder_needs_type_condition? ? @_geokit_relation.where(type_condition) : @_geokit_relation
232
- #end
233
-
234
- # If it's a :within query, add a bounding box to improve performance.
235
- # This only gets called if a :bounds argument is not otherwise supplied.
236
- def formulate_bounds_from_distance(options, origin, units)
237
- distance = options[:within] if options.has_key?(:within) && options[:within].is_a?(Numeric)
238
- distance = options[:range].last-(options[:range].exclude_end?? 1 : 0) if options.has_key?(:range)
239
- if distance
240
- Geokit::Bounds.from_point_and_radius(origin,distance,:units=>units)
241
- else
242
- nil
243
- end
244
- end
245
-
246
- def distance_conditions(options)
247
- origin = extract_origin_from_options(options)
248
- units = extract_units_from_options(options)
249
- formula = extract_formula_from_options(options)
250
- distance_column_name = distance_sql(origin, units, formula)
251
-
252
- if options.has_key?(:within)
253
- Arel.sql(distance_column_name).lteq(options[:within])
254
- elsif options.has_key?(:beyond)
255
- Arel.sql(distance_column_name).gt(options[:beyond])
256
- elsif options.has_key?(:range)
257
- min_condition = Arel.sql(distance_column_name).gteq(options[:range].begin)
258
- max_condition = if options[:range].exclude_end?
259
- Arel.sql(distance_column_name).lt(options[:range].end)
260
- else
261
- Arel.sql(distance_column_name).lteq(options[:range].end)
262
- end
263
- min_condition.and(max_condition)
264
- end
265
- end
266
-
267
- def bound_conditions(bounds, inclusive = false)
268
- return nil unless bounds
269
- if inclusive
270
- lt_operator = :lteq
271
- gt_operator = :gteq
272
- else
273
- lt_operator = :lt
274
- gt_operator = :gt
275
- end
276
- sw,ne = bounds.sw, bounds.ne
277
- lat, lng = Arel.sql(qualified_lat_column_name), Arel.sql(qualified_lng_column_name)
278
- lat.send(gt_operator, sw.lat).and(lat.send(lt_operator, ne.lat)).and(
279
- if bounds.crosses_meridian?
280
- lng.send(lt_operator, ne.lng).or(lng.send(gt_operator, sw.lng))
281
- else
282
- lng.send(gt_operator, sw.lng).and(lng.send(lt_operator, ne.lng))
283
- end
284
- )
285
- end
286
-
287
- # Extracts the origin instance out of the options if it exists and returns
288
- # it. If there is no origin, looks for latitude and longitude values to
289
- # create an origin. The side-effect of the method is to remove these
290
- # option keys from the hash.
291
- def extract_origin_from_options(options)
292
- origin = options.delete(:origin)
293
- res = normalize_point_to_lat_lng(origin) if origin
294
- res
295
- end
296
-
297
- # Extract the units out of the options if it exists and returns it. If
298
- # there is no :units key, it uses the default. The side effect of the
299
- # method is to remove the :units key from the options hash.
300
- def extract_units_from_options(options)
301
- units = options[:units] || default_units
302
- options.delete(:units)
303
- units
304
- end
305
-
306
- # Extract the formula out of the options if it exists and returns it. If
307
- # there is no :formula key, it uses the default. The side effect of the
308
- # method is to remove the :formula key from the options hash.
309
- def extract_formula_from_options(options)
310
- formula = options[:formula] || default_formula
311
- options.delete(:formula)
312
- formula
313
- end
314
-
315
- def extract_bounds_from_options(options)
316
- bounds = options.delete(:bounds)
317
- bounds = Geokit::Bounds.normalize(bounds) if bounds
318
- end
319
-
320
- # Geocode IP address.
321
- def geocode_ip_address(origin)
322
- geo_location = Geokit::Geocoders::MultiGeocoder.geocode(origin)
323
- return geo_location if geo_location.success
324
- raise Geokit::Geocoders::GeocodeError
325
- end
326
-
327
- # Given a point in a variety of (an address to geocode,
328
- # an array of [lat,lng], or an object with appropriate lat/lng methods, an IP addres)
329
- # this method will normalize it into a Geokit::LatLng instance. The only thing this
330
- # method adds on top of LatLng#normalize is handling of IP addresses
331
- def normalize_point_to_lat_lng(point)
332
- res = geocode_ip_address(point) if point.is_a?(String) && /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/.match(point)
333
- res = Geokit::LatLng.normalize(point) unless res
334
- res
335
- end
336
-
337
- # Looks for the distance column and replaces it with the distance sql. If an origin was not
338
- # passed in and the distance column exists, we leave it to be flagged as bad SQL by the database.
339
- # Conditions are either a string or an array. In the case of an array, the first entry contains
340
- # the condition.
341
- def substitute_distance_in_where_values(arel, origin, units=default_units, formula=default_formula)
342
- pattern = Regexp.new("\\b#{distance_column_name}\\b")
343
- value = distance_sql(origin, units, formula)
344
- arel.where_values.map! do |where_value|
345
- if where_value.is_a?(String)
346
- where_value.gsub(pattern, value)
347
- else
348
- where_value
349
- end
350
- end
351
- arel
352
- end
353
-
354
- # Returns the distance SQL using the spherical world formula (Haversine). The SQL is tuned
355
- # to the database in use.
356
- def sphere_distance_sql(origin, units)
357
- # "origin" can be a Geokit::LatLng (with :lat and :lng methods), e.g.
358
- # when using geo_scope or it can be an ActsAsMappable with customized
359
- # latitude and longitude methods, e.g. when using distance_sql.
360
- lat = deg2rad(get_lat(origin))
361
- lng = deg2rad(get_lng(origin))
362
- multiplier = units_sphere_multiplier(units)
363
- geokit_finder_adapter.sphere_distance_sql(lat, lng, multiplier) if geokit_finder_adapter
364
- end
365
-
366
- # Returns the distance SQL using the flat-world formula (Phythagorean Theory). The SQL is tuned
367
- # to the database in use.
368
- def flat_distance_sql(origin, units)
369
- lat_degree_units = units_per_latitude_degree(units)
370
- lng_degree_units = units_per_longitude_degree(get_lat(origin), units)
371
- geokit_finder_adapter.flat_distance_sql(origin, lat_degree_units, lng_degree_units)
372
- end
373
-
374
- def get_lat(origin)
375
- origin.respond_to?(:lat) ? origin.lat \
376
- : origin.send(:"#{lat_column_name}")
377
- end
378
-
379
- def get_lng(origin)
380
- origin.respond_to?(:lng) ? origin.lng \
381
- : origin.send(:"#{lng_column_name}")
382
- end
383
-
384
- end # ClassMethods
385
-
386
- # this is the callback for auto_geocoding
387
- def auto_geocode_address
388
- address=self.send(auto_geocode_field).to_s
389
- geo=Geokit::Geocoders::MultiGeocoder.geocode(address)
390
-
391
- if geo.success
392
- self.send("#{lat_column_name}=", geo.send(:"#{lat_column_name}"))
393
- self.send("#{lng_column_name}=", geo.send(:"#{lng_column_name}"))
394
- else
395
- errors.add(auto_geocode_field, auto_geocode_error_message)
396
- end
397
-
398
- geo.success
399
- end
400
-
401
- def self.end_of_reflection_chain(through, klass)
402
- while through
403
- reflection = nil
404
- if through.is_a?(Hash)
405
- association, through = through.to_a.first
406
- else
407
- association, through = through, nil
408
- end
409
-
410
- if reflection = klass.reflect_on_association(association)
411
- klass = reflection.klass
412
- else
413
- raise ArgumentError, "You gave #{association} in :through, but I could not find it on #{klass}."
414
- end
415
- end
416
-
417
- reflection
418
- end
419
-
420
- end # ActsAsMappable
421
- end # Geokit
422
-
423
-
424
-
425
- # ActiveRecord::Base.extend Geokit::ActsAsMappable
1
+ require 'active_record'
2
+ require 'active_support/concern'
3
+
4
+ module Geokit
5
+ module ActsAsMappable
6
+
7
+ class UnsupportedAdapter < StandardError ; end
8
+
9
+ #class Relation < ActiveRecord::Relation
10
+ # attr_accessor :distance_formula
11
+
12
+ # def where(opts, *rest)
13
+ # return self if opts.blank?
14
+ # relation = clone
15
+ # where_values = build_where(opts, rest)
16
+ # relation.where_values += substitute_distance_in_values(where_values)
17
+ # relation
18
+ # end
19
+
20
+ # def order(*args)
21
+ # return self if args.blank?
22
+ # relation = clone
23
+ # order_values = args.flatten
24
+ # relation.order_values += substitute_distance_in_values(order_values)
25
+ # relation
26
+ # end
27
+
28
+ #private
29
+ # def substitute_distance_in_values(values)
30
+ # return values unless @distance_formula
31
+ # # substitute distance with the actual distance equation
32
+ # pattern = Regexp.new("\\b#{@klass.distance_column_name}\\b")
33
+ # values.map {|value| value.is_a?(String) ? value.gsub(pattern, @distance_formula) : value }
34
+ # end
35
+ #end
36
+
37
+ extend ActiveSupport::Concern
38
+
39
+ included do
40
+ include Geokit::Mappable
41
+ end
42
+
43
+ # this is the callback for auto_geocoding
44
+ def auto_geocode_address
45
+ address=self.send(auto_geocode_field).to_s
46
+ geo=Geokit::Geocoders::MultiGeocoder.geocode(address)
47
+
48
+ if geo.success
49
+ self.send("#{lat_column_name}=", geo.send(:"#{lat_column_name}"))
50
+ self.send("#{lng_column_name}=", geo.send(:"#{lng_column_name}"))
51
+ else
52
+ errors.add(auto_geocode_field, auto_geocode_error_message)
53
+ end
54
+
55
+ geo.success
56
+ end
57
+
58
+ def self.end_of_reflection_chain(through, klass)
59
+ while through
60
+ reflection = nil
61
+ if through.is_a?(Hash)
62
+ association, through = through.to_a.first
63
+ else
64
+ association, through = through, nil
65
+ end
66
+
67
+ if reflection = klass.reflect_on_association(association)
68
+ klass = reflection.klass
69
+ else
70
+ raise ArgumentError, "You gave #{association} in :through, but I could not find it on #{klass}."
71
+ end
72
+ end
73
+
74
+ reflection
75
+ end
76
+
77
+ end # ActsAsMappable
78
+ end # Geokit
@@ -1,31 +1,31 @@
1
- module Geokit
2
- module Adapters
3
- class Abstract
4
- class NotImplementedError < StandardError ; end
5
-
6
- cattr_accessor :loaded
7
-
8
- class << self
9
- def load(klass) ; end
10
- end
11
-
12
- def initialize(klass)
13
- @owner = klass
14
- end
15
-
16
- def method_missing(method, *args, &block)
17
- return @owner.send(method, *args, &block) if @owner.respond_to?(method)
18
- super
19
- end
20
-
21
- def sphere_distance_sql(lat, lng, multiplier)
22
- raise NotImplementedError, '#sphere_distance_sql is not implemented'
23
- end
24
-
25
- def flat_distance_sql(origin, lat_degree_units, lng_degree_units)
26
- raise NotImplementedError, '#flat_distance_sql is not implemented'
27
- end
28
-
29
- end
30
- end
1
+ module Geokit
2
+ module Adapters
3
+ class Abstract
4
+ class NotImplementedError < StandardError ; end
5
+
6
+ cattr_accessor :loaded
7
+
8
+ class << self
9
+ def load(klass) ; end
10
+ end
11
+
12
+ def initialize(klass)
13
+ @owner = klass
14
+ end
15
+
16
+ def method_missing(method, *args, &block)
17
+ return @owner.send(method, *args, &block) if @owner.respond_to?(method)
18
+ super
19
+ end
20
+
21
+ def sphere_distance_sql(lat, lng, multiplier)
22
+ raise NotImplementedError, '#sphere_distance_sql is not implemented'
23
+ end
24
+
25
+ def flat_distance_sql(origin, lat_degree_units, lng_degree_units)
26
+ raise NotImplementedError, '#flat_distance_sql is not implemented'
27
+ end
28
+
29
+ end
30
+ end
31
31
  end
@@ -1,22 +1,22 @@
1
- module Geokit
2
- module Adapters
3
- class MySQL < Abstract
4
-
5
- def sphere_distance_sql(lat, lng, multiplier)
6
- %|
7
- (ACOS(least(1,COS(#{lat})*COS(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*COS(RADIANS(#{qualified_lng_column_name}))+
8
- COS(#{lat})*SIN(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*SIN(RADIANS(#{qualified_lng_column_name}))+
9
- SIN(#{lat})*SIN(RADIANS(#{qualified_lat_column_name}))))*#{multiplier})
10
- |
11
- end
12
-
13
- def flat_distance_sql(origin, lat_degree_units, lng_degree_units)
14
- %|
15
- SQRT(POW(#{lat_degree_units}*(#{origin.lat}-#{qualified_lat_column_name}),2)+
16
- POW(#{lng_degree_units}*(#{origin.lng}-#{qualified_lng_column_name}),2))
17
- |
18
- end
19
-
20
- end
21
- end
1
+ module Geokit
2
+ module Adapters
3
+ class MySQL < Abstract
4
+
5
+ def sphere_distance_sql(lat, lng, multiplier)
6
+ %|
7
+ (ACOS(least(1,COS(#{lat})*COS(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*COS(RADIANS(#{qualified_lng_column_name}))+
8
+ COS(#{lat})*SIN(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*SIN(RADIANS(#{qualified_lng_column_name}))+
9
+ SIN(#{lat})*SIN(RADIANS(#{qualified_lat_column_name}))))*#{multiplier})
10
+ |
11
+ end
12
+
13
+ def flat_distance_sql(origin, lat_degree_units, lng_degree_units)
14
+ %|
15
+ SQRT(POW(#{lat_degree_units}*(#{origin.lat}-#{qualified_lat_column_name}),2)+
16
+ POW(#{lng_degree_units}*(#{origin.lng}-#{qualified_lng_column_name}),2))
17
+ |
18
+ end
19
+
20
+ end
21
+ end
22
22
  end
@@ -1,8 +1,8 @@
1
- require 'geokit-rails/adapters/mysql'
2
-
3
- module Geokit
4
- module Adapters
5
- class Mysql2 < MySQL
6
- end
7
- end
8
- end
1
+ require 'geokit-rails/adapters/mysql'
2
+
3
+ module Geokit
4
+ module Adapters
5
+ class Mysql2 < MySQL
6
+ end
7
+ end
8
+ end
@@ -1,8 +1,8 @@
1
- require 'geokit-rails/adapters/mysql'
2
-
3
- module Geokit
4
- module Adapters
5
- class Mysql2Spatial < MySQL
6
- end
7
- end
8
- end
1
+ require 'geokit-rails/adapters/mysql'
2
+
3
+ module Geokit
4
+ module Adapters
5
+ class Mysql2Spatial < MySQL
6
+ end
7
+ end
8
+ end