my_zipcode_gem 0.2.0 → 0.2.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: 70c300c19c90f28c2ac614a2795c600bfb3f0d4d
4
- data.tar.gz: 671303664dee6662adbd54f57ab3b8d9f3046f41
3
+ metadata.gz: fa3086991a379075e85a57c7165f3435dd2b193c
4
+ data.tar.gz: 73ca08c0d39a99b4da16cde40c2772d1a6098e7e
5
5
  SHA512:
6
- metadata.gz: 1a36d6a90fea133bbd531fc0e9d7faa98c9e5f12a0e534b1babd12fa1a8aa317aff7b8d8781771c8e468e426c3119b22fc931f7557ce08a08cf1622c7d7a0215
7
- data.tar.gz: 704517c8c1140868b71af6203cacb6ad77e3f96d9125a24d6d83b8c0249bba85362f6b3b9c1bd8308c7e6fb963ca31ec463cf0c1379c9ee00bfcb4e36366e00c
6
+ metadata.gz: 744078d00f5ae40c44241ac57784dbf1602a372532a125a5af6998ebb3a5b36b07d0796f7b62cc9a30db29fc19666d3482c8badb89542fab3387af7b2d78ff82
7
+ data.tar.gz: b77952605f4e0ed337ec44ee920e061b513334dd488f282ff41344e1fe6573f93cf56e05b1bad5cfd42ad07ba2634446206589c7c8270a5cdb588836d32b68f1
@@ -1,44 +1,54 @@
1
- h1. My Zipcode Gem
1
+ # My Zipcode Gem
2
2
 
3
- !https://img.shields.io/github/license/midwire/my_zipcode_gem.svg!:https://github.com/midwire/my_zipcode_gem/blob/master/MIT-LICENSE
3
+ [![MIT-LICENSE](https://img.shields.io/github/license/midwire/my_zipcode_gem.svg)](https://github.com/midwire/my_zipcode_gem/blob/master/MIT-LICENSE)
4
4
 
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.
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
- h2. Installation
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
- bc. gem 'my_zipcode_gem'
16
+ gem 'my_zipcode_gem'
12
17
 
13
18
  Run:
14
19
 
15
- bc. rake bundle install
20
+ rake bundle install
16
21
 
17
22
  Generate the models and populate the data:
18
23
 
19
- bc. rails g my_zipcode_gem:models
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
- h2. Usage
32
+ ## Usage
26
33
 
27
- bc. zipcode = Zipcode.find_by_code '66206'
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.is_geocoded? # => true, most if not all should be pre-geocoded.
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
- bc. Zipcode.find_by_city_state "Shawnee Mission", "KS"
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
- bc. state = State.find_by_abbr "MO"
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
- h3. Automatic JQuery/AJAX lookup
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
- bc. f.text_field :zip, :size => 5, :maxlength => 5, :class => 'zipcode_interactive'
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
- Then add this to your application.js, but remember to replace [mycontrollername] with your own controller.
63
-
64
- bc. $(document).ready(function() {
65
- // Interactive Zipcodes
66
- $('input.zipcode_interactive').blur(function(data) {
67
- var elem_id = $(this).attr("id");
68
- var base_id = elem_id.substring(0, elem_id.lastIndexOf("_"));
69
- $.get("/mycontrollername/get_zip_data/" + this.value, {},
70
- function(data) {
71
- var zipcode = $.parseJSON(data);
72
- var city = $('#' + base_id + '_city');
73
- var state = $('#' + base_id + '_state');
74
- var county = $('#' + base_id + '_county');
75
- if (zipcode.err) {
76
- alert(zipcode.err);
77
- } else {
78
- city.val(zipcode.city);
79
- state.val(zipcode.state)
80
- county.val(zipcode.county)
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
- bc. def get_zip_data
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
- bc. get 'mycontrollername/get_zip_data/:code', :controller => 'mycontrollername', :action => 'get_zip_data'
131
+ get 'mycontrollername/get_zip_data/:code', :controller => 'mycontrollername', :action => 'get_zip_data'
119
132
 
120
133
  That's about it.
121
134
 
122
- Let me know if there are any errors. I cut and pasted the code above from a working application, but there may be some gotchas that I missed.
135
+ Please report any errors or [issues](https://github.com/midwire/my_zipcode_gem/issues).
123
136
 
124
- h2. LOG
137
+ ## LOG
125
138
 
126
- h3. 05/03/2011:
139
+ ### 05/03/2011:
127
140
 
128
141
  Initial Release
@@ -41,6 +41,5 @@ module MyZipcodeGem
41
41
  def create_rakefile
42
42
  template 'zipcodes.rake', "lib/tasks/zipcodes.rake"
43
43
  end
44
-
45
44
  end
46
45
  end
@@ -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, :uniqueness => {:scope => :state_id, :case_sensitive => false}, :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
-
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, :uniqueness => { :case_sensitive => false }, :presence => true
10
- validates :name, :uniqueness => { :case_sensitive => false }, :presence => true
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, :uniqueness => true, :presence => true
8
- validates :state_id, :county_id, :city, :presence => true
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(:county => :state).where("city like '#{city}%' AND states.abbr like '%#{state}%'").first
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 is_geocoded?
24
+ def geocoded?
25
25
  (!lat.nil? && !lon.nil?)
26
26
  end
27
27
  end
@@ -1,5 +1,6 @@
1
1
  require 'open-uri'
2
2
  require 'csv'
3
+
3
4
  namespace :zipcodes do
4
5
 
5
6
  desc "Update states table"
@@ -1,3 +1,3 @@
1
1
  module MyZipcodeGem
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.2.1'.freeze
3
3
  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.0
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-01-03 00:00:00.000000000 Z
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.textile
54
+ - README.md
55
55
  - Rakefile
56
56
  - features/step_definitions/common_steps.rb
57
57
  - features/step_definitions/rails_setup_steps.rb