neighborhood 0.0.1 → 0.1.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 +4 -4
- data/README.md +76 -12
- data/lib/generators/neighborhood_generator.rb +49 -0
- data/lib/generators/templates/.keep +0 -0
- data/lib/neighborhood.rb +5 -1
- data/lib/neighborhood/in_the_neighborhood.rb +36 -0
- data/lib/neighborhood/version.rb +1 -1
- data/neighborhood.gemspec +2 -0
- metadata +19 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9d37b2adbaa206a5d585a5e05e2754cca94ab66
|
4
|
+
data.tar.gz: 6c1047694cd6649fa98d6682ed78f429a46edcd2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb33500ab95e932430dbab64c1d43a1325ecf15c5ae5220c47cf6fda823a5c583ade44bfa0485314f3a1860747792d172fa0b35b13ae8a7fcb94f8b0448b9d2d
|
7
|
+
data.tar.gz: db438c434e5e967b9924d559d18d2ebe02dcc0c22213486d2c0b231091575cc97386b1c822d9166c32aabc2ee31bd9b08b64a214c5da7c3d9dacacbdfaf2940c
|
data/README.md
CHANGED
@@ -1,28 +1,92 @@
|
|
1
|
-
|
1
|
+
Neighborhood
|
2
|
+
==========
|
2
3
|
|
3
|
-
|
4
|
+
Neighborhood helps you easily add address fields and geocoding to your Rails
|
5
|
+
models.
|
4
6
|
|
5
|
-
|
7
|
+
It makes use of the [Geocoder gem](https://github.com/alexreisner/geocoder) for
|
8
|
+
all geocoding, although it handles some logic behind-the-scenes.
|
6
9
|
|
7
|
-
|
10
|
+
Installation
|
11
|
+
----------
|
12
|
+
|
13
|
+
This gem is meant to work with Ruby on Rails projects. I recommend you add it
|
14
|
+
to your project's Gemfile:
|
8
15
|
|
9
16
|
```ruby
|
10
|
-
gem 'neighborhood'
|
17
|
+
gem 'neighborhood', '~> 0.1.0'
|
18
|
+
```
|
19
|
+
|
20
|
+
> **WARNING: Neighborhood will likely undergo major changes as I add more
|
21
|
+
> features over time. I highly recommend you limit the version in your
|
22
|
+
> `Gemfile` when installing Neighborhood (as shown above).**
|
23
|
+
|
24
|
+
Then install your bundle:
|
25
|
+
|
26
|
+
```text
|
27
|
+
$ bundle
|
28
|
+
```
|
29
|
+
|
30
|
+
Usage
|
31
|
+
----------
|
32
|
+
|
33
|
+
### Adding Fields to a Model
|
34
|
+
|
35
|
+
To add address fields to your model, run the generator:
|
36
|
+
|
37
|
+
```text
|
38
|
+
$ bundle exec rails g neighborhood [Model]
|
11
39
|
```
|
12
40
|
|
13
|
-
|
41
|
+
This adds the following fields:
|
42
|
+
|
43
|
+
* `street_address`
|
44
|
+
* `suite_apt`
|
45
|
+
* `city`
|
46
|
+
* `state`
|
47
|
+
* `zip`
|
48
|
+
* `country`
|
49
|
+
* `phone`
|
50
|
+
* `lat`
|
51
|
+
* `lng`
|
52
|
+
* `full_address`
|
53
|
+
|
54
|
+
Then migrate your database:
|
55
|
+
|
56
|
+
```text
|
57
|
+
$ bundle exec rake db:migrate
|
58
|
+
```
|
14
59
|
|
15
|
-
|
60
|
+
### Add Auto-Geocoding
|
61
|
+
|
62
|
+
To add automatic geocoding, add the `in_the_neighborhood` method to the model
|
63
|
+
to which you added the fields. For example, if you added these fields to a
|
64
|
+
`User` model, it might look like this:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
class User < ActiveRecord::Base
|
68
|
+
in_the_neighborhood
|
69
|
+
end
|
70
|
+
```
|
16
71
|
|
17
|
-
|
72
|
+
> **WARNING: Neighborhood's `in_the_neighborhood` is tightly wound to the
|
73
|
+
> address fields. You need to make sure you've run the generator (and migrated)
|
74
|
+
> before you can mess with this method.
|
18
75
|
|
19
|
-
|
76
|
+
This method add an `after_validation` callback that will geocode the *address*
|
77
|
+
on the model. `address` is an instance method made available to you which
|
78
|
+
combines the individual address fields into one field.
|
20
79
|
|
21
|
-
|
80
|
+
When geocoding, Neighborhood automatically sets `lat`, `lng`, `full_address`
|
81
|
+
and `country`. Of course, you can still set country automatically, it just may
|
82
|
+
be overwritten.
|
22
83
|
|
23
|
-
|
84
|
+
> *Note: I know there's a lot missing here. I'm simply adding features and
|
85
|
+
> options as I need them. Feel free to contribute if you see something missing
|
86
|
+
> that you need.*
|
24
87
|
|
25
|
-
|
88
|
+
Contributing
|
89
|
+
----------
|
26
90
|
|
27
91
|
1. Fork it ( https://github.com/[my-github-username]/neighborhood/fork )
|
28
92
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rails/generators'
|
3
|
+
|
4
|
+
class NeighborhoodGenerator < Rails::Generators::Base
|
5
|
+
desc "Add address fields and geocoding to a model"
|
6
|
+
|
7
|
+
source_root File.expand_path('../templates', __FILE__)
|
8
|
+
|
9
|
+
argument :model, :type => :string
|
10
|
+
|
11
|
+
def confirm_model
|
12
|
+
@model = model.constantize
|
13
|
+
rescue
|
14
|
+
puts "Can't find the model: #{model}"
|
15
|
+
exit
|
16
|
+
end
|
17
|
+
|
18
|
+
def set_references
|
19
|
+
@model_pl = @model.to_s.humanize.downcase.pluralize
|
20
|
+
end
|
21
|
+
|
22
|
+
def generate_migration
|
23
|
+
migration_name = "add_neighborhood_fields_to_#{@model_pl}"
|
24
|
+
generate "migration #{migration_name} #{database_fields}"
|
25
|
+
insert_into_file(
|
26
|
+
Dir.glob("#{Rails.root}/db/migrate/*.rb").last,
|
27
|
+
", :precision => 7, :scale => 5",
|
28
|
+
:after => /\:l(at|ng), \:decimal/
|
29
|
+
)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def database_fields
|
35
|
+
[
|
36
|
+
'street_address',
|
37
|
+
'suite_apt',
|
38
|
+
'city',
|
39
|
+
'state',
|
40
|
+
'zip',
|
41
|
+
'country',
|
42
|
+
'phone',
|
43
|
+
'lat:decimal',
|
44
|
+
'lng:decimal',
|
45
|
+
'full_address:text',
|
46
|
+
].join(' ')
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
File without changes
|
data/lib/neighborhood.rb
CHANGED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'geocoder'
|
2
|
+
|
3
|
+
class << ActiveRecord::Base
|
4
|
+
|
5
|
+
def in_the_neighborhood
|
6
|
+
geocoded_by :address, :latitude => :lat, :longitude => :lng
|
7
|
+
|
8
|
+
after_validation :geocode_if_address
|
9
|
+
|
10
|
+
define_method "address" do
|
11
|
+
[street_address, suite_apt, city, state, zip, country].compact.join(', ')
|
12
|
+
end
|
13
|
+
|
14
|
+
define_method "geocode_if_address" do
|
15
|
+
if address.present?
|
16
|
+
geo = Geocoder.search(address)
|
17
|
+
if geo.size > 0
|
18
|
+
geo = geo.first.data
|
19
|
+
# set basic geo
|
20
|
+
attrs = {
|
21
|
+
:lat => geo['geometry']['location']['lat'],
|
22
|
+
:lng => geo['geometry']['location']['lng'],
|
23
|
+
:full_address => geo['formatted_address'],
|
24
|
+
}
|
25
|
+
# set country if necessary
|
26
|
+
country = geo['address_components']
|
27
|
+
.select { |a| a['types'].include?('country') }.first['long_name']
|
28
|
+
attrs[:country] = country if country.present?
|
29
|
+
# save attributes
|
30
|
+
update_columns(attrs)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/lib/neighborhood/version.rb
CHANGED
data/neighborhood.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: neighborhood
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sean C Davis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: geocoder
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
description: ''
|
42
56
|
email:
|
43
57
|
- scdavis41@gmail.com
|
@@ -50,7 +64,10 @@ files:
|
|
50
64
|
- LICENSE.md
|
51
65
|
- README.md
|
52
66
|
- Rakefile
|
67
|
+
- lib/generators/neighborhood_generator.rb
|
68
|
+
- lib/generators/templates/.keep
|
53
69
|
- lib/neighborhood.rb
|
70
|
+
- lib/neighborhood/in_the_neighborhood.rb
|
54
71
|
- lib/neighborhood/version.rb
|
55
72
|
- neighborhood.gemspec
|
56
73
|
homepage: ''
|