activerecord-postgres-earthdistance 0.5.2 → 0.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 94eb40debf0a4955024e1c8c4213b88530b294b1
4
- data.tar.gz: ec0ec0eb7d2472ebe8bad60197bbf0a7b45f1d36
3
+ metadata.gz: 49e3fc1de6b2ece19a9479d5276208b3b0219d14
4
+ data.tar.gz: 218459e70fbd4a1086a20b93545d30b1d7df828d
5
5
  SHA512:
6
- metadata.gz: c9818e3e89ae5b0906c30fa097b26eb7445541541dbce5b31e633641f13d8a1ba797c500f4e420b0b6fad88315914e35c345971540043626ef1d1b589f5ecc35
7
- data.tar.gz: b6dfb3232de9828d812589a01a4f199ee36af6dbdda53064373253e5000370982465eec956f122e9efcdb3fdb9029f1c24e3e5cc820653146d01f56208ae5384
6
+ metadata.gz: 83e61e7ff6ec64126fb22fd32397a9711e03010e67eeb545c9aa6d5ce90760ca9b497f6d0e35b8cb8de3063f21bec45c02228bda5b50e079abd716e2d293a3c5
7
+ data.tar.gz: f9695b98ed7528a2132b2b355dd56b2f8facf98233b78c7a87c604a5355968e1473aaa7d0dded4473b9792b83745dbf6bfce829af72a52ebbc65523a668e1c56
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- activerecord-postgres-earthdistance (0.5.2)
4
+ activerecord-postgres-earthdistance (0.6.0)
5
5
  activerecord (>= 3.1)
6
6
  pg
7
7
  rake
@@ -9,30 +9,31 @@ PATH
9
9
  GEM
10
10
  remote: http://rubygems.org/
11
11
  specs:
12
- activemodel (5.0.2)
13
- activesupport (= 5.0.2)
14
- activerecord (5.0.2)
15
- activemodel (= 5.0.2)
16
- activesupport (= 5.0.2)
17
- arel (~> 7.0)
18
- activesupport (5.0.2)
12
+ activemodel (5.1.6)
13
+ activesupport (= 5.1.6)
14
+ activerecord (5.1.6)
15
+ activemodel (= 5.1.6)
16
+ activesupport (= 5.1.6)
17
+ arel (~> 8.0)
18
+ activesupport (5.1.6)
19
19
  concurrent-ruby (~> 1.0, >= 1.0.2)
20
- i18n (~> 0.7)
20
+ i18n (>= 0.7, < 2)
21
21
  minitest (~> 5.1)
22
22
  tzinfo (~> 1.1)
23
- arel (7.1.4)
23
+ arel (8.0.0)
24
24
  coderay (1.1.1)
25
25
  concurrent-ruby (1.0.5)
26
26
  diff-lcs (1.3)
27
- i18n (0.8.1)
27
+ i18n (1.0.0)
28
+ concurrent-ruby (~> 1.0)
28
29
  method_source (0.8.2)
29
- minitest (5.10.1)
30
- pg (0.20.0)
30
+ minitest (5.11.3)
31
+ pg (1.0.0)
31
32
  pry (0.10.4)
32
33
  coderay (~> 1.1.0)
33
34
  method_source (~> 0.8.1)
34
35
  slop (~> 3.4)
35
- rake (12.0.0)
36
+ rake (12.3.1)
36
37
  rdoc (5.1.0)
37
38
  rspec (3.5.0)
38
39
  rspec-core (~> 3.5.0)
@@ -49,7 +50,7 @@ GEM
49
50
  rspec-support (3.5.0)
50
51
  slop (3.6.0)
51
52
  thread_safe (0.3.6)
52
- tzinfo (1.2.3)
53
+ tzinfo (1.2.5)
53
54
  thread_safe (~> 0.1)
54
55
 
55
56
  PLATFORMS
@@ -63,4 +64,4 @@ DEPENDENCIES
63
64
  rspec
64
65
 
65
66
  BUNDLED WITH
66
- 1.14.3
67
+ 1.16.1
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "activerecord-postgres-earthdistance"
7
- s.version = "0.5.2"
7
+ s.version = "0.6.0"
8
8
 
9
9
  s.platform = Gem::Platform::RUBY
10
10
  s.license = "MIT"
@@ -3,15 +3,23 @@ module ActiveRecordPostgresEarthdistance
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  module ClassMethods
6
+ MILES_TO_METERS_FACTOR = 1609.344
6
7
  def acts_as_geolocated(options = {})
7
- cattr_accessor :latitude_column, :longitude_column, :through_table
8
- self.latitude_column = options[:lat] || (column_names.include?("lat") ? "lat" : "latitude")
9
- self.longitude_column = options[:lng] ||
10
- (column_names.include?("lng") ? "lng" : "longitude")
11
- self.through_table = options[:through]
8
+ if table_exists?
9
+ cattr_accessor :latitude_column, :longitude_column, :through_table, :distance_unit
10
+ self.latitude_column = options[:lat] || (column_names.include?("lat") ? "lat" : "latitude")
11
+ self.longitude_column = options[:lng] ||
12
+ (column_names.include?("lng") ? "lng" : "longitude")
13
+ self.through_table = options[:through]
14
+ self.distance_unit = options[:distance_unit]
15
+ else
16
+ puts "[WARNING] table #{table_name} doesn't exist, acts_as_geolocated won't work. Skip this warning if you are running db migration"
17
+ end
18
+ rescue ActiveRecord::NoDatabaseError
12
19
  end
13
20
 
14
21
  def within_box(radius, lat, lng)
22
+ radius = radius.try(:*, MILES_TO_METERS_FACTOR) if distance_unit === :miles
15
23
  earth_box = Arel::Nodes::NamedFunction.new(
16
24
  "earth_box",
17
25
  [Utils.ll_to_earth_coords(lat, lng), Utils.quote_value(radius)]
@@ -27,6 +35,7 @@ module ActiveRecordPostgresEarthdistance
27
35
  end
28
36
 
29
37
  def within_radius(radius, lat, lng)
38
+ radius = radius.try(:*, MILES_TO_METERS_FACTOR) if distance_unit === :miles
30
39
  earth_distance = Utils.earth_distance(through_table_klass, lat, lng)
31
40
  within_box(radius, lat, lng)
32
41
  .where(Arel::Nodes::InfixOperation.new("<=", earth_distance, Utils.quote_value(radius)))
@@ -73,6 +73,74 @@ describe "ActiveRecord::Base.act_as_geolocated" do
73
73
  end
74
74
  end
75
75
 
76
+ describe "#within_box with miles" do
77
+ let(:test_data) { { lat: nil, lng: nil, radius: nil } }
78
+
79
+ subject { Place.within_box(test_data[:radius], test_data[:lat], test_data[:lng]) }
80
+
81
+ before(:all) {
82
+ Place.acts_as_geolocated distance_unit: :miles
83
+ @place = Place.create!(lat: -30.0277041, lng: -51.2287346)
84
+ }
85
+ after(:all) {
86
+ Place.acts_as_geolocated
87
+ @place.destroy
88
+ }
89
+
90
+ context "when query with null data" do
91
+ it { is_expected.to be_empty }
92
+ end
93
+
94
+ context "when query for the exact same point with radius 0" do
95
+ let(:test_data) { { lat: -30.0277041, lng: -51.2287346, radius: 0 } }
96
+
97
+ it { is_expected.to eq [@place] }
98
+ end
99
+
100
+ context "when query for place within the box" do
101
+ let(:test_data) { { radius: 2400, lat: -27.5969039, lng: -48.5494544 } }
102
+
103
+ it { is_expected.to eq [@place] }
104
+ end
105
+
106
+ context "when query for place within the box, but outside the radius" do
107
+ let(:test_data) { { radius: 186, lat: -27.5969039, lng: -48.5494544 } }
108
+
109
+ it "the place shouldn't be within the radius" do
110
+ expect(Place.within_radius(test_data[:radius], test_data[:lat], test_data[:lng])).to be_empty
111
+ end
112
+
113
+ it { is_expected.to eq [@place] }
114
+ end
115
+
116
+ context "when query for place outside the box" do
117
+ let(:test_data) { { radius: 0.62, lat: -27.5969039, lng: -48.5494544 } }
118
+ it { is_expected.to be_empty }
119
+ end
120
+
121
+ context "when joining tables that are also geoloacted" do
122
+ let(:test_data) { { radius: 0.62, lat: -27.5969039, lng: -48.5494544 } }
123
+
124
+ subject { Place.within_box(test_data[:radius], test_data[:lat], test_data[:lng]) }
125
+
126
+ it "should work with objects having columns with the same name" do
127
+ expect do
128
+ Place
129
+ .joins(:events)
130
+ .within_radius(test_data[:radius], test_data[:lat], test_data[:lng]).to_a
131
+ end.to_not raise_error
132
+ end
133
+
134
+ it "should work with nested associations" do
135
+ expect do
136
+ Event
137
+ .joins(:events)
138
+ .within_radius(test_data[:radius], test_data[:lat], test_data[:lng]).to_a
139
+ end.to_not raise_error
140
+ end
141
+ end
142
+ end
143
+
76
144
  describe "#within_radius" do
77
145
  let(:test_data) { { lat: nil, lng: nil, radius: nil } }
78
146
  subject { Place.within_radius(test_data[:radius], test_data[:lat], test_data[:lng]) }
@@ -139,6 +207,75 @@ describe "ActiveRecord::Base.act_as_geolocated" do
139
207
  end
140
208
  end
141
209
 
210
+ describe "#within_radius with miles" do
211
+ let(:test_data) { { lat: nil, lng: nil, radius: nil } }
212
+ subject { Place.within_radius(test_data[:radius], test_data[:lat], test_data[:lng]) }
213
+ before(:all) do
214
+ # Place.distance_unit = :miles
215
+ Place.acts_as_geolocated distance_unit: :miles
216
+ @place = Place.create!(lat: -30.0277041, lng: -51.2287346)
217
+ end
218
+
219
+ after(:all) do
220
+ Place.acts_as_geolocated
221
+ @place.destroy
222
+ end
223
+
224
+ context "when query with null data" do
225
+ it { is_expected.to eq [] }
226
+ end
227
+
228
+ context "when query for the exact same point with radius 0" do
229
+ let(:test_data) { { lat: -30.0277041, lng: -51.2287346, radius: 0 } }
230
+ it { is_expected.to eq [@place] }
231
+ end
232
+
233
+ context "when query for place within radius" do
234
+ let(:test_data) { { radius: 2400, lat: -27.5969039, lng: -48.5494544 } }
235
+ it { is_expected.to eq [@place] }
236
+ end
237
+
238
+ context "when query for place outside the radius" do
239
+ let(:test_data) { { radius: 0.62, lat: -27.5969039, lng: -48.5494544 } }
240
+ it { is_expected.to eq [] }
241
+ end
242
+
243
+ context "uses lat and long of through table" do
244
+ subject do
245
+ Job.within_radius(test_data[:radius], test_data[:lat], test_data[:lng])
246
+ end
247
+
248
+ before(:all) do
249
+ @event = Event.create!(lat: -30.0277041, lng: -51.2287346)
250
+ @job = Job.create!(event: @event)
251
+ end
252
+
253
+ after(:all) do
254
+ @event.destroy
255
+ @job.destroy
256
+ end
257
+
258
+ context "when query with null data" do
259
+ it { is_expected.to eq [] }
260
+ end
261
+
262
+ context "when query for the exact same point with radius 0" do
263
+ let(:test_data) { { lat: -30.0277041, lng: -51.2287346, radius: 0 } }
264
+ it { is_expected.to eq [@job] }
265
+ end
266
+
267
+ context "when query for place within radius" do
268
+ let(:test_data) { { radius: 4_000_000, lat: -27.5969039, lng: -48.5494544 } }
269
+ it { is_expected.to eq [@job] }
270
+ end
271
+
272
+ context "when query for place outside the radius" do
273
+ let(:test_data) { { radius: 1000, lat: -27.5969039, lng: -48.5494544 } }
274
+ it { is_expected.to eq [] }
275
+ end
276
+ end
277
+ end
278
+
142
279
  describe "#order_by_distance" do
143
280
  let(:current_location) { { lat: nil, lng: nil, radius: nil } }
144
281
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-postgres-earthdistance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diogo Biazus
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-01 00:00:00.000000000 Z
11
+ date: 2018-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -159,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
159
  version: 1.3.6
160
160
  requirements: []
161
161
  rubyforge_project:
162
- rubygems_version: 2.5.1
162
+ rubygems_version: 2.6.13
163
163
  signing_key:
164
164
  specification_version: 4
165
165
  summary: Check distances with latitude and longitude using PostgreSQL special indexes