activerecord-spatial 2.0.0 → 3.0.0

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 (49) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +3314 -1052
  3. data/.travis.yml +17 -0
  4. data/Gemfile +1 -0
  5. data/MIT-LICENSE +1 -1
  6. data/README.rdoc +6 -12
  7. data/activerecord-spatial.gemspec +1 -1
  8. data/lib/activerecord-spatial.rb +1 -0
  9. data/lib/activerecord-spatial/active_record.rb +1 -0
  10. data/lib/activerecord-spatial/active_record/connection_adapters/postgresql/adapter_extensions.rb +1 -0
  11. data/lib/activerecord-spatial/active_record/connection_adapters/postgresql/adapter_extensions/active_record.rb +4 -3
  12. data/lib/activerecord-spatial/active_record/connection_adapters/postgresql/postgis.rb +1 -0
  13. data/lib/activerecord-spatial/active_record/connection_adapters/postgresql/unknown_srid.rb +2 -1
  14. data/lib/activerecord-spatial/active_record/models/geography_column.rb +1 -0
  15. data/lib/activerecord-spatial/active_record/models/geometry_column.rb +1 -0
  16. data/lib/activerecord-spatial/active_record/models/spatial_column.rb +1 -0
  17. data/lib/activerecord-spatial/active_record/models/spatial_ref_sys.rb +1 -0
  18. data/lib/activerecord-spatial/associations.rb +4 -12
  19. data/lib/activerecord-spatial/associations/active_record.rb +96 -54
  20. data/lib/activerecord-spatial/associations/base.rb +2 -19
  21. data/lib/activerecord-spatial/associations/reflection/spatial_reflection.rb +1 -0
  22. data/lib/activerecord-spatial/associations/spatial_association.rb +41 -0
  23. data/lib/activerecord-spatial/spatial_columns.rb +1 -0
  24. data/lib/activerecord-spatial/spatial_function.rb +2 -1
  25. data/lib/activerecord-spatial/spatial_scope_constants.rb +1 -0
  26. data/lib/activerecord-spatial/spatial_scope_constants/postgis_2_0.rb +1 -0
  27. data/lib/activerecord-spatial/spatial_scope_constants/postgis_2_2.rb +1 -0
  28. data/lib/activerecord-spatial/spatial_scope_constants/postgis_legacy.rb +1 -0
  29. data/lib/activerecord-spatial/spatial_scopes.rb +14 -13
  30. data/lib/activerecord-spatial/version.rb +2 -1
  31. data/test/accessors_geographies_tests.rb +2 -1
  32. data/test/accessors_geometries_tests.rb +2 -1
  33. data/test/adapter_tests.rb +5 -4
  34. data/test/associations_tests.rb +40 -25
  35. data/test/geography_column_tests.rb +2 -1
  36. data/test/geometry_column_tests.rb +2 -1
  37. data/test/models/bar.rb +1 -9
  38. data/test/models/blort.rb +1 -7
  39. data/test/models/foo.rb +3 -9
  40. data/test/models/foo3d.rb +1 -9
  41. data/test/models/foo_geography.rb +1 -8
  42. data/test/models/zortable.rb +1 -9
  43. data/test/schema.rb +54 -0
  44. data/test/spatial_function_tests.rb +2 -1
  45. data/test/spatial_scopes_geographies_tests.rb +2 -1
  46. data/test/spatial_scopes_tests.rb +2 -1
  47. data/test/test_helper.rb +57 -91
  48. metadata +9 -7
  49. data/lib/activerecord-spatial/associations/preloader/spatial_association.rb +0 -57
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module ActiveRecordSpatial
3
4
  # This little module helps us out with geometry columns. At least, in
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module ActiveRecordSpatial
3
4
  class SpatialFunction
@@ -81,7 +82,7 @@ module ActiveRecordSpatial
81
82
  options[:desc]
82
83
  end
83
84
 
84
- ''.tap do |ret|
85
+ ''.dup.tap do |ret|
85
86
  ret << ' DESC' if desc
86
87
  ret << " NULLS #{options[:nulls].to_s.upcase}" if options[:nulls]
87
88
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module ActiveRecordSpatial
3
4
  module SpatialScopeConstants
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module ActiveRecordSpatial
3
4
  module SpatialScopeConstants
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module ActiveRecordSpatial
3
4
  module SpatialScopeConstants
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module ActiveRecordSpatial
3
4
  module SpatialScopeConstants
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module ActiveRecordSpatial
3
4
  # Creates named scopes for spatial relationships. The scopes created
@@ -142,7 +143,7 @@ module ActiveRecordSpatial
142
143
 
143
144
  SpatialScopeConstants::RELATIONSHIPS.each do |relationship|
144
145
  class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
145
- scope :st_#{relationship}, lambda { |geom, options = {}|
146
+ scope :st_#{relationship}, ->(geom, options = {}) {
146
147
  options = {
147
148
  geom_arg: geom
148
149
  }.merge(options)
@@ -158,7 +159,7 @@ module ActiveRecordSpatial
158
159
 
159
160
  SpatialScopeConstants::ONE_GEOMETRY_ARGUMENT_AND_ONE_ARGUMENT_RELATIONSHIPS.each do |relationship|
160
161
  class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
161
- scope :st_#{relationship}, lambda { |geom, distance, options = {}|
162
+ scope :st_#{relationship}, ->(geom, distance, options = {}) {
162
163
  options = {
163
164
  geom_arg: geom,
164
165
  args: distance
@@ -172,7 +173,7 @@ module ActiveRecordSpatial
172
173
  end
173
174
 
174
175
  class_eval do
175
- scope :st_geometry_type, lambda { |*args|
176
+ scope :st_geometry_type, ->(*args) {
176
177
  assert_arguments_length[args, 1]
177
178
  options = args.extract_options!
178
179
  types = args
@@ -185,7 +186,7 @@ module ActiveRecordSpatial
185
186
 
186
187
  SpatialScopeConstants::ZERO_ARGUMENT_MEASUREMENTS.each do |measurement|
187
188
  class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
188
- scope :order_by_st_#{measurement}, lambda { |options = {}|
189
+ scope :order_by_st_#{measurement}, ->(options = {}) {
189
190
  if options.is_a?(Symbol)
190
191
  options = {
191
192
  desc: options
@@ -195,14 +196,14 @@ module ActiveRecordSpatial
195
196
  function_call = ActiveRecordSpatial::SpatialFunction.build!(self, '#{measurement}', options).to_sql
196
197
  function_call << ActiveRecordSpatial::SpatialFunction.additional_ordering(options)
197
198
 
198
- order(function_call)
199
+ order(Arel.sql(function_call))
199
200
  }
200
201
  RUBY
201
202
  end
202
203
 
203
204
  SpatialScopeConstants::ONE_GEOMETRY_ARGUMENT_MEASUREMENTS.each do |measurement|
204
205
  class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
205
- scope :order_by_st_#{measurement}, lambda { |geom, options = {}|
206
+ scope :order_by_st_#{measurement}, ->(geom, options = {}) {
206
207
  if options.is_a?(Symbol)
207
208
  options = {
208
209
  desc: options
@@ -216,14 +217,14 @@ module ActiveRecordSpatial
216
217
  function_call = ActiveRecordSpatial::SpatialFunction.build!(self, '#{measurement}', options).to_sql
217
218
  function_call << ActiveRecordSpatial::SpatialFunction.additional_ordering(options)
218
219
 
219
- order(function_call)
220
+ order(Arel.sql(function_call))
220
221
  }
221
222
  RUBY
222
223
  end
223
224
 
224
225
  SpatialScopeConstants::ONE_ARGUMENT_MEASUREMENTS.each do |measurement|
225
226
  class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
226
- scope :order_by_st_#{measurement}, lambda { |argument, options = {}|
227
+ scope :order_by_st_#{measurement}, ->(argument, options = {}) {
227
228
  options = {
228
229
  args: argument
229
230
  }.merge(options)
@@ -231,13 +232,13 @@ module ActiveRecordSpatial
231
232
  function_call = ActiveRecordSpatial::SpatialFunction.build!(self, '#{measurement}', options).to_sql
232
233
  function_call << ActiveRecordSpatial::SpatialFunction.additional_ordering(options)
233
234
 
234
- order(function_call)
235
+ order(Arel.sql(function_call))
235
236
  }
236
237
  RUBY
237
238
  end
238
239
 
239
240
  class_eval do
240
- scope :order_by_st_hausdorffdistance, lambda { |*args|
241
+ scope :order_by_st_hausdorffdistance, ->(*args) {
241
242
  assert_arguments_length[args, 1, 3]
242
243
  options = args.extract_options!
243
244
  geom, densify_frac = args
@@ -254,10 +255,10 @@ module ActiveRecordSpatial
254
255
  ).to_sql
255
256
  function_call << ActiveRecordSpatial::SpatialFunction.additional_ordering(options)
256
257
 
257
- order(function_call)
258
+ order(Arel.sql(function_call))
258
259
  }
259
260
 
260
- scope :order_by_st_distance_spheroid, lambda { |geom, spheroid, options = {}|
261
+ scope :order_by_st_distance_spheroid, ->(geom, spheroid, options = {}) {
261
262
  options = {
262
263
  geom_arg: geom,
263
264
  args: spheroid
@@ -274,7 +275,7 @@ module ActiveRecordSpatial
274
275
  ).to_sql
275
276
  function_call << ActiveRecordSpatial::SpatialFunction.additional_ordering(options)
276
277
 
277
- order(function_call)
278
+ order(Arel.sql(function_call))
278
279
  }
279
280
 
280
281
  class << self
@@ -1,4 +1,5 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module ActiveRecordSpatial
3
- VERSION = '2.0.0'.freeze
4
+ VERSION = '3.0.0'
4
5
  end
@@ -1,9 +1,10 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  $LOAD_PATH << File.dirname(__FILE__)
3
4
  require 'test_helper'
4
5
 
5
6
  class AccessorsGeographiesTests < ActiveRecordSpatialTestCase
6
- def self.before_suite
7
+ def setup
7
8
  load_models(:foo_geography)
8
9
  end
9
10
 
@@ -1,9 +1,10 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  $LOAD_PATH << File.dirname(__FILE__)
3
4
  require 'test_helper'
4
5
 
5
6
  class AccessorsGeometriesTests < ActiveRecordSpatialTestCase
6
- def self.before_suite
7
+ def setup
7
8
  load_models(:foo)
8
9
  end
9
10
 
@@ -1,9 +1,10 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  $LOAD_PATH << File.dirname(__FILE__)
3
4
  require 'test_helper'
4
5
 
5
6
  class AdapterTests < ActiveRecordSpatialTestCase
6
- def self.before_suite
7
+ def setup
7
8
  load_models(:foo)
8
9
  end
9
10
 
@@ -12,8 +13,8 @@ class AdapterTests < ActiveRecordSpatialTestCase
12
13
  c.type == :geometry
13
14
  end
14
15
 
15
- other_columns = Foo.columns.select do |c|
16
- c.type != :geometry
16
+ other_columns = Foo.columns.reject do |c|
17
+ c.type == :geometry
17
18
  end
18
19
 
19
20
  assert_equal(2, geometry_columns.length)
@@ -23,7 +24,7 @@ end
23
24
 
24
25
  if ActiveRecordSpatial.geography_columns?
25
26
  class AdapterWithGeographyTests < ActiveRecordSpatialTestCase
26
- def self.before_suite
27
+ def setup
27
28
  load_models(:foo_geography)
28
29
  end
29
30
 
@@ -1,9 +1,10 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  $LOAD_PATH << File.dirname(__FILE__)
3
4
  require 'test_helper'
4
5
 
5
6
  class DefaultIntersectsRelationshipTest < ActiveRecordSpatialTestCase
6
- def self.before_suite
7
+ def setup
7
8
  load_models(:foo, :bar)
8
9
 
9
10
  Foo.class_eval do
@@ -28,7 +29,7 @@ class DefaultIntersectsRelationshipTest < ActiveRecordSpatialTestCase
28
29
  end
29
30
 
30
31
  class RelationshipsTest < ActiveRecordSpatialTestCase
31
- def self.before_suite
32
+ def setup
32
33
  load_models(:foo, :bar)
33
34
  end
34
35
 
@@ -45,7 +46,7 @@ class RelationshipsTest < ActiveRecordSpatialTestCase
45
46
  overlaps: [],
46
47
  touches: [],
47
48
  within: [3],
48
- :'3dintersects' => [3]
49
+ '3dintersects': [3]
49
50
  }.each do |relationship, ids|
50
51
  define_method("test_#{relationship}") do
51
52
  skip("ST_#{relationship} is unavailable") unless Foo.respond_to?("st_#{relationship}")
@@ -63,8 +64,22 @@ class RelationshipsTest < ActiveRecordSpatialTestCase
63
64
  end
64
65
  end
65
66
 
67
+ class PlainRelationshipsTest < ActiveRecordSpatialTestCase
68
+ def setup
69
+ load_models(:foo, :blort)
70
+ end
71
+
72
+ def test_with_eager_loading
73
+ values = Foo.includes(:blorts).to_a.collect do |foo|
74
+ foo.blorts.length
75
+ end
76
+
77
+ assert_equal([3, 3, 3], values)
78
+ end
79
+ end
80
+
66
81
  class RelationshipsWithSelfTest < ActiveRecordSpatialTestCase
67
- def self.before_suite
82
+ def setup
68
83
  load_models(:foo, :bar)
69
84
  end
70
85
 
@@ -81,7 +96,7 @@ class RelationshipsWithSelfTest < ActiveRecordSpatialTestCase
81
96
  overlaps: [],
82
97
  touches: [],
83
98
  within: [1, 3],
84
- :'3dintersects' => [1, 3]
99
+ '3dintersects': [1, 3]
85
100
  }.each do |relationship, ids|
86
101
  define_method("test_#{relationship}") do
87
102
  skip("ST_#{relationship} is unavailable") unless Foo.respond_to?("st_#{relationship}")
@@ -100,7 +115,7 @@ class RelationshipsWithSelfTest < ActiveRecordSpatialTestCase
100
115
  end
101
116
 
102
117
  class RelationshipsWithForeignGeomTest < ActiveRecordSpatialTestCase
103
- def self.before_suite
118
+ def setup
104
119
  load_models(:foo, :bar)
105
120
  end
106
121
 
@@ -117,7 +132,7 @@ class RelationshipsWithForeignGeomTest < ActiveRecordSpatialTestCase
117
132
  overlaps: [],
118
133
  touches: [],
119
134
  within: [3],
120
- :'3dintersects' => [3]
135
+ '3dintersects': [3]
121
136
  }.each do |relationship, ids|
122
137
  define_method("test_#{relationship}") do
123
138
  skip("ST_#{relationship} is unavailable") unless Foo.respond_to?("st_#{relationship}")
@@ -140,7 +155,7 @@ class RelationshipsWithForeignGeomTest < ActiveRecordSpatialTestCase
140
155
  end
141
156
 
142
157
  class RelationshipsWithGeomTest < ActiveRecordSpatialTestCase
143
- def self.before_suite
158
+ def setup
144
159
  load_models(:foo, :bar)
145
160
  end
146
161
 
@@ -157,7 +172,7 @@ class RelationshipsWithGeomTest < ActiveRecordSpatialTestCase
157
172
  overlaps: [],
158
173
  touches: [3],
159
174
  within: [],
160
- :'3dintersects' => [3]
175
+ '3dintersects': [3]
161
176
  }.each do |relationship, ids|
162
177
  define_method("test_#{relationship}") do
163
178
  skip("ST_#{relationship} is unavailable") unless Foo.respond_to?("st_#{relationship}")
@@ -180,7 +195,7 @@ class RelationshipsWithGeomTest < ActiveRecordSpatialTestCase
180
195
  end
181
196
 
182
197
  class CountsTest < ActiveRecordSpatialTestCase
183
- def self.before_suite
198
+ def setup
184
199
  load_default_models
185
200
  end
186
201
 
@@ -191,7 +206,7 @@ class CountsTest < ActiveRecordSpatialTestCase
191
206
  end
192
207
 
193
208
  class WithCounterSqlTest < ActiveRecordSpatialTestCase
194
- def self.before_suite
209
+ def setup
195
210
  load_models(:foo, :bar)
196
211
  end
197
212
 
@@ -207,7 +222,7 @@ class WithCounterSqlTest < ActiveRecordSpatialTestCase
207
222
  end
208
223
 
209
224
  class ScopeOptionsTest < ActiveRecordSpatialTestCase
210
- def self.before_suite
225
+ def setup
211
226
  load_models(:foo, :bar)
212
227
 
213
228
  Foo.class_eval do
@@ -227,7 +242,7 @@ class ScopeOptionsTest < ActiveRecordSpatialTestCase
227
242
  end
228
243
 
229
244
  class PreloadTest < ActiveRecordSpatialTestCase
230
- def self.before_suite
245
+ def setup
231
246
  load_default_models
232
247
  end
233
248
 
@@ -259,7 +274,7 @@ class PreloadTest < ActiveRecordSpatialTestCase
259
274
  end
260
275
 
261
276
  class PreloadWithOtherGeomTest < ActiveRecordSpatialTestCase
262
- def self.before_suite
277
+ def setup
263
278
  load_models(:foo, :bar)
264
279
 
265
280
  Foo.class_eval do
@@ -297,7 +312,7 @@ class PreloadWithOtherGeomTest < ActiveRecordSpatialTestCase
297
312
  end
298
313
 
299
314
  class PolymorphicAssociationsTest < ActiveRecordSpatialTestCase
300
- def self.before_suite
315
+ def setup
301
316
  load_models(:foo, :bar, :zortable)
302
317
 
303
318
  Foo.class_eval do
@@ -364,7 +379,7 @@ class PolymorphicAssociationsTest < ActiveRecordSpatialTestCase
364
379
  end
365
380
 
366
381
  class PolymorphicAssociationsWithRelationshipsTest < ActiveRecordSpatialTestCase
367
- def self.before_suite
382
+ def setup
368
383
  load_models(:foo, :bar, :zortable)
369
384
  end
370
385
 
@@ -381,7 +396,7 @@ class PolymorphicAssociationsWithRelationshipsTest < ActiveRecordSpatialTestCase
381
396
  overlaps: [],
382
397
  touches: [],
383
398
  within: [1, 7],
384
- :'3dintersects' => [1, 7]
399
+ '3dintersects': [1, 7]
385
400
  }.each do |relationship, ids|
386
401
  define_method("test_#{relationship}") do
387
402
  skip("ST_#{relationship} is unavailable") unless Foo.respond_to?("st_#{relationship}")
@@ -401,7 +416,7 @@ class PolymorphicAssociationsWithRelationshipsTest < ActiveRecordSpatialTestCase
401
416
  end
402
417
 
403
418
  class ClassNameOptionTest < ActiveRecordSpatialTestCase
404
- def self.before_suite
419
+ def setup
405
420
  load_models(:foo, :bar)
406
421
 
407
422
  Foo.class_eval do
@@ -417,7 +432,7 @@ class ClassNameOptionTest < ActiveRecordSpatialTestCase
417
432
  end
418
433
 
419
434
  class ScopesTest < ActiveRecordSpatialTestCase
420
- def self.before_suite
435
+ def setup
421
436
  load_models(:foo, :bar)
422
437
 
423
438
  Foo.class_eval do
@@ -453,7 +468,7 @@ class ScopesTest < ActiveRecordSpatialTestCase
453
468
  end
454
469
 
455
470
  class GeomWrapperTest < ActiveRecordSpatialTestCase
456
- def self.before_suite
471
+ def setup
457
472
  load_models(:foo, :bar)
458
473
 
459
474
  Foo.class_eval do
@@ -487,7 +502,7 @@ class GeomWrapperTest < ActiveRecordSpatialTestCase
487
502
  end
488
503
 
489
504
  class ForeignGeomWrapperTest < ActiveRecordSpatialTestCase
490
- def self.before_suite
505
+ def setup
491
506
  load_models(:foo, :bar)
492
507
 
493
508
  Foo.class_eval do
@@ -521,7 +536,7 @@ class ForeignGeomWrapperTest < ActiveRecordSpatialTestCase
521
536
  end
522
537
 
523
538
  class BothGeomWrapperTest < ActiveRecordSpatialTestCase
524
- def self.before_suite
539
+ def setup
525
540
  load_models(:foo, :bar)
526
541
 
527
542
  Foo.class_eval do
@@ -558,7 +573,7 @@ class BothGeomWrapperTest < ActiveRecordSpatialTestCase
558
573
  end
559
574
 
560
575
  class BothGeomWrapperWithMixedSRIDsTest < ActiveRecordSpatialTestCase
561
- def self.before_suite
576
+ def setup
562
577
  load_models(:foo, :bar)
563
578
 
564
579
  Foo.class_eval do
@@ -596,7 +611,7 @@ class BothGeomWrapperWithMixedSRIDsTest < ActiveRecordSpatialTestCase
596
611
  end
597
612
 
598
613
  class BothGeomWrapperAndOptionsWithMixedSRIDsTest < ActiveRecordSpatialTestCase
599
- def self.before_suite
614
+ def setup
600
615
  load_models(:foo, :bar)
601
616
 
602
617
  Foo.class_eval do
@@ -636,7 +651,7 @@ class BothGeomWrapperAndOptionsWithMixedSRIDsTest < ActiveRecordSpatialTestCase
636
651
 
637
652
  class ScopeArgumentTest < ActiveRecordSpatialTestCase
638
653
  def setup
639
- self.class.load_models(:foo, :bar, :blort)
654
+ load_models(:foo, :bar)
640
655
  end
641
656
 
642
657
  def test_foo