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
@@ -0,0 +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
+ 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
@@ -0,0 +1,54 @@
1
+ module Geokit
2
+ module ActsAsMappable
3
+
4
+ # Add the +acts_as_mappable+ method into ActiveRecord subclasses
5
+ module Glue # :nodoc:
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods # :nodoc:
9
+ 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 ]
10
+
11
+ def acts_as_mappable(options = {})
12
+ metaclass = (class << self; self; end)
13
+
14
+ include Geokit::ActsAsMappable
15
+
16
+ cattr_accessor :through
17
+ self.through = options[:through]
18
+
19
+ if reflection = Geokit::ActsAsMappable.end_of_reflection_chain(self.through, self)
20
+ metaclass.instance_eval do
21
+ OPTION_SYMBOLS.each do |method_name|
22
+ define_method method_name do
23
+ reflection.klass.send(method_name)
24
+ end
25
+ end
26
+ end
27
+ else
28
+ cattr_accessor *OPTION_SYMBOLS
29
+
30
+ self.distance_column_name = options[:distance_column_name] || 'distance'
31
+ self.default_units = options[:default_units] || Geokit::default_units
32
+ self.default_formula = options[:default_formula] || Geokit::default_formula
33
+ self.lat_column_name = options[:lat_column_name] || 'lat'
34
+ self.lng_column_name = options[:lng_column_name] || 'lng'
35
+ self.skip_loading = options[:skip_loading]
36
+ self.qualified_lat_column_name = "#{table_name}.#{lat_column_name}"
37
+ self.qualified_lng_column_name = "#{table_name}.#{lng_column_name}"
38
+
39
+ if options.include?(:auto_geocode) && options[:auto_geocode]
40
+ # if the form auto_geocode=>true is used, let the defaults take over by suppling an empty hash
41
+ options[:auto_geocode] = {} if options[:auto_geocode] == true
42
+ cattr_accessor :auto_geocode_field, :auto_geocode_error_message
43
+ self.auto_geocode_field = options[:auto_geocode][:field] || 'address'
44
+ self.auto_geocode_error_message = options[:auto_geocode][:error_message] || 'could not locate address'
45
+
46
+ # set the actual callback here
47
+ before_validation :auto_geocode_address, :on => :create
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end # Glue
53
+ end
54
+ end