on_the_map 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +47 -0
- data/Gemfile.lock +187 -0
- data/LICENSE.txt +20 -0
- data/README.md +70 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/address.rb +71 -0
- data/lib/on_the_map/addressable.rb +62 -0
- data/lib/on_the_map/geo_locatable.rb +95 -0
- data/lib/on_the_map/mappable.rb +47 -0
- data/lib/on_the_map/positionable.rb +29 -0
- data/lib/on_the_map.rb +0 -0
- data/on_the_map.gemspec +87 -0
- data/spec/address_spec.rb +5 -0
- data/spec/on_the_map/addressable_spec.rb +71 -0
- data/spec/on_the_map/geo_locatable_spec.rb +44 -0
- data/spec/on_the_map/geo_locatable_valid_spec.rb +58 -0
- data/spec/on_the_map/mappable_spec.rb +84 -0
- data/spec/on_the_map/positionable_spec.rb +52 -0
- data/spec/on_the_map_spec.rb +5 -0
- data/spec/spec_helper.rb +44 -0
- data/spec/support/gmaps_lookup_stubs.rb +86 -0
- metadata +219 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
gem 'mongoid', '>= 4', github: 'mongoid/mongoid'
|
4
|
+
gem 'sugar-high', '~> 0.7.3', github: 'kristianmandrup/sugar-high'
|
5
|
+
|
6
|
+
gem 'hashie', '~> 1.2'
|
7
|
+
gem 'concerned', '~> 0.1.3'
|
8
|
+
|
9
|
+
gem "rspec", ">= 2.12.0", group: [:test, :development]
|
10
|
+
|
11
|
+
group :test do
|
12
|
+
gem 'rails', '>= 4.0.0.beta1', github: 'rails/rails' # '>= 3.1' #
|
13
|
+
gem 'geocoder'
|
14
|
+
gem 'mongoid_geospatial'
|
15
|
+
gem 'mongoid_indexing', '~> 0.1.2'
|
16
|
+
|
17
|
+
gem 'gmaps4rails', '>= 2.0.0.pre', github: 'apneadiving/Google-Maps-for-Rails'
|
18
|
+
# gem 'gmaps-autocomplete-rails'
|
19
|
+
|
20
|
+
# time travel
|
21
|
+
# gem 'timecop', '~> 0.3.5', :git => 'git://github.com/change/timecop.git'
|
22
|
+
gem 'delorean', '~> 1.1.1'
|
23
|
+
|
24
|
+
gem 'cutter', '~> 0.8.2'
|
25
|
+
|
26
|
+
# data store
|
27
|
+
gem 'database_cleaner', '>= 0.8'
|
28
|
+
|
29
|
+
gem 'factory_girl'
|
30
|
+
|
31
|
+
# Fake data
|
32
|
+
gem 'ffaker', '>= 1.14'
|
33
|
+
gem 'fakeweb'
|
34
|
+
|
35
|
+
gem 'shoulda'
|
36
|
+
gem 'shoulda-matchers'
|
37
|
+
|
38
|
+
# http://railsapps.github.com/tutorial-rails-mongoid-devise.html
|
39
|
+
# gem 'mongoid-rspec', '>= 1.7', github: 'evansagge/mongoid-rspec'
|
40
|
+
end
|
41
|
+
|
42
|
+
group :development do
|
43
|
+
gem "rdoc", ">= 3.1"
|
44
|
+
gem "bundler", ">= 1.1.0"
|
45
|
+
gem "jeweler", ">= 1.8.4"
|
46
|
+
gem "simplecov",">= 0.5"
|
47
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,187 @@
|
|
1
|
+
GIT
|
2
|
+
remote: git://github.com/apneadiving/Google-Maps-for-Rails.git
|
3
|
+
revision: 3ff52deb455e1077c9f60660486fcd38f78e737f
|
4
|
+
specs:
|
5
|
+
gmaps4rails (2.0.0.pre)
|
6
|
+
|
7
|
+
GIT
|
8
|
+
remote: git://github.com/kristianmandrup/sugar-high.git
|
9
|
+
revision: a664566b1ae94a404553c2fe2434165e0c5f550b
|
10
|
+
specs:
|
11
|
+
sugar-high (0.7.3)
|
12
|
+
|
13
|
+
GIT
|
14
|
+
remote: git://github.com/mongoid/mongoid.git
|
15
|
+
revision: c2170560124b95c11f03f0753b0b9cc89ff80719
|
16
|
+
specs:
|
17
|
+
mongoid (4.0.0)
|
18
|
+
activemodel (~> 4.0.0.beta)
|
19
|
+
moped (~> 1.4.2)
|
20
|
+
origin (~> 1.0)
|
21
|
+
tzinfo (~> 0.3.22)
|
22
|
+
|
23
|
+
GIT
|
24
|
+
remote: git://github.com/rails/rails.git
|
25
|
+
revision: b359c5db9f0e7dfb2cbf34362a9b310cf5aadd3a
|
26
|
+
specs:
|
27
|
+
actionmailer (4.0.0.beta1)
|
28
|
+
actionpack (= 4.0.0.beta1)
|
29
|
+
mail (~> 2.5.3)
|
30
|
+
actionpack (4.0.0.beta1)
|
31
|
+
activesupport (= 4.0.0.beta1)
|
32
|
+
builder (~> 3.1.0)
|
33
|
+
erubis (~> 2.7.0)
|
34
|
+
rack (~> 1.5.2)
|
35
|
+
rack-test (~> 0.6.2)
|
36
|
+
activemodel (4.0.0.beta1)
|
37
|
+
activesupport (= 4.0.0.beta1)
|
38
|
+
builder (~> 3.1.0)
|
39
|
+
activerecord (4.0.0.beta1)
|
40
|
+
activemodel (= 4.0.0.beta1)
|
41
|
+
activerecord-deprecated_finders (~> 0.0.3)
|
42
|
+
activesupport (= 4.0.0.beta1)
|
43
|
+
arel (~> 4.0.0.beta1)
|
44
|
+
activesupport (4.0.0.beta1)
|
45
|
+
i18n (~> 0.6, >= 0.6.4)
|
46
|
+
minitest (~> 4.2)
|
47
|
+
multi_json (~> 1.3)
|
48
|
+
thread_safe (~> 0.1)
|
49
|
+
tzinfo (~> 0.3.33)
|
50
|
+
rails (4.0.0.beta1)
|
51
|
+
actionmailer (= 4.0.0.beta1)
|
52
|
+
actionpack (= 4.0.0.beta1)
|
53
|
+
activerecord (= 4.0.0.beta1)
|
54
|
+
activesupport (= 4.0.0.beta1)
|
55
|
+
bundler (>= 1.3.0, < 2.0)
|
56
|
+
railties (= 4.0.0.beta1)
|
57
|
+
sprockets-rails (~> 2.0.0.rc3)
|
58
|
+
railties (4.0.0.beta1)
|
59
|
+
actionpack (= 4.0.0.beta1)
|
60
|
+
activesupport (= 4.0.0.beta1)
|
61
|
+
rake (>= 0.8.7)
|
62
|
+
rdoc (~> 3.4)
|
63
|
+
thor (>= 0.17.0, < 2.0)
|
64
|
+
|
65
|
+
GEM
|
66
|
+
remote: http://rubygems.org/
|
67
|
+
specs:
|
68
|
+
activerecord-deprecated_finders (0.0.3)
|
69
|
+
arel (4.0.0.beta1)
|
70
|
+
atomic (1.0.1)
|
71
|
+
bourne (1.1.2)
|
72
|
+
mocha (= 0.10.5)
|
73
|
+
builder (3.1.4)
|
74
|
+
chronic (0.9.1)
|
75
|
+
colorize (0.5.8)
|
76
|
+
concerned (0.1.5)
|
77
|
+
cutter (0.8.9)
|
78
|
+
colorize (>= 0.5)
|
79
|
+
database_cleaner (0.9.1)
|
80
|
+
delorean (1.1.1)
|
81
|
+
chronic
|
82
|
+
diff-lcs (1.2.1)
|
83
|
+
erubis (2.7.0)
|
84
|
+
factory_girl (4.2.0)
|
85
|
+
activesupport (>= 3.0.0)
|
86
|
+
fakeweb (1.3.0)
|
87
|
+
ffaker (1.15.0)
|
88
|
+
geocoder (1.1.6)
|
89
|
+
git (1.2.5)
|
90
|
+
hashie (1.2.0)
|
91
|
+
hike (1.2.1)
|
92
|
+
i18n (0.6.4)
|
93
|
+
jeweler (1.8.4)
|
94
|
+
bundler (~> 1.0)
|
95
|
+
git (>= 1.2.5)
|
96
|
+
rake
|
97
|
+
rdoc
|
98
|
+
json (1.7.7)
|
99
|
+
mail (2.5.3)
|
100
|
+
i18n (>= 0.4.0)
|
101
|
+
mime-types (~> 1.16)
|
102
|
+
treetop (~> 1.4.8)
|
103
|
+
metaclass (0.0.1)
|
104
|
+
mime-types (1.21)
|
105
|
+
minitest (4.6.2)
|
106
|
+
mocha (0.10.5)
|
107
|
+
metaclass (~> 0.0.1)
|
108
|
+
mongoid_geospatial (2.7.2)
|
109
|
+
activesupport (>= 3.2)
|
110
|
+
mongoid (>= 3.0.0)
|
111
|
+
mongoid_indexing (0.1.2)
|
112
|
+
mongoid (>= 3)
|
113
|
+
moped (>= 1.4)
|
114
|
+
rails (>= 3.1)
|
115
|
+
moped (1.4.3)
|
116
|
+
multi_json (1.6.1)
|
117
|
+
origin (1.0.11)
|
118
|
+
polyglot (0.3.3)
|
119
|
+
rack (1.5.2)
|
120
|
+
rack-test (0.6.2)
|
121
|
+
rack (>= 1.0)
|
122
|
+
rake (10.0.3)
|
123
|
+
rdoc (3.12.2)
|
124
|
+
json (~> 1.4)
|
125
|
+
rspec (2.13.0)
|
126
|
+
rspec-core (~> 2.13.0)
|
127
|
+
rspec-expectations (~> 2.13.0)
|
128
|
+
rspec-mocks (~> 2.13.0)
|
129
|
+
rspec-core (2.13.0)
|
130
|
+
rspec-expectations (2.13.0)
|
131
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
132
|
+
rspec-mocks (2.13.0)
|
133
|
+
shoulda (3.3.2)
|
134
|
+
shoulda-context (~> 1.0.1)
|
135
|
+
shoulda-matchers (~> 1.4.1)
|
136
|
+
shoulda-context (1.0.2)
|
137
|
+
shoulda-matchers (1.4.2)
|
138
|
+
activesupport (>= 3.0.0)
|
139
|
+
bourne (~> 1.1.2)
|
140
|
+
simplecov (0.7.1)
|
141
|
+
multi_json (~> 1.0)
|
142
|
+
simplecov-html (~> 0.7.1)
|
143
|
+
simplecov-html (0.7.1)
|
144
|
+
sprockets (2.9.0)
|
145
|
+
hike (~> 1.2)
|
146
|
+
multi_json (~> 1.0)
|
147
|
+
rack (~> 1.0)
|
148
|
+
tilt (~> 1.1, != 1.3.0)
|
149
|
+
sprockets-rails (2.0.0.rc3)
|
150
|
+
actionpack (>= 3.0)
|
151
|
+
activesupport (>= 3.0)
|
152
|
+
sprockets (~> 2.8)
|
153
|
+
thor (0.17.0)
|
154
|
+
thread_safe (0.1.0)
|
155
|
+
atomic
|
156
|
+
tilt (1.3.4)
|
157
|
+
treetop (1.4.12)
|
158
|
+
polyglot
|
159
|
+
polyglot (>= 0.3.1)
|
160
|
+
tzinfo (0.3.36)
|
161
|
+
|
162
|
+
PLATFORMS
|
163
|
+
ruby
|
164
|
+
|
165
|
+
DEPENDENCIES
|
166
|
+
bundler (>= 1.1.0)
|
167
|
+
concerned (~> 0.1.3)
|
168
|
+
cutter (~> 0.8.2)
|
169
|
+
database_cleaner (>= 0.8)
|
170
|
+
delorean (~> 1.1.1)
|
171
|
+
factory_girl
|
172
|
+
fakeweb
|
173
|
+
ffaker (>= 1.14)
|
174
|
+
geocoder
|
175
|
+
gmaps4rails (>= 2.0.0.pre)!
|
176
|
+
hashie (~> 1.2)
|
177
|
+
jeweler (>= 1.8.4)
|
178
|
+
mongoid (>= 4)!
|
179
|
+
mongoid_geospatial
|
180
|
+
mongoid_indexing (~> 0.1.2)
|
181
|
+
rails (>= 4.0.0.beta1)!
|
182
|
+
rdoc (>= 3.1)
|
183
|
+
rspec (>= 2.12.0)
|
184
|
+
shoulda
|
185
|
+
shoulda-matchers
|
186
|
+
simplecov (>= 0.5)
|
187
|
+
sugar-high (~> 0.7.3)!
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Kristian Mandrup
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# On The Map
|
2
|
+
|
3
|
+
Mongoid concerns for putting models "on the map".
|
4
|
+
|
5
|
+
Contains various useful concern modules for:
|
6
|
+
|
7
|
+
* position
|
8
|
+
* address
|
9
|
+
* geo-coding
|
10
|
+
* placing model on a map
|
11
|
+
* geo spatial indexing of position
|
12
|
+
* geo spatial searching on position
|
13
|
+
|
14
|
+
* Adressable
|
15
|
+
|
16
|
+
### Adressable
|
17
|
+
|
18
|
+
* embeds an address on the model
|
19
|
+
* adds delegate methods to all embedded address fields
|
20
|
+
* adds `full_address` method that returns full adress from concatenation of all fields
|
21
|
+
|
22
|
+
### GeoLocatable
|
23
|
+
|
24
|
+
* includes `addressable` and `positionable` concerns
|
25
|
+
* performs geocoding after an address is created or updated
|
26
|
+
* adds `latitude` and `longitude` methods to model
|
27
|
+
|
28
|
+
### Mappable
|
29
|
+
|
30
|
+
* include `Gmaps4rails::ActsAsGmappable`
|
31
|
+
* adds field `normalized_address` to store address calculated and returned by Google Maps geo-coding
|
32
|
+
* adds method `gmaps4rails_address` which returns main adress for use in geo-coding
|
33
|
+
|
34
|
+
### Positionable
|
35
|
+
|
36
|
+
* includes `Mongoid::Geospatial` into the model to make GeoSpatial helpers available directly on the model
|
37
|
+
* Adds geo_field `position` (macro from Mongoid GeoSpatial)
|
38
|
+
* adds spatial indexing for position field
|
39
|
+
|
40
|
+
* positon field is indexed and used in geo-searches (fx find points near a point)
|
41
|
+
|
42
|
+
## Usage
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
include_concerns :addressable, from: :on_the_map
|
46
|
+
include_concerns :mappable, from: :on_the_map
|
47
|
+
include_concerns :positionable, from: :on_the_map
|
48
|
+
|
49
|
+
include_concerns :geo_locatable, from: :on_the_map
|
50
|
+
```
|
51
|
+
|
52
|
+
See the specs for examples on how to use these concerns.
|
53
|
+
|
54
|
+
Note: There is room for improvement ;)
|
55
|
+
|
56
|
+
## Contributing to on_the_map
|
57
|
+
|
58
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
59
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
60
|
+
* Fork the project.
|
61
|
+
* Start a feature/bugfix branch.
|
62
|
+
* Commit and push until you are happy with your contribution.
|
63
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
64
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
65
|
+
|
66
|
+
## Copyright
|
67
|
+
|
68
|
+
Copyright (c) 2013 Kristian Mandrup. See LICENSE.txt for
|
69
|
+
further details.
|
70
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "on_the_map"
|
18
|
+
gem.homepage = "http://github.com/kristianmandrup/on_the_map"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Pre-baked Mongoid concerns to put models on the map}
|
21
|
+
gem.description = %Q{Makes it easy to add functionality to models related to geocoding, addressing and placing them as pins on a map}
|
22
|
+
gem.email = "kmandrup@gmail.com"
|
23
|
+
gem.authors = ["Kristian Mandrup"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rdoc/task'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "on_the_map #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/address.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
class Address
|
2
|
+
# include BasicDocument
|
3
|
+
include Mongoid::Document
|
4
|
+
include Mongoid::Dirty
|
5
|
+
|
6
|
+
after_update :perform_geocoding
|
7
|
+
|
8
|
+
def self.geo_address_fields
|
9
|
+
%w{city state state_code province province_code postal_code country country_code}
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.all_geo_address_fields
|
13
|
+
geo_address_fields + %w{latitude longitude}
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.address_fields
|
17
|
+
%w{street number floor_adr} + geo_address_fields
|
18
|
+
end
|
19
|
+
|
20
|
+
address_fields.each do |name|
|
21
|
+
field name, type: String
|
22
|
+
end
|
23
|
+
|
24
|
+
field :full, type: String, default: ""
|
25
|
+
|
26
|
+
embedded_in :addressable, polymorphic: true
|
27
|
+
|
28
|
+
# validates :street, presence: true
|
29
|
+
|
30
|
+
before_create :set_defaults
|
31
|
+
|
32
|
+
def floor
|
33
|
+
floor_adr.to_i
|
34
|
+
end
|
35
|
+
|
36
|
+
def region
|
37
|
+
state || province
|
38
|
+
end
|
39
|
+
|
40
|
+
def xtra_fields
|
41
|
+
[:floor_adr]
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_s
|
45
|
+
Address.address_fields.inject("") do |res, name|
|
46
|
+
res << "#{name}: #{send(name)}\n"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def geolocatable?
|
51
|
+
!full.blank?
|
52
|
+
end
|
53
|
+
|
54
|
+
def perform_geocoding
|
55
|
+
return unless geolocatable?
|
56
|
+
addressable.perform_geocoding if addressable.respond_to? :perform_geocoding
|
57
|
+
end
|
58
|
+
alias_method :perform_geocoding!, :perform_geocoding
|
59
|
+
|
60
|
+
protected
|
61
|
+
|
62
|
+
def set_defaults
|
63
|
+
write_attribute :country_code, 'DK'
|
64
|
+
end
|
65
|
+
|
66
|
+
def set_full
|
67
|
+
self.full = Address.address_fields.select do |name|
|
68
|
+
!send(name).blank?
|
69
|
+
end.map{|name| send(name) }.join(', ')
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'address'
|
2
|
+
|
3
|
+
module OnTheMap
|
4
|
+
module Addressable
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
embeds_one :address, as: :addressable
|
9
|
+
|
10
|
+
Address.address_fields.each do |fname|
|
11
|
+
delegate fname, to: :address
|
12
|
+
|
13
|
+
meth_name = "#{fname}="
|
14
|
+
|
15
|
+
define_method meth_name do |value|
|
16
|
+
# create new empty address if none defined
|
17
|
+
self.address ||= Address.new
|
18
|
+
|
19
|
+
unless value.to_s.strip.blank?
|
20
|
+
# set address field
|
21
|
+
self.address.send(meth_name, value)
|
22
|
+
|
23
|
+
# update full address
|
24
|
+
self.address.send :set_full
|
25
|
+
|
26
|
+
perform_geocoding if perform_geocoding? fname
|
27
|
+
|
28
|
+
address.save!
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
delegate :region, to: :address
|
34
|
+
end
|
35
|
+
|
36
|
+
def perform_geocoding? name
|
37
|
+
return unless respond_to? :perform_geocoding
|
38
|
+
geocode_field?(name)
|
39
|
+
end
|
40
|
+
|
41
|
+
def set_address hash
|
42
|
+
adr = Hashie::Mash.new hash
|
43
|
+
Address.address_fields.each do |name|
|
44
|
+
self.send("#{name}=", adr.send(name)) if adr.send(name)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def full_address
|
49
|
+
self.address.full
|
50
|
+
end
|
51
|
+
|
52
|
+
def floor_adr= adr
|
53
|
+
self.address.floor_adr = adr if address
|
54
|
+
end
|
55
|
+
|
56
|
+
protected
|
57
|
+
|
58
|
+
def geocode_field? name
|
59
|
+
[:city, :region].include? name.to_sym
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'on_the_map/addressable'
|
2
|
+
require 'on_the_map/positionable'
|
3
|
+
|
4
|
+
module OnTheMap
|
5
|
+
module GeoLocatable
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
include_concerns :addressable, :positionable, from: 'OnTheMap'
|
10
|
+
|
11
|
+
include Geocoder::Model::Mongoid
|
12
|
+
|
13
|
+
after_create :perform_geocoding
|
14
|
+
after_update :perform_geocoding
|
15
|
+
|
16
|
+
field :geocoded, type: Boolean, default: false
|
17
|
+
alias_method :geocoded?, :geocoded
|
18
|
+
|
19
|
+
geocoded_by :full_address, coordinates: :position, skip_index: true do |obj,results|
|
20
|
+
if geo = results.first
|
21
|
+
if geo.latitude && geo.longitude
|
22
|
+
obj.send(:geocoding_started!)
|
23
|
+
|
24
|
+
Address.geo_address_fields.each do |fname|
|
25
|
+
geo_value = geo.send(fname)
|
26
|
+
obj.send("#{fname}=", geo_value) unless geo_value.blank?
|
27
|
+
end
|
28
|
+
|
29
|
+
obj.position = [geo.longitude, geo.latitude]
|
30
|
+
|
31
|
+
obj.geocoded = true
|
32
|
+
|
33
|
+
if obj.respond_to? :gmaps
|
34
|
+
obj.gmaps = true
|
35
|
+
end
|
36
|
+
|
37
|
+
obj.send(:geocoding_done!)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# after_validation :geocode
|
43
|
+
|
44
|
+
# reverse_geocoded_by :latitude, :longitude #, :address => :full_address
|
45
|
+
|
46
|
+
# FROM: https://github.com/alexreisner/geocoder
|
47
|
+
|
48
|
+
# reverse_geocoded_by :coordinates
|
49
|
+
# after_validation :reverse_geocode # auto-fetch address
|
50
|
+
# Once you've set up your model you'll need to create the necessary spatial indices in your database:
|
51
|
+
|
52
|
+
# rake db:mongoid:create_indexes
|
53
|
+
# Be sure to read Latitude/Longitude Order in the Notes on MongoDB section below on how to properly retrieve latitude/longitude coordinates from your objects.
|
54
|
+
end
|
55
|
+
|
56
|
+
def latitude
|
57
|
+
perform_geocoding unless position
|
58
|
+
position[1] if position
|
59
|
+
end
|
60
|
+
alias_method :lat, :latitude
|
61
|
+
|
62
|
+
def longitude
|
63
|
+
perform_geocoding unless position
|
64
|
+
position[0] if position
|
65
|
+
end
|
66
|
+
alias_method :lng, :longitude
|
67
|
+
|
68
|
+
def geocodable?
|
69
|
+
!position && geolocatable? || address.changed?
|
70
|
+
end
|
71
|
+
|
72
|
+
def geolocatable?
|
73
|
+
address && address.geolocatable?
|
74
|
+
end
|
75
|
+
|
76
|
+
def perform_geocoding
|
77
|
+
geocode if geocodable? && !geocoding?
|
78
|
+
end
|
79
|
+
alias_method :perform_geocoding!, :perform_geocoding
|
80
|
+
|
81
|
+
def geocoding?
|
82
|
+
@geocoding_started == true
|
83
|
+
end
|
84
|
+
|
85
|
+
protected
|
86
|
+
|
87
|
+
def geocoding_started!
|
88
|
+
@geocoding_started = true
|
89
|
+
end
|
90
|
+
|
91
|
+
def geocoding_done!
|
92
|
+
@geocoding_started = false
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module OnTheMap
|
2
|
+
module Mappable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
include ::Gmaps4rails::ActsAsGmappable
|
7
|
+
|
8
|
+
include_concerns :geo_locatable, :positionable, from: 'OnTheMap'
|
9
|
+
|
10
|
+
# see https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Model-Customization
|
11
|
+
# https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Mongo%C3%AFd
|
12
|
+
acts_as_gmappable position: :position,
|
13
|
+
process_geocoding: :gmaps_geocode?, #
|
14
|
+
address:"gmaps4rails_address", normalized_address: "normalized_address",
|
15
|
+
msg: "Sorry, not even Google could figure out where that is"
|
16
|
+
|
17
|
+
field :gmaps, type: Boolean
|
18
|
+
|
19
|
+
field :normalized_address # used to save address matched by Google
|
20
|
+
end
|
21
|
+
|
22
|
+
# When Geo Coding is to be performed by Gmaps4rails
|
23
|
+
# Leave it up to geocoder!
|
24
|
+
def gmaps_geocode?
|
25
|
+
# has_address? && (has_position? || has_geo_coords?) &&
|
26
|
+
false # (!address.blank? && (lat.blank? || lng.blank?)) || position_changed?
|
27
|
+
end
|
28
|
+
|
29
|
+
def has_geo_coords?
|
30
|
+
respond_to?(:latitude) && latitude && respond_to?(:longitude) && longitude
|
31
|
+
end
|
32
|
+
|
33
|
+
def has_position?
|
34
|
+
respond_to?(:position) && position
|
35
|
+
end
|
36
|
+
|
37
|
+
def has_address?
|
38
|
+
respond_to?(:address) && address
|
39
|
+
end
|
40
|
+
|
41
|
+
#describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki
|
42
|
+
def gmaps4rails_address
|
43
|
+
return address_str if respond_to? :address_str # Use GeoLocatable if such exists
|
44
|
+
"#{address.street}, #{address.city}, #{address.country}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'mongoid_geospatial'
|
2
|
+
|
3
|
+
module OnTheMap
|
4
|
+
module Positionable
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
include Mongoid::Geospatial
|
9
|
+
|
10
|
+
geo_field :position, :delegate => true
|
11
|
+
|
12
|
+
spatial_index :position
|
13
|
+
spatial_scope :position
|
14
|
+
end
|
15
|
+
|
16
|
+
def configure_positionable
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def longitude= x
|
22
|
+
position.x = x
|
23
|
+
end
|
24
|
+
|
25
|
+
def latitude= y
|
26
|
+
position.y = y
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/on_the_map.rb
ADDED
File without changes
|