activerecord-postgres-earthdistance 0.4.0 → 0.4.1

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: f9458af5bee1a6c5416b450109d62aa9888f37d7
4
- data.tar.gz: 52a8f7eb26951d8c77f3b1332b630ed5978f560f
3
+ metadata.gz: ba84eef72063d43d8409981a0de2169081953159
4
+ data.tar.gz: 8d9775c25f7dbafe1df807816c9e1e72dca92718
5
5
  SHA512:
6
- metadata.gz: 7e53a9aa32535c9ffd538d28221f99618f7497add3a8b6f4f63b38c4fee66e05feb6d0ae420c0ecec8bf8f4dbb7271564b586751a139c49be7f39d06e9e7642c
7
- data.tar.gz: 895a6791fdd7d3a6ed3b73b70254a136cb119517f0dc5ba341c0e7a1f02aee61aa34d3915ee68412cfeaa06cc42a2c192d9bded4600e2fdb6edf2a12d2c382d5
6
+ metadata.gz: ef50f5bba44fa8612cb55f3837dedb9fd1a66990d6a44fbfe000aafebb341d5b98899c54f739af4dbcec95b7b29ee27b60a2797b95d215b2061d5383124e8622
7
+ data.tar.gz: 84f152880d57a62d38aff7ec1d1f04b45882b3019deb74abb2e4381a2940a110a61cfd61c4315bcc5cfce26696ff825367acec64bdd88a96ff3e53f73a7bbd55
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- activerecord-postgres-earthdistance (0.4.0)
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.8)
13
- actionpack (= 4.1.8)
14
- actionview (= 4.1.8)
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
- actionpack (4.1.8)
17
- actionview (= 4.1.8)
18
- activesupport (= 4.1.8)
19
- rack (~> 1.5.2)
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
- actionview (4.1.8)
22
- activesupport (= 4.1.8)
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
- activemodel (4.1.8)
26
- activesupport (= 4.1.8)
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.8)
29
- activemodel (= 4.1.8)
30
- activesupport (= 4.1.8)
31
- arel (~> 5.0.0)
32
- activesupport (4.1.8)
33
- i18n (~> 0.6, >= 0.6.9)
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.1)
45
+ thread_safe (~> 0.3, >= 0.3.4)
37
46
  tzinfo (~> 1.1)
38
- arel (5.0.1.20140414130214)
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
- hike (1.2.3)
43
- i18n (0.6.11)
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.4.3)
48
- minitest (5.4.3)
49
- multi_json (1.10.1)
50
- pg (0.17.1)
51
- rack (1.5.2)
52
- rack-test (0.6.2)
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.8)
55
- actionmailer (= 4.1.8)
56
- actionpack (= 4.1.8)
57
- actionview (= 4.1.8)
58
- activemodel (= 4.1.8)
59
- activerecord (= 4.1.8)
60
- activesupport (= 4.1.8)
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.8)
63
- sprockets-rails (~> 2.0)
64
- railties (4.1.8)
65
- actionpack (= 4.1.8)
66
- activesupport (= 4.1.8)
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 (2.12.3)
81
- hike (~> 1.2)
82
- multi_json (~> 1.0)
103
+ sprockets (3.1.0)
83
104
  rack (~> 1.0)
84
- tilt (~> 1.1, != 1.3.0)
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.4)
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
@@ -45,9 +45,13 @@ Edit the created migration:
45
45
 
46
46
  ```ruby
47
47
  class AddIndexToPlaces < ActiveRecord::Migration
48
- def change
48
+ def up
49
49
  add_earthdistance_index :places
50
50
  end
51
+
52
+ def down
53
+ remove_earthdistance_index :places
54
+ end
51
55
  end
52
56
  ```
53
57
 
@@ -4,7 +4,7 @@ $:.unshift lib unless $:.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "activerecord-postgres-earthdistance"
7
- s.version = "0.4.0"
7
+ s.version = "0.4.1"
8
8
 
9
9
  s.platform = Gem::Platform::RUBY
10
10
  s.license = "MIT"
@@ -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 radius, lat, lng
18
- where("ll_to_earth(#{self.latitude_column}, #{self.longitude_column}) <@ earth_box(ll_to_earth(?, ?), ?)", lat, lng, radius)
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 within_radius radius, lat, lng
22
- within_box(radius, lat, lng).where("earth_distance(ll_to_earth(#{self.latitude_column}, #{self.longitude_column}), ll_to_earth(?, ?)) <= ?", lat, lng, radius)
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
- def order_by_distance lat, lng, order= "ASC"
26
- order("earth_distance(ll_to_earth(#{self.latitude_column}, #{self.longitude_column}), ll_to_earth(#{lat}, #{lng})) #{order}")
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
@@ -0,0 +1,5 @@
1
+ class Event < ActiveRecord::Base
2
+ acts_as_geolocated
3
+ belongs_to :place
4
+ has_many :events
5
+ end
@@ -1,3 +1,4 @@
1
1
  class Place < ActiveRecord::Base
2
2
  acts_as_geolocated
3
+ has_many :events
3
4
  end
@@ -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 model used in spec
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.0
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: 2014-12-08 00:00:00.000000000 Z
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.2.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