activerecord-postgres-earthdistance 0.4.0 → 0.4.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.
- checksums.yaml +4 -4
- data/Gemfile.lock +66 -47
- data/README.md +5 -1
- data/activerecord-postgres-earthdistance.gemspec +1 -1
- data/lib/activerecord-postgres-earthdistance/activerecord.rb +4 -3
- data/lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb +22 -10
- data/spec/act_as_geolocated_spec.rb +15 -0
- data/spec/fixtures/event.rb +5 -0
- data/spec/fixtures/place.rb +1 -0
- data/spec/spec_helper.rb +4 -1
- metadata +5 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ba84eef72063d43d8409981a0de2169081953159
|
|
4
|
+
data.tar.gz: 8d9775c25f7dbafe1df807816c9e1e72dca92718
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ef50f5bba44fa8612cb55f3837dedb9fd1a66990d6a44fbfe000aafebb341d5b98899c54f739af4dbcec95b7b29ee27b60a2797b95d215b2061d5383124e8622
|
|
7
|
+
data.tar.gz: 84f152880d57a62d38aff7ec1d1f04b45882b3019deb74abb2e4381a2940a110a61cfd61c4315bcc5cfce26696ff825367acec64bdd88a96ff3e53f73a7bbd55
|
data/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
activerecord-postgres-earthdistance (0.4.
|
|
4
|
+
activerecord-postgres-earthdistance (0.4.1)
|
|
5
5
|
pg
|
|
6
6
|
rails (>= 3.1)
|
|
7
7
|
rake
|
|
@@ -9,61 +9,84 @@ PATH
|
|
|
9
9
|
GEM
|
|
10
10
|
remote: http://rubygems.org/
|
|
11
11
|
specs:
|
|
12
|
-
actionmailer (4.1
|
|
13
|
-
actionpack (= 4.1
|
|
14
|
-
actionview (= 4.1
|
|
12
|
+
actionmailer (4.2.1)
|
|
13
|
+
actionpack (= 4.2.1)
|
|
14
|
+
actionview (= 4.2.1)
|
|
15
|
+
activejob (= 4.2.1)
|
|
15
16
|
mail (~> 2.5, >= 2.5.4)
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
|
18
|
+
actionpack (4.2.1)
|
|
19
|
+
actionview (= 4.2.1)
|
|
20
|
+
activesupport (= 4.2.1)
|
|
21
|
+
rack (~> 1.6)
|
|
20
22
|
rack-test (~> 0.6.2)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
|
24
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.1)
|
|
25
|
+
actionview (4.2.1)
|
|
26
|
+
activesupport (= 4.2.1)
|
|
23
27
|
builder (~> 3.1)
|
|
24
28
|
erubis (~> 2.7.0)
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
|
30
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.1)
|
|
31
|
+
activejob (4.2.1)
|
|
32
|
+
activesupport (= 4.2.1)
|
|
33
|
+
globalid (>= 0.3.0)
|
|
34
|
+
activemodel (4.2.1)
|
|
35
|
+
activesupport (= 4.2.1)
|
|
27
36
|
builder (~> 3.1)
|
|
28
|
-
activerecord (4.1
|
|
29
|
-
activemodel (= 4.1
|
|
30
|
-
activesupport (= 4.1
|
|
31
|
-
arel (~>
|
|
32
|
-
activesupport (4.1
|
|
33
|
-
i18n (~> 0.
|
|
37
|
+
activerecord (4.2.1)
|
|
38
|
+
activemodel (= 4.2.1)
|
|
39
|
+
activesupport (= 4.2.1)
|
|
40
|
+
arel (~> 6.0)
|
|
41
|
+
activesupport (4.2.1)
|
|
42
|
+
i18n (~> 0.7)
|
|
34
43
|
json (~> 1.7, >= 1.7.7)
|
|
35
44
|
minitest (~> 5.1)
|
|
36
|
-
thread_safe (~> 0.
|
|
45
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
|
37
46
|
tzinfo (~> 1.1)
|
|
38
|
-
arel (
|
|
47
|
+
arel (6.0.0)
|
|
39
48
|
builder (3.2.2)
|
|
40
49
|
diff-lcs (1.2.5)
|
|
41
50
|
erubis (2.7.0)
|
|
42
|
-
|
|
43
|
-
|
|
51
|
+
globalid (0.3.5)
|
|
52
|
+
activesupport (>= 4.1.0)
|
|
53
|
+
i18n (0.7.0)
|
|
44
54
|
json (1.8.1)
|
|
55
|
+
loofah (2.0.2)
|
|
56
|
+
nokogiri (>= 1.5.9)
|
|
45
57
|
mail (2.6.3)
|
|
46
58
|
mime-types (>= 1.16, < 3)
|
|
47
|
-
mime-types (2.
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
59
|
+
mime-types (2.6.1)
|
|
60
|
+
mini_portile (0.6.2)
|
|
61
|
+
minitest (5.7.0)
|
|
62
|
+
nokogiri (1.6.6.2)
|
|
63
|
+
mini_portile (~> 0.6.0)
|
|
64
|
+
pg (0.18.2)
|
|
65
|
+
rack (1.6.1)
|
|
66
|
+
rack-test (0.6.3)
|
|
53
67
|
rack (>= 1.0)
|
|
54
|
-
rails (4.1
|
|
55
|
-
actionmailer (= 4.1
|
|
56
|
-
actionpack (= 4.1
|
|
57
|
-
actionview (= 4.1
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
68
|
+
rails (4.2.1)
|
|
69
|
+
actionmailer (= 4.2.1)
|
|
70
|
+
actionpack (= 4.2.1)
|
|
71
|
+
actionview (= 4.2.1)
|
|
72
|
+
activejob (= 4.2.1)
|
|
73
|
+
activemodel (= 4.2.1)
|
|
74
|
+
activerecord (= 4.2.1)
|
|
75
|
+
activesupport (= 4.2.1)
|
|
61
76
|
bundler (>= 1.3.0, < 2.0)
|
|
62
|
-
railties (= 4.1
|
|
63
|
-
sprockets-rails
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
77
|
+
railties (= 4.2.1)
|
|
78
|
+
sprockets-rails
|
|
79
|
+
rails-deprecated_sanitizer (1.0.3)
|
|
80
|
+
activesupport (>= 4.2.0.alpha)
|
|
81
|
+
rails-dom-testing (1.0.6)
|
|
82
|
+
activesupport (>= 4.2.0.beta, < 5.0)
|
|
83
|
+
nokogiri (~> 1.6.0)
|
|
84
|
+
rails-deprecated_sanitizer (>= 1.0.1)
|
|
85
|
+
rails-html-sanitizer (1.0.2)
|
|
86
|
+
loofah (~> 2.0)
|
|
87
|
+
railties (4.2.1)
|
|
88
|
+
actionpack (= 4.2.1)
|
|
89
|
+
activesupport (= 4.2.1)
|
|
67
90
|
rake (>= 0.8.7)
|
|
68
91
|
thor (>= 0.18.1, < 2.0)
|
|
69
92
|
rake (10.4.2)
|
|
@@ -77,18 +100,14 @@ GEM
|
|
|
77
100
|
rspec-expectations (2.99.2)
|
|
78
101
|
diff-lcs (>= 1.1.3, < 2.0)
|
|
79
102
|
rspec-mocks (2.99.2)
|
|
80
|
-
sprockets (
|
|
81
|
-
hike (~> 1.2)
|
|
82
|
-
multi_json (~> 1.0)
|
|
103
|
+
sprockets (3.1.0)
|
|
83
104
|
rack (~> 1.0)
|
|
84
|
-
|
|
85
|
-
sprockets-rails (2.2.2)
|
|
105
|
+
sprockets-rails (2.3.1)
|
|
86
106
|
actionpack (>= 3.0)
|
|
87
107
|
activesupport (>= 3.0)
|
|
88
108
|
sprockets (>= 2.8, < 4.0)
|
|
89
109
|
thor (0.19.1)
|
|
90
|
-
thread_safe (0.3.
|
|
91
|
-
tilt (1.4.1)
|
|
110
|
+
thread_safe (0.3.5)
|
|
92
111
|
tzinfo (1.2.2)
|
|
93
112
|
thread_safe (~> 0.1)
|
|
94
113
|
|
data/README.md
CHANGED
|
@@ -4,13 +4,14 @@ module ActiveRecord
|
|
|
4
4
|
module ConnectionAdapters
|
|
5
5
|
module SchemaStatements
|
|
6
6
|
|
|
7
|
-
# Installs hstore by creating the Postgres extension
|
|
8
|
-
# if it does not exist
|
|
9
|
-
#
|
|
10
7
|
def add_earthdistance_index table_name, options = {}
|
|
11
8
|
execute "CREATE INDEX %s_earthdistance_ix ON %s USING gist (ll_to_earth(%s, %s));" %
|
|
12
9
|
[table_name, table_name, (options[:lat] || 'lat'), (options[:lng] || 'lng')]
|
|
13
10
|
end
|
|
11
|
+
|
|
12
|
+
def remove_earthdistance_index table_name
|
|
13
|
+
execute "DROP INDEX %s_earthdistance_ix;" % [table_name]
|
|
14
|
+
end
|
|
14
15
|
end
|
|
15
16
|
end
|
|
16
17
|
end
|
|
@@ -2,9 +2,6 @@ module ActiveRecordPostgresEarthdistance
|
|
|
2
2
|
module ActsAsGeolocated
|
|
3
3
|
extend ActiveSupport::Concern
|
|
4
4
|
|
|
5
|
-
included do
|
|
6
|
-
end
|
|
7
|
-
|
|
8
5
|
module ClassMethods
|
|
9
6
|
def acts_as_geolocated(options = {})
|
|
10
7
|
if table_exists?
|
|
@@ -14,16 +11,32 @@ module ActiveRecordPostgresEarthdistance
|
|
|
14
11
|
end
|
|
15
12
|
end
|
|
16
13
|
|
|
17
|
-
def within_box
|
|
18
|
-
|
|
14
|
+
def within_box(radius, lat, lng)
|
|
15
|
+
earth_box = Arel::Nodes::NamedFunction.new('earth_box', [ll_to_earth_coords(lat, lng), radius])
|
|
16
|
+
where Arel::Nodes::InfixOperation.new('<@', ll_to_earth_columns, earth_box)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def within_radius(radius, lat, lng)
|
|
20
|
+
earth_distance = Arel::Nodes::NamedFunction.new('earth_distance', [ll_to_earth_columns, ll_to_earth_coords(lat, lng)])
|
|
21
|
+
within_box(radius, lat, lng).where(Arel::Nodes::InfixOperation.new('<=', earth_distance, radius))
|
|
19
22
|
end
|
|
20
23
|
|
|
21
|
-
def
|
|
22
|
-
|
|
24
|
+
def order_by_distance(lat, lng, order = "ASC")
|
|
25
|
+
earth_distance = Arel::Nodes::NamedFunction.new('earth_distance', [ll_to_earth_columns, ll_to_earth_coords(lat, lng)])
|
|
26
|
+
order("#{earth_distance.to_sql} #{order.to_s}")
|
|
23
27
|
end
|
|
24
28
|
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
protected
|
|
30
|
+
def ll_to_earth_columns
|
|
31
|
+
Arel::Nodes::NamedFunction.new('ll_to_earth', [arel_table[self.latitude_column], arel_table[self.longitude_column]])
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def ll_to_earth_coords lat, lng
|
|
35
|
+
if Arel::Nodes.respond_to?(:build_quoted) # for arel >= 6.0.0
|
|
36
|
+
Arel::Nodes::NamedFunction.new('ll_to_earth', [Arel::Nodes.build_quoted(lat), Arel::Nodes.build_quoted(lng)])
|
|
37
|
+
else
|
|
38
|
+
Arel::Nodes::NamedFunction.new('ll_to_earth', [lat, lng])
|
|
39
|
+
end
|
|
27
40
|
end
|
|
28
41
|
end
|
|
29
42
|
end
|
|
@@ -38,7 +51,6 @@ module ActiveRecordPostgresEarthdistance
|
|
|
38
51
|
end
|
|
39
52
|
end
|
|
40
53
|
end
|
|
41
|
-
|
|
42
54
|
end
|
|
43
55
|
|
|
44
56
|
ActiveRecord::Base.send :include, ActiveRecordPostgresEarthdistance::ActsAsGeolocated
|
|
@@ -39,6 +39,21 @@ describe "ActiveRecord::Base.act_as_geolocated" do
|
|
|
39
39
|
let(:test_data) { { radius: 1000, lat: -27.5969039, lng: -48.5494544 } }
|
|
40
40
|
it { should be_empty }
|
|
41
41
|
end
|
|
42
|
+
|
|
43
|
+
context "when joining tables that are also geoloacted" do
|
|
44
|
+
let(:test_data) { { radius: 1000, lat: -27.5969039, lng: -48.5494544 } }
|
|
45
|
+
|
|
46
|
+
subject { Place.within_box(test_data[:radius], test_data[:lat], test_data[:lng]) }
|
|
47
|
+
|
|
48
|
+
it "should work with objects having columns with the same name" do
|
|
49
|
+
expect { Place.joins(:events).within_radius(test_data[:radius], test_data[:lat], test_data[:lng]).to_a }.to_not raise_error
|
|
50
|
+
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "should work with nested associations" do
|
|
54
|
+
expect { Event.joins(:events).within_radius(test_data[:radius], test_data[:lat], test_data[:lng]).to_a }.to_not raise_error
|
|
55
|
+
end
|
|
56
|
+
end
|
|
42
57
|
end
|
|
43
58
|
|
|
44
59
|
describe "#within_radius" do
|
data/spec/fixtures/place.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
|
@@ -23,7 +23,9 @@ RSpec.configure do |config|
|
|
|
23
23
|
CREATE EXTENSION IF NOT EXISTS cube;
|
|
24
24
|
CREATE EXTENSION IF NOT EXISTS earthdistance;
|
|
25
25
|
DROP TABLE IF EXISTS places;
|
|
26
|
+
DROP TABLE IF EXISTS events;
|
|
26
27
|
CREATE TABLE places (id serial PRIMARY KEY, data text, lat float8, lng float8);
|
|
28
|
+
CREATE TABLE events (id serial PRIMARY KEY, event_id integer, place_id integer, data text, lat float8, lng float8);
|
|
27
29
|
}
|
|
28
30
|
rescue Exception => e
|
|
29
31
|
puts "Exception: #{e}"
|
|
@@ -39,8 +41,9 @@ RSpec.configure do |config|
|
|
|
39
41
|
retry
|
|
40
42
|
end
|
|
41
43
|
|
|
42
|
-
# Load
|
|
44
|
+
# Load models used in spec
|
|
43
45
|
require 'fixtures/place'
|
|
46
|
+
require 'fixtures/event'
|
|
44
47
|
end
|
|
45
48
|
|
|
46
49
|
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.
|
|
4
|
+
version: 0.4.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Diogo Biazus
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2015-06-17 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|
|
@@ -120,6 +120,7 @@ files:
|
|
|
120
120
|
- lib/templates/earthdistance.sql
|
|
121
121
|
- lib/templates/setup_earthdistance.rb
|
|
122
122
|
- spec/act_as_geolocated_spec.rb
|
|
123
|
+
- spec/fixtures/event.rb
|
|
123
124
|
- spec/fixtures/place.rb
|
|
124
125
|
- spec/spec_helper.rb
|
|
125
126
|
homepage: http://github.com/diogob/activerecord-postgres-earthdistance
|
|
@@ -142,11 +143,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
142
143
|
version: 1.3.6
|
|
143
144
|
requirements: []
|
|
144
145
|
rubyforge_project:
|
|
145
|
-
rubygems_version: 2.
|
|
146
|
+
rubygems_version: 2.4.5
|
|
146
147
|
signing_key:
|
|
147
148
|
specification_version: 4
|
|
148
149
|
summary: Check distances with latitude and longitude using PostgreSQL special indexes
|
|
149
150
|
test_files:
|
|
150
151
|
- spec/act_as_geolocated_spec.rb
|
|
152
|
+
- spec/fixtures/event.rb
|
|
151
153
|
- spec/fixtures/place.rb
|
|
152
154
|
- spec/spec_helper.rb
|