my_zipcode_gem 0.2.0 → 0.2.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/{README.textile → README.md} +55 -42
- data/lib/generators/my_zipcode_gem/models_generator.rb +0 -1
- data/lib/generators/my_zipcode_gem/templates/county_model.rb +8 -8
- data/lib/generators/my_zipcode_gem/templates/state_model.rb +4 -5
- data/lib/generators/my_zipcode_gem/templates/zipcode_model.rb +8 -8
- data/lib/generators/my_zipcode_gem/templates/zipcodes.rake +1 -0
- data/lib/generators/my_zipcode_gem/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa3086991a379075e85a57c7165f3435dd2b193c
|
4
|
+
data.tar.gz: 73ca08c0d39a99b4da16cde40c2772d1a6098e7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 744078d00f5ae40c44241ac57784dbf1602a372532a125a5af6998ebb3a5b36b07d0796f7b62cc9a30db29fc19666d3482c8badb89542fab3387af7b2d78ff82
|
7
|
+
data.tar.gz: b77952605f4e0ed337ec44ee920e061b513334dd488f282ff41344e1fe6573f93cf56e05b1bad5cfd42ad07ba2634446206589c7c8270a5cdb588836d32b68f1
|
@@ -1,44 +1,54 @@
|
|
1
|
-
|
1
|
+
# My Zipcode Gem
|
2
2
|
|
3
|
-
!https://img.shields.io/github/license/midwire/my_zipcode_gem.svg
|
3
|
+
[](https://github.com/midwire/my_zipcode_gem/blob/master/MIT-LICENSE)
|
4
4
|
|
5
|
-
Simple gem to handle zipcode lookups and related functionality.
|
5
|
+
Simple gem to handle zipcode lookups and related functionality. `rake zipcodes:update` will automatically download and update your local zipcode database. Generates three models for extended zipcode functionality.
|
6
6
|
|
7
|
-
|
7
|
+
## Versions
|
8
|
+
|
9
|
+
* 0.1.x is for Rails versions less than 4.x
|
10
|
+
* 0.2.x is for Rails 4 and above
|
11
|
+
|
12
|
+
## Installation
|
8
13
|
|
9
14
|
Add the following line to your Gemfile:
|
10
15
|
|
11
|
-
|
16
|
+
gem 'my_zipcode_gem'
|
12
17
|
|
13
18
|
Run:
|
14
19
|
|
15
|
-
|
20
|
+
rake bundle install
|
16
21
|
|
17
22
|
Generate the models and populate the data:
|
18
23
|
|
19
|
-
|
24
|
+
```
|
25
|
+
rails g my_zipcode_gem:models
|
20
26
|
rake db:migrate
|
21
27
|
rake zipcodes:update
|
28
|
+
```
|
22
29
|
|
23
30
|
You should now have three new tables and three new models, Zipcode, State, County.
|
24
31
|
|
25
|
-
|
32
|
+
## Usage
|
26
33
|
|
27
|
-
|
34
|
+
```
|
35
|
+
zipcode = Zipcode.find_by_code '66206'
|
28
36
|
zipcode.state.abbr # => 'KS'
|
29
37
|
zipcode.city # => 'Shawnee Mission'
|
30
38
|
zipcode.county.name # => 'Johnson'
|
31
39
|
zipcode.lat.to_s # => '38.959356', it is actually a BigDecimal object converted to_s for documentation.
|
32
40
|
zipcode.lon.to_s # => '-94.716155', ditto
|
33
|
-
zipcode.
|
41
|
+
zipcode.geocoded? # => true, most if not all should be pre-geocoded.
|
42
|
+
```
|
34
43
|
|
35
44
|
You can also look for a zipcode from a city and state:
|
36
45
|
|
37
|
-
|
46
|
+
Zipcode.find_by_city_state "Shawnee Mission", "KS"
|
38
47
|
|
39
48
|
You can use State and County objects as follows:
|
40
49
|
|
41
|
-
|
50
|
+
```
|
51
|
+
state = State.find_by_abbr "MO"
|
42
52
|
state.cities.count # => 963
|
43
53
|
state.cities # gives you a sorted array of all cities for the state
|
44
54
|
state.zipcodes.count # => 1195
|
@@ -47,45 +57,48 @@ county = state.counties.first
|
|
47
57
|
county.cities.count # => 5
|
48
58
|
county.cities # gives you a sorted array of all cities for the county
|
49
59
|
county.zipcodes.count # => 5
|
60
|
+
```
|
50
61
|
|
51
|
-
|
62
|
+
### Automatic JQuery/AJAX lookup
|
52
63
|
|
53
64
|
You can have a user enter a zipcode and automatically lookup their city, state and county.
|
54
65
|
|
55
66
|
Put something like this in your view:
|
56
67
|
|
57
|
-
|
68
|
+
```
|
69
|
+
f.text_field :zip, :size => 5, :maxlength => 5, :class => 'zipcode_interactive'
|
58
70
|
f.text_field :city, :size => 20, :maxlength => 60, :readonly => true
|
59
71
|
f.text_field(:state, :size => 2, :maxlength => 2, :readonly => true)
|
60
72
|
f.text_field(:county, :size => 20, :maxlength => 60, :readonly => true)
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
})
|
73
|
+
```
|
74
|
+
|
75
|
+
Then add this to your application.js, but remember to replace `mycontrollername` with your own controller.
|
76
|
+
|
77
|
+
$(document).ready(function() {
|
78
|
+
// Interactive Zipcodes
|
79
|
+
$('input.zipcode_interactive').blur(function(data) {
|
80
|
+
var elem_id = $(this).attr("id");
|
81
|
+
var base_id = elem_id.substring(0, elem_id.lastIndexOf("_"));
|
82
|
+
$.get("/mycontrollername/get_zip_data/" + this.value, {},
|
83
|
+
function(data) {
|
84
|
+
var zipcode = $.parseJSON(data);
|
85
|
+
var city = $('#' + base_id + '_city');
|
86
|
+
var state = $('#' + base_id + '_state');
|
87
|
+
var county = $('#' + base_id + '_county');
|
88
|
+
if (zipcode.err) {
|
89
|
+
alert(zipcode.err);
|
90
|
+
} else {
|
91
|
+
city.val(zipcode.city);
|
92
|
+
state.val(zipcode.state)
|
93
|
+
county.val(zipcode.county)
|
94
|
+
}
|
95
|
+
})
|
96
|
+
});
|
84
97
|
});
|
85
98
|
|
86
99
|
You will also need a controller method similar to this, which will return the data to your form:
|
87
100
|
|
88
|
-
|
101
|
+
def get_zip_data
|
89
102
|
@zipcode = Zipcode.find_by_code(params[:code], :include => [:county, :state])
|
90
103
|
if @zipcode
|
91
104
|
@counties = County.find(:all, :conditions => [ "state_id = ?", @zipcode.county.state_id ])
|
@@ -115,14 +128,14 @@ bc. def get_zip_data
|
|
115
128
|
|
116
129
|
And define a route for the AJAX function in routes.rb:
|
117
130
|
|
118
|
-
|
131
|
+
get 'mycontrollername/get_zip_data/:code', :controller => 'mycontrollername', :action => 'get_zip_data'
|
119
132
|
|
120
133
|
That's about it.
|
121
134
|
|
122
|
-
|
135
|
+
Please report any errors or [issues](https://github.com/midwire/my_zipcode_gem/issues).
|
123
136
|
|
124
|
-
|
137
|
+
## LOG
|
125
138
|
|
126
|
-
|
139
|
+
### 05/03/2011:
|
127
140
|
|
128
141
|
Initial Release
|
@@ -1,16 +1,16 @@
|
|
1
1
|
require 'memoist'
|
2
|
+
|
2
3
|
class County < ActiveRecord::Base
|
3
4
|
extend Memoist
|
4
|
-
attr_accessible :state_id, :region_id, :abbr, :name, :count_seat
|
5
|
-
|
6
5
|
belongs_to :state
|
7
6
|
has_many :zipcodes
|
8
|
-
|
9
|
-
validates :name, :
|
10
|
-
|
11
|
-
|
12
|
-
scope :
|
13
|
-
|
7
|
+
|
8
|
+
validates :name, uniqueness: { scope: :state_id, case_sensitive: false },
|
9
|
+
presence: true
|
10
|
+
|
11
|
+
scope :without_zipcodes, -> { joins("LEFT JOIN zipcodes ON zipcodes.county_id = counties.id").where("zipcodes.county_id IS NULL") }
|
12
|
+
scope :without_state, -> { where("state_id IS NULL") }
|
13
|
+
|
14
14
|
def cities
|
15
15
|
zipcodes.map(&:city).sort.uniq
|
16
16
|
end
|
@@ -1,14 +1,13 @@
|
|
1
1
|
require 'memoist'
|
2
|
+
|
2
3
|
class State < ActiveRecord::Base
|
3
4
|
extend Memoist
|
4
|
-
attr_accessible :abbr, :name
|
5
|
-
|
6
5
|
has_many :zipcodes
|
7
6
|
has_many :counties
|
8
7
|
|
9
|
-
validates :abbr, :
|
10
|
-
validates :name, :
|
11
|
-
|
8
|
+
validates :abbr, uniqueness: { case_sensitive: false }, presence: true
|
9
|
+
validates :name, uniqueness: { case_sensitive: false }, presence: true
|
10
|
+
|
12
11
|
def cities
|
13
12
|
zipcodes.map(&:city).sort.uniq
|
14
13
|
end
|
@@ -1,19 +1,19 @@
|
|
1
1
|
class Zipcode < ActiveRecord::Base
|
2
|
-
attr_accessible :code, :city, :state_id, :county_id, :lat, :lon
|
3
|
-
|
4
2
|
belongs_to :county
|
5
3
|
belongs_to :state
|
6
|
-
|
7
|
-
validates :code, :
|
8
|
-
validates :state_id, :county_id, :city, :
|
9
|
-
|
4
|
+
|
5
|
+
validates :code, uniqueness: true, presence: true
|
6
|
+
validates :state_id, :county_id, :city, presence: true
|
7
|
+
|
10
8
|
scope :without_county, -> { where("county_id IS NULL") }
|
11
9
|
scope :without_state, -> { where("state_id IS NULL") }
|
12
10
|
scope :ungeocoded, -> { where("lat IS NULL OR lon IS NULL") }
|
13
11
|
|
14
12
|
class << self
|
15
13
|
def find_by_city_state(city, state)
|
16
|
-
includes(:
|
14
|
+
includes(county: :state)
|
15
|
+
.where("city like ? AND states.abbr like ?", "#{city}%", "%#{state}%")
|
16
|
+
.first
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
@@ -21,7 +21,7 @@ class Zipcode < ActiveRecord::Base
|
|
21
21
|
[lat, lon]
|
22
22
|
end
|
23
23
|
|
24
|
-
def
|
24
|
+
def geocoded?
|
25
25
|
(!lat.nil? && !lon.nil?)
|
26
26
|
end
|
27
27
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: my_zipcode_gem
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Blackburn
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2017-
|
12
|
+
date: 2017-04-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -51,7 +51,7 @@ files:
|
|
51
51
|
- ".travis.yml"
|
52
52
|
- Gemfile
|
53
53
|
- MIT-LICENSE
|
54
|
-
- README.
|
54
|
+
- README.md
|
55
55
|
- Rakefile
|
56
56
|
- features/step_definitions/common_steps.rb
|
57
57
|
- features/step_definitions/rails_setup_steps.rb
|