us_zipcode 0.1.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 +7 -0
- data/.gitignore +7 -0
- data/.travis.yml +19 -0
- data/Gemfile +21 -0
- data/MIT-LICENSE +20 -0
- data/README.textile +179 -0
- data/README.textile~ +184 -0
- data/Rakefile +19 -0
- data/features/step_definitions/common_steps.rb +76 -0
- data/features/step_definitions/rails_setup_steps.rb +17 -0
- data/features/step_definitions/rails_setup_steps.rb~ +17 -0
- data/features/support/env.rb +10 -0
- data/features/support/string.rb +128 -0
- data/features/zipcodes.feature +24 -0
- data/features/zipcodes.feature~ +24 -0
- data/lib/generators/us_zipcode/models_generator.rb +41 -0
- data/lib/generators/us_zipcode/models_generator.rb~ +41 -0
- data/lib/generators/us_zipcode/templates/county_model.rb +17 -0
- data/lib/generators/us_zipcode/templates/county_model.rb~ +17 -0
- data/lib/generators/us_zipcode/templates/migration.rb +41 -0
- data/lib/generators/us_zipcode/templates/migration.rb~ +41 -0
- data/lib/generators/us_zipcode/templates/state_model.rb +15 -0
- data/lib/generators/us_zipcode/templates/state_model.rb~ +15 -0
- data/lib/generators/us_zipcode/templates/zipcode_model.rb +30 -0
- data/lib/generators/us_zipcode/templates/zipcode_model.rb~ +30 -0
- data/lib/generators/us_zipcode/templates/zipcodes.rake +163 -0
- data/lib/generators/us_zipcode/templates/zipcodes.rake~ +163 -0
- data/lib/generators/us_zipcode/version.rb +3 -0
- data/lib/generators/us_zipcode/version.rb~ +3 -0
- data/lib/my_zipcode_gem.rb~ +34 -0
- data/lib/us_zipcode.rb +29 -0
- data/lib/us_zipcode.rb~ +29 -0
- data/rails3_2.gemfile +21 -0
- data/rails4_0.gemfile +23 -0
- data/rails4_1.gemfile +23 -0
- data/us_zipcode.gemspec +24 -0
- data/us_zipcode.gemspec~ +24 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 53d43033c31d31ac68965cdead72e8773b697010
|
4
|
+
data.tar.gz: bc506c4a298bfdfe6d98b87c1f55993e73056332
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7c091e5a454709020baa2025a9cc68e7112ce4c22e543f9986d28e7a54c21b5e1440ecb7f01c2e5f0246b9d71c972914517d2f17f1d00597e858650e67010e2c
|
7
|
+
data.tar.gz: 66f68df2b60e9c270ddd2c1fa03a22d5e58f1c18efecb0a2853628683ae1a0fcd0a5d4206bf6bcd7e2f078f4be94551f4d9d18ea3d030108727c46df6915c7ce
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
language: ruby
|
2
|
+
bundler_args: --without development
|
3
|
+
rvm:
|
4
|
+
- 1.9.3
|
5
|
+
- 2.0
|
6
|
+
- 2.1
|
7
|
+
|
8
|
+
before_script:
|
9
|
+
# - psql -c 'create database my_zipcode_gem_test;' -U postgres
|
10
|
+
# - mysql -e 'create database my_zipcode_gem_test;'
|
11
|
+
|
12
|
+
gemfile:
|
13
|
+
- rails3_2.gemfile
|
14
|
+
- rails4_0.gemfile
|
15
|
+
- rails4_1.gemfile
|
16
|
+
|
17
|
+
branches:
|
18
|
+
only:
|
19
|
+
- master
|
data/Gemfile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
# jquery-rails is used by the dummy application
|
6
|
+
gem 'jquery-rails'
|
7
|
+
gem 'sqlite3'
|
8
|
+
gem 'mysql2'
|
9
|
+
gem 'pg'
|
10
|
+
gem 'midwire_common'
|
11
|
+
|
12
|
+
group :test do
|
13
|
+
gem 'capybara'
|
14
|
+
gem 'guard-rspec'
|
15
|
+
gem 'guard-spork'
|
16
|
+
gem 'shoulda', '~> 3.5.0'
|
17
|
+
gem 'rspec'
|
18
|
+
gem 'rspec-rails', '~> 2.12.0'
|
19
|
+
gem 'cucumber'
|
20
|
+
gem 'cucumber-rails', '~> 1.3.0', require: false
|
21
|
+
end
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2011 Chris Blackburn
|
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.textile
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
h1. Us Zip Code
|
2
|
+
|
3
|
+
|
4
|
+
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
|
+
|
6
|
+
h2. Installation
|
7
|
+
|
8
|
+
Add the following line to your Gemfile:
|
9
|
+
|
10
|
+
bc. gem 'us_zipcode'
|
11
|
+
|
12
|
+
Run:
|
13
|
+
|
14
|
+
bc. rake bundle install
|
15
|
+
|
16
|
+
Generate the models and populate the data:
|
17
|
+
|
18
|
+
bc. rails g us_zipcode:models
|
19
|
+
rake db:migrate
|
20
|
+
rake zipcodes:update
|
21
|
+
|
22
|
+
You should now have three new tables and three new models, Zipcode, State, County.
|
23
|
+
|
24
|
+
h2. Usage
|
25
|
+
|
26
|
+
bc. zipcode = Zipcode.find_by_code '66206'
|
27
|
+
zipcode.state.abbr # => 'KS'
|
28
|
+
zipcode.city # => 'Shawnee Mission'
|
29
|
+
zipcode.county.name # => 'Johnson'
|
30
|
+
zipcode.lat.to_s # => '38.959356', it is actually a BigDecimal object converted to_s for documentation.
|
31
|
+
zipcode.lon.to_s # => '-94.716155', ditto
|
32
|
+
zipcode.is_geocoded? # => true, most if not all should be pre-geocoded.
|
33
|
+
|
34
|
+
|
35
|
+
You can use State and County objects as follows:
|
36
|
+
|
37
|
+
bc. state = State.find_by_abbr "MO"
|
38
|
+
state.cities.count # => 963
|
39
|
+
state.cities # gives you a sorted array of all cities for the state
|
40
|
+
state.zipcodes.count # => 1195
|
41
|
+
...
|
42
|
+
county = state.counties.first
|
43
|
+
county.cities.count # => 5
|
44
|
+
county.cities # gives you a sorted array of all cities for the county
|
45
|
+
county.zipcodes.count # => 5
|
46
|
+
|
47
|
+
|
48
|
+
h3. Look Up City by State & zip code + county by city
|
49
|
+
|
50
|
+
First you have to select a state .On state selection , you can see the cities drop down which are associated with the selected state.
|
51
|
+
On city selection, you can see the corresponding county with its zip code.
|
52
|
+
|
53
|
+
Write the following code in your view:
|
54
|
+
|
55
|
+
<%= select_tag "state",options_from_collection_for_select(State.all, "id", "name") %>
|
56
|
+
<div id ="city"></div>
|
57
|
+
<div id ="county"></div>
|
58
|
+
<script>
|
59
|
+
$("#state").on("change", function () {
|
60
|
+
$.ajax({
|
61
|
+
url: "/mycontroller/get_cities_by_state",
|
62
|
+
dataType: "script",
|
63
|
+
method: "get",
|
64
|
+
data: {state_id: $(this).val()}
|
65
|
+
});
|
66
|
+
$("#county").html('');
|
67
|
+
});
|
68
|
+
</script>
|
69
|
+
|
70
|
+
Create mycontroller & paste the following code:
|
71
|
+
|
72
|
+
def get_cities_by_state
|
73
|
+
@cities = Zipcode.group(:city).where(state_id: params[:state_id])
|
74
|
+
end
|
75
|
+
|
76
|
+
def get_county_and_zip_by_city
|
77
|
+
@zipcodes = Zipcode.find_by_city(params[:city])
|
78
|
+
end
|
79
|
+
|
80
|
+
Create the following two js.erb files in app/views/mycontroller/
|
81
|
+
|
82
|
+
1.get_cities_by_state.js.erb & paste the following code:
|
83
|
+
|
84
|
+
$("#city").html('<%= escape_javascript(select_tag "city", options_from_collection_for_select(@cities, :id, :city)) %>');
|
85
|
+
$("#city").on("change", function(){
|
86
|
+
$.ajax({
|
87
|
+
url: "/home/get_county_and_zip_by_city",
|
88
|
+
dataType: "script",
|
89
|
+
method: "get",
|
90
|
+
data: {city: $("#city select option:selected"). text()}
|
91
|
+
});
|
92
|
+
});
|
93
|
+
|
94
|
+
2.get_county_and_zip_by_city.js.erb
|
95
|
+
|
96
|
+
$("#county").html('<%= escape_javascript(select_tag "county", options_from_collection_for_select(@zipcodes, :id, :county_and_zip)) %>');
|
97
|
+
|
98
|
+
In the last update your routes:
|
99
|
+
|
100
|
+
scope '/mycontroller' do
|
101
|
+
get 'get_cities_by_state' => 'mycontroller#get_cities_by_state'
|
102
|
+
get 'get_county_and_zip_by_city' => 'mycontroller#get_county_and_zip_by_city'
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
h3. Automatic JQuery/AJAX lookup
|
108
|
+
|
109
|
+
You can have a user enter a zipcode and automatically lookup their city, state and county.
|
110
|
+
|
111
|
+
Put something like this in your view:
|
112
|
+
|
113
|
+
bc. f.text_field :zip, :size => 5, :maxlength => 5, :class => 'zipcode_interactive'
|
114
|
+
f.text_field :city, :size => 20, :maxlength => 60, :readonly => true
|
115
|
+
f.text_field(:state, :size => 2, :maxlength => 2, :readonly => true)
|
116
|
+
f.text_field(:county, :size => 20, :maxlength => 60, :readonly => true)
|
117
|
+
|
118
|
+
Then add this to your application.js, but remember to replace [mycontrollername] with your own controller.
|
119
|
+
|
120
|
+
bc. $(document).ready(function() {
|
121
|
+
// Interactive Zipcodes
|
122
|
+
$('input.zipcode_interactive').blur(function(data) {
|
123
|
+
var elem_id = $(this).attr("id");
|
124
|
+
var base_id = elem_id.substring(0, elem_id.lastIndexOf("_"));
|
125
|
+
$.get("/mycontrollername/get_zip_data/" + this.value, {},
|
126
|
+
function(data) {
|
127
|
+
var zipcode = $.parseJSON(data);
|
128
|
+
var city = $('#' + base_id + '_city');
|
129
|
+
var state = $('#' + base_id + '_state');
|
130
|
+
var county = $('#' + base_id + '_county');
|
131
|
+
if (zipcode.err) {
|
132
|
+
alert(zipcode.err);
|
133
|
+
} else {
|
134
|
+
city.val(zipcode.city);
|
135
|
+
state.val(zipcode.state)
|
136
|
+
county.val(zipcode.county)
|
137
|
+
}
|
138
|
+
})
|
139
|
+
});
|
140
|
+
});
|
141
|
+
|
142
|
+
You will also need a controller method similar to this, which will return the data to your form:
|
143
|
+
|
144
|
+
bc. def get_zip_data
|
145
|
+
@zipcode = Zipcode.find_by_code(params[:code], :include => [:county, :state])
|
146
|
+
if @zipcode
|
147
|
+
@counties = County.find(:all, :conditions => [ "state_id = ?", @zipcode.county.state_id ])
|
148
|
+
data = {
|
149
|
+
'state' => @zipcode.state.abbr,
|
150
|
+
'county' => @zipcode.county.name,
|
151
|
+
'city' => @zipcode.city.titleize
|
152
|
+
}
|
153
|
+
render :text => data.to_json
|
154
|
+
else
|
155
|
+
if params[:code].blank?
|
156
|
+
return true
|
157
|
+
else
|
158
|
+
if params[:code].is_zipcode?
|
159
|
+
data = {
|
160
|
+
'err' => "Could not find Zipcode [#{params[:code]}]. If this is a valid zipcode please notify support <support@mydomain.com>, so we can update our database."
|
161
|
+
}
|
162
|
+
else
|
163
|
+
data = {
|
164
|
+
'err' => "[#{params[:code]}] is not a valid Zipcode."
|
165
|
+
}
|
166
|
+
end
|
167
|
+
render :text => data.to_json
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
And define a route for the AJAX function in routes.rb:
|
173
|
+
|
174
|
+
bc. get 'mycontrollername/get_zip_data/:code', :controller => 'mycontrollername', :action => 'get_zip_data'
|
175
|
+
|
176
|
+
That's about it.
|
177
|
+
|
178
|
+
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.
|
179
|
+
|
data/README.textile~
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
h1. Us Zip Code
|
2
|
+
|
3
|
+
|
4
|
+
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
|
+
|
6
|
+
h2. Installation
|
7
|
+
|
8
|
+
Add the following line to your Gemfile:
|
9
|
+
|
10
|
+
bc. gem 'us_zipcode'
|
11
|
+
|
12
|
+
Run:
|
13
|
+
|
14
|
+
bc. rake bundle install
|
15
|
+
|
16
|
+
Generate the models and populate the data:
|
17
|
+
|
18
|
+
bc. rails g us_zipcode:models
|
19
|
+
rake db:migrate
|
20
|
+
rake zipcodes:update
|
21
|
+
|
22
|
+
You should now have three new tables and three new models, Zipcode, State, County.
|
23
|
+
|
24
|
+
h2. Usage
|
25
|
+
|
26
|
+
bc. zipcode = Zipcode.find_by_code '66206'
|
27
|
+
zipcode.state.abbr # => 'KS'
|
28
|
+
zipcode.city # => 'Shawnee Mission'
|
29
|
+
zipcode.county.name # => 'Johnson'
|
30
|
+
zipcode.lat.to_s # => '38.959356', it is actually a BigDecimal object converted to_s for documentation.
|
31
|
+
zipcode.lon.to_s # => '-94.716155', ditto
|
32
|
+
zipcode.is_geocoded? # => true, most if not all should be pre-geocoded.
|
33
|
+
|
34
|
+
|
35
|
+
You can use State and County objects as follows:
|
36
|
+
|
37
|
+
bc. state = State.find_by_abbr "MO"
|
38
|
+
state.cities.count # => 963
|
39
|
+
state.cities # gives you a sorted array of all cities for the state
|
40
|
+
state.zipcodes.count # => 1195
|
41
|
+
...
|
42
|
+
county = state.counties.first
|
43
|
+
county.cities.count # => 5
|
44
|
+
county.cities # gives you a sorted array of all cities for the county
|
45
|
+
county.zipcodes.count # => 5
|
46
|
+
|
47
|
+
|
48
|
+
h3. Look Up City by State & zip code + county by city
|
49
|
+
|
50
|
+
First you have to select a state .On state selection , you can see the cities drop down which are associated with the selected state.
|
51
|
+
On city selection, you can see the corresponding county with its zip code.
|
52
|
+
|
53
|
+
Write the following code in your view:
|
54
|
+
|
55
|
+
<%= select_tag "state",options_from_collection_for_select(State.all, "id", "name") %>
|
56
|
+
<div id ="city"></div>
|
57
|
+
<div id ="county"></div>
|
58
|
+
<script>
|
59
|
+
$("#state").on("change", function () {
|
60
|
+
$.ajax({
|
61
|
+
url: "/mycontroller/get_cities_by_state",
|
62
|
+
dataType: "script",
|
63
|
+
method: "get",
|
64
|
+
data: {state_id: $(this).val()}
|
65
|
+
});
|
66
|
+
$("#county").html('');
|
67
|
+
});
|
68
|
+
</script>
|
69
|
+
|
70
|
+
Create mycontroller & paste the following code:
|
71
|
+
|
72
|
+
def get_cities_by_state
|
73
|
+
@cities = Zipcode.group(:city).where(state_id: params[:state_id])
|
74
|
+
end
|
75
|
+
|
76
|
+
def get_county_and_zip_by_city
|
77
|
+
@zipcodes = Zipcode.find_by_city(params[:city])
|
78
|
+
end
|
79
|
+
|
80
|
+
Create the following two js.erb files in app/views/mycontroller/
|
81
|
+
|
82
|
+
1.get_cities_by_state.js.erb & paste the following code:
|
83
|
+
|
84
|
+
$("#city").html('<%= escape_javascript(select_tag "city", options_from_collection_for_select(@cities, :id, :city)) %>');
|
85
|
+
$("#city").on("change", function(){
|
86
|
+
$.ajax({
|
87
|
+
url: "/home/get_county_and_zip_by_city",
|
88
|
+
dataType: "script",
|
89
|
+
method: "get",
|
90
|
+
data: {city: $("#city select option:selected"). text()}
|
91
|
+
});
|
92
|
+
});
|
93
|
+
|
94
|
+
2.get_county_and_zip_by_city.js.erb
|
95
|
+
|
96
|
+
$("#county").html('<%= escape_javascript(select_tag "county", options_from_collection_for_select(@zipcodes, :id, :county_and_zip)) %>');
|
97
|
+
|
98
|
+
In the last update your routes:
|
99
|
+
|
100
|
+
scope '/mycontroller' do
|
101
|
+
get 'get_cities_by_state' => 'mycontroller#get_cities_by_state'
|
102
|
+
get 'get_county_and_zip_by_city' => 'mycontroller#get_county_and_zip_by_city'
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
h3. Automatic JQuery/AJAX lookup
|
108
|
+
|
109
|
+
You can have a user enter a zipcode and automatically lookup their city, state and county.
|
110
|
+
|
111
|
+
Put something like this in your view:
|
112
|
+
|
113
|
+
bc. f.text_field :zip, :size => 5, :maxlength => 5, :class => 'zipcode_interactive'
|
114
|
+
f.text_field :city, :size => 20, :maxlength => 60, :readonly => true
|
115
|
+
f.text_field(:state, :size => 2, :maxlength => 2, :readonly => true)
|
116
|
+
f.text_field(:county, :size => 20, :maxlength => 60, :readonly => true)
|
117
|
+
|
118
|
+
Then add this to your application.js, but remember to replace [mycontrollername] with your own controller.
|
119
|
+
|
120
|
+
bc. $(document).ready(function() {
|
121
|
+
// Interactive Zipcodes
|
122
|
+
$('input.zipcode_interactive').blur(function(data) {
|
123
|
+
var elem_id = $(this).attr("id");
|
124
|
+
var base_id = elem_id.substring(0, elem_id.lastIndexOf("_"));
|
125
|
+
$.get("/mycontrollername/get_zip_data/" + this.value, {},
|
126
|
+
function(data) {
|
127
|
+
var zipcode = $.parseJSON(data);
|
128
|
+
var city = $('#' + base_id + '_city');
|
129
|
+
var state = $('#' + base_id + '_state');
|
130
|
+
var county = $('#' + base_id + '_county');
|
131
|
+
if (zipcode.err) {
|
132
|
+
alert(zipcode.err);
|
133
|
+
} else {
|
134
|
+
city.val(zipcode.city);
|
135
|
+
state.val(zipcode.state)
|
136
|
+
county.val(zipcode.county)
|
137
|
+
}
|
138
|
+
})
|
139
|
+
});
|
140
|
+
});
|
141
|
+
|
142
|
+
You will also need a controller method similar to this, which will return the data to your form:
|
143
|
+
|
144
|
+
bc. def get_zip_data
|
145
|
+
@zipcode = Zipcode.find_by_code(params[:code], :include => [:county, :state])
|
146
|
+
if @zipcode
|
147
|
+
@counties = County.find(:all, :conditions => [ "state_id = ?", @zipcode.county.state_id ])
|
148
|
+
data = {
|
149
|
+
'state' => @zipcode.state.abbr,
|
150
|
+
'county' => @zipcode.county.name,
|
151
|
+
'city' => @zipcode.city.titleize
|
152
|
+
}
|
153
|
+
render :text => data.to_json
|
154
|
+
else
|
155
|
+
if params[:code].blank?
|
156
|
+
return true
|
157
|
+
else
|
158
|
+
if params[:code].is_zipcode?
|
159
|
+
data = {
|
160
|
+
'err' => "Could not find Zipcode [#{params[:code]}]. If this is a valid zipcode please notify support <support@mydomain.com>, so we can update our database."
|
161
|
+
}
|
162
|
+
else
|
163
|
+
data = {
|
164
|
+
'err' => "[#{params[:code]}] is not a valid Zipcode."
|
165
|
+
}
|
166
|
+
end
|
167
|
+
render :text => data.to_json
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
And define a route for the AJAX function in routes.rb:
|
173
|
+
|
174
|
+
bc. get 'mycontrollername/get_zip_data/:code', :controller => 'mycontrollername', :action => 'get_zip_data'
|
175
|
+
|
176
|
+
That's about it.
|
177
|
+
|
178
|
+
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.
|
179
|
+
|
180
|
+
h2. LOG
|
181
|
+
|
182
|
+
h3. 05/03/2011:
|
183
|
+
|
184
|
+
Initial Release
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'midwire_common/rake_tasks'
|
6
|
+
rescue StandardError => e
|
7
|
+
puts('>>> Can\'t load midwire_common/rake_tasks.')
|
8
|
+
puts(">>> Did you 'bundle exec'?: #{e.message}")
|
9
|
+
exit
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'cucumber'
|
13
|
+
require 'cucumber/rake/task'
|
14
|
+
Cucumber::Rake::Task.new(:features) do |t|
|
15
|
+
t.cucumber_opts = "features --format progress"
|
16
|
+
end
|
17
|
+
|
18
|
+
task :default => :features
|
19
|
+
task :test => :features
|
@@ -0,0 +1,76 @@
|
|
1
|
+
When(/^I run "([^\"]*)"$/) do |command|
|
2
|
+
system("cd #{@current_directory} && #{command}").should be_true
|
3
|
+
end
|
4
|
+
|
5
|
+
When(/^I add "([^\"]*)" to file "([^\"]*)"$/) do |content, short_path|
|
6
|
+
path = File.join(@current_directory, short_path)
|
7
|
+
File.should exist(path)
|
8
|
+
File.open(path, 'a') { |f| f.write(content + "\n") }
|
9
|
+
end
|
10
|
+
|
11
|
+
When(/^I replace "([^\"]*)" with "([^\"]*)" in file "([^\"]*)"$/) do |old_content, new_content, short_path|
|
12
|
+
path = File.join(@current_directory, short_path)
|
13
|
+
File.should exist(path)
|
14
|
+
content = File.read(path).gsub(old_content, new_content)
|
15
|
+
File.open(path, 'w') { |f| f.write(content) }
|
16
|
+
end
|
17
|
+
|
18
|
+
When(/^I insert "([^\"]*)" into "([^\"]*)" after line (\d+)$/) do |content, short_path, after_line|
|
19
|
+
path = File.join(@current_directory, short_path)
|
20
|
+
File.should exist(path)
|
21
|
+
lines = File.read(path).split("\n")
|
22
|
+
lines[after_line.to_i, 0] = content
|
23
|
+
File.open(path, 'w') { |f| f.write(lines.join("\n")) }
|
24
|
+
end
|
25
|
+
|
26
|
+
Then(/^I should see file "([^\"]*)"$/) do |path|
|
27
|
+
File.should exist(File.join(@current_directory, path))
|
28
|
+
end
|
29
|
+
|
30
|
+
Then(/^I should see "(.*)" in file "([^\"]*)"$/) do |content, short_path|
|
31
|
+
path = File.join(@current_directory, short_path)
|
32
|
+
File.should exist(path)
|
33
|
+
File.readlines(path).join.should include(content)
|
34
|
+
end
|
35
|
+
|
36
|
+
Then(/^I should not see "(.*)" in file "([^\"]*)"$/) do |content, short_path|
|
37
|
+
path = File.join(@current_directory, short_path)
|
38
|
+
File.should exist(path)
|
39
|
+
File.readlines(path).join.should_not include(content)
|
40
|
+
end
|
41
|
+
|
42
|
+
Then(/^I should see the following files$/) do |table|
|
43
|
+
table.raw.flatten.each do |path|
|
44
|
+
File.should exist(File.join(@current_directory, path))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
Then(/^I should see the following in file "([^\"]*)"$/) do |short_path, table|
|
49
|
+
path = File.join(@current_directory, short_path)
|
50
|
+
File.should exist(path)
|
51
|
+
table.raw.flatten.each do |content|
|
52
|
+
File.readlines(path).join.should include(content)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
Then(/^I should successfully run "([^\"]*)"$/) do |command|
|
57
|
+
system("cd #{@current_directory} && #{command}").should be_true
|
58
|
+
end
|
59
|
+
|
60
|
+
Then(/^I should see "([^\"]*)" when running "([^\"]*)"$/) do |expected_response, command|
|
61
|
+
`cd #{@current_directory} && #{command}`.should include(expected_response)
|
62
|
+
end
|
63
|
+
|
64
|
+
Then(/^I should see (\d+) records in the "([^\"]*)" table$/) do |count, table_name|
|
65
|
+
FileUtils.chdir(@current_directory)
|
66
|
+
ActiveRecord::Base.establish_connection(
|
67
|
+
:adapter => 'sqlite3',
|
68
|
+
:database => "db/test.sqlite3",
|
69
|
+
:pool => 5,
|
70
|
+
:timeout => 5000
|
71
|
+
)
|
72
|
+
sql = "SELECT COUNT(*) FROM #{table_name}"
|
73
|
+
result = ActiveRecord::Base.connection.select_rows(sql)
|
74
|
+
puts ">>> result: [#{result.flatten.first}]"
|
75
|
+
"Record Count:#{result.flatten.first}".should == "Record Count:#{count}"
|
76
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
Given(/^a new Rails app$/) do
|
2
|
+
FileUtils.rm_rf "tmp/rails_app"
|
3
|
+
FileUtils.mkdir_p("tmp")
|
4
|
+
system("rails new tmp/rails_app -T").should be_true
|
5
|
+
system("ln -s ../../../lib/generators tmp/rails_app/lib/generators").should be_true
|
6
|
+
@current_directory = File.expand_path("tmp/rails_app")
|
7
|
+
end
|
8
|
+
|
9
|
+
Given %{a new migrated Rails app} do
|
10
|
+
# Don't delete the rails app
|
11
|
+
FileUtils.mkdir_p("tmp")
|
12
|
+
system("rails new tmp/rails_app -f").should be_true
|
13
|
+
system("ln -sf ../../../lib/generators tmp/rails_app/lib/generators").should be_true
|
14
|
+
@current_directory = File.expand_path("tmp/rails_app")
|
15
|
+
When %{I run "rails g us_zipcode:models"}
|
16
|
+
Then %{I should successfully run "rake db:migrate"}
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
Given(/^a new Rails app$/) do
|
2
|
+
FileUtils.rm_rf "tmp/rails_app"
|
3
|
+
FileUtils.mkdir_p("tmp")
|
4
|
+
system("rails new tmp/rails_app -T").should be_true
|
5
|
+
system("ln -s ../../../lib/generators tmp/rails_app/lib/generators").should be_true
|
6
|
+
@current_directory = File.expand_path("tmp/rails_app")
|
7
|
+
end
|
8
|
+
|
9
|
+
Given %{a new migrated Rails app} do
|
10
|
+
# Don't delete the rails app
|
11
|
+
FileUtils.mkdir_p("tmp")
|
12
|
+
system("rails new tmp/rails_app -f").should be_true
|
13
|
+
system("ln -sf ../../../lib/generators tmp/rails_app/lib/generators").should be_true
|
14
|
+
@current_directory = File.expand_path("tmp/rails_app")
|
15
|
+
When %{I run "rails g my_zipcode_gem:models"}
|
16
|
+
Then %{I should successfully run "rake db:migrate"}
|
17
|
+
end
|