activerecord-postgres-earthdistance 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c47a1398981c4a66255e7539712df6343c33791
|
4
|
+
data.tar.gz: 2ff076ca1b78a76daa37b915dbe5b15c02b2f904
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01d68f1a5c84ccc17dda51536625e38ef64f790214035cccad2c9b359c8889bad9837f31e108fb307fb02baf54a6ad7418322d518a43b888ba80d880cf511daf
|
7
|
+
data.tar.gz: '0719bc8b1e9c9f8dddb2610536868f6b8027eb3adc8fa19d640c3d2df89a4f73ccb1533f405c40f57c10c866575e27198cf881eb023ed6934958aa890cefcaff'
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
activerecord-postgres-earthdistance (0.
|
4
|
+
activerecord-postgres-earthdistance (0.7.0)
|
5
5
|
activerecord (>= 3.1)
|
6
6
|
pg
|
7
7
|
rake
|
@@ -9,22 +9,22 @@ PATH
|
|
9
9
|
GEM
|
10
10
|
remote: http://rubygems.org/
|
11
11
|
specs:
|
12
|
-
activemodel (5.
|
13
|
-
activesupport (= 5.
|
14
|
-
activerecord (5.
|
15
|
-
activemodel (= 5.
|
16
|
-
activesupport (= 5.
|
17
|
-
arel (
|
18
|
-
activesupport (5.
|
12
|
+
activemodel (5.2.0)
|
13
|
+
activesupport (= 5.2.0)
|
14
|
+
activerecord (5.2.0)
|
15
|
+
activemodel (= 5.2.0)
|
16
|
+
activesupport (= 5.2.0)
|
17
|
+
arel (>= 9.0)
|
18
|
+
activesupport (5.2.0)
|
19
19
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
20
20
|
i18n (>= 0.7, < 2)
|
21
21
|
minitest (~> 5.1)
|
22
22
|
tzinfo (~> 1.1)
|
23
|
-
arel (
|
23
|
+
arel (9.0.0)
|
24
24
|
coderay (1.1.1)
|
25
25
|
concurrent-ruby (1.0.5)
|
26
26
|
diff-lcs (1.3)
|
27
|
-
i18n (1.0.
|
27
|
+
i18n (1.0.1)
|
28
28
|
concurrent-ruby (~> 1.0)
|
29
29
|
method_source (0.8.2)
|
30
30
|
minitest (5.11.3)
|
@@ -1,9 +1,10 @@
|
|
1
1
|
module ActiveRecordPostgresEarthdistance
|
2
|
+
MILES_TO_METERS_FACTOR = 1609.344
|
2
3
|
module ActsAsGeolocated
|
3
4
|
extend ActiveSupport::Concern
|
5
|
+
|
4
6
|
|
5
|
-
module ClassMethods
|
6
|
-
MILES_TO_METERS_FACTOR = 1609.344
|
7
|
+
module ClassMethods
|
7
8
|
def acts_as_geolocated(options = {})
|
8
9
|
if table_exists?
|
9
10
|
cattr_accessor :latitude_column, :longitude_column, :through_table, :distance_unit
|
@@ -43,7 +44,7 @@ module ActiveRecordPostgresEarthdistance
|
|
43
44
|
|
44
45
|
def order_by_distance(lat, lng, order = "ASC")
|
45
46
|
earth_distance = Utils.earth_distance(through_table_klass, lat, lng)
|
46
|
-
joins(through_table).order("#{earth_distance.to_sql} #{order}")
|
47
|
+
joins(through_table).order(Arel.sql("#{earth_distance.to_sql} #{order}"))
|
47
48
|
end
|
48
49
|
|
49
50
|
def through_table_klass
|
@@ -96,7 +97,12 @@ module ActiveRecordPostgresEarthdistance
|
|
96
97
|
if relation.select_values.empty? && include_default_columns
|
97
98
|
values << relation.arel_table[Arel.star]
|
98
99
|
end
|
99
|
-
|
100
|
+
distances = Utils.earth_distance(through_table_klass, lat, lng, name)
|
101
|
+
distances = Arel::Nodes::Multiplication.new(
|
102
|
+
Utils.quote_value(1 / MILES_TO_METERS_FACTOR), distances
|
103
|
+
) if relation.distance_unit === :miles
|
104
|
+
|
105
|
+
values << distances
|
100
106
|
|
101
107
|
relation.select_values = values
|
102
108
|
end
|
@@ -358,4 +358,46 @@ describe "ActiveRecord::Base.act_as_geolocated" do
|
|
358
358
|
end
|
359
359
|
end
|
360
360
|
end
|
361
|
+
|
362
|
+
describe "#selecting_distance_from with miles" do
|
363
|
+
let(:current_location) { { lat: nil, lng: nil, radius: nil } }
|
364
|
+
subject do
|
365
|
+
Place
|
366
|
+
.order_by_distance(current_location[:lat], current_location[:lng])
|
367
|
+
.selecting_distance_from(current_location[:lat], current_location[:lng])
|
368
|
+
.first
|
369
|
+
.try { |p| [p.data, p.distance.to_f] }
|
370
|
+
end
|
371
|
+
before(:all) do
|
372
|
+
Place.acts_as_geolocated distance_unit: :miles
|
373
|
+
@place = Place.create!(data: "Amsterdam", lat: 52.370216, lng: 4.895168) # Amsterdam
|
374
|
+
end
|
375
|
+
after(:all) do
|
376
|
+
Place.acts_as_geolocated
|
377
|
+
@place.destroy
|
378
|
+
end
|
379
|
+
context "when selecting distance" do
|
380
|
+
let(:current_location) { { lat: 52.229676, lng: 21.012229 } } # Warsaw
|
381
|
+
it { is_expected.to eq ["Amsterdam", 680.410076641856] }
|
382
|
+
end
|
383
|
+
|
384
|
+
context "through table" do
|
385
|
+
|
386
|
+
subject { Job.all.selecting_distance_from(current_location[:lat], current_location[:lng]).first }
|
387
|
+
|
388
|
+
before(:all) do
|
389
|
+
@event = Event.create!(lat: -30.0277041, lng: -51.2287346)
|
390
|
+
@job = Job.create!(event: @event)
|
391
|
+
end
|
392
|
+
|
393
|
+
after(:all) do
|
394
|
+
@event.destroy
|
395
|
+
@job.destroy
|
396
|
+
end
|
397
|
+
|
398
|
+
context "when selecting distance" do
|
399
|
+
it { is_expected.to respond_to :distance }
|
400
|
+
end
|
401
|
+
end
|
402
|
+
end
|
361
403
|
end
|
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.
|
4
|
+
version: 0.7.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: 2018-04-
|
11
|
+
date: 2018-04-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|