autocomplete_zipcode 0.1.1 → 0.1.2
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 +75 -0
- data/example/Gemfile.lock +1 -3
- data/example/app/assets/javascripts/application.js +9 -0
- data/example/app/views/addresses/_form.html.erb +1 -0
- data/lib/autocomplete_zipcode/version.rb +1 -1
- data/vendor/assets/javascripts/autocomplete_zipcode.js +12 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 80a0dae6b23729364949a833b8072dbfaa75a587
|
4
|
+
data.tar.gz: 955c6d77c5efb151d998e2ec17f2b73f88d2bd29
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c690142e7826bfdd683ff77833801ea46c6bfcab0360373bae7991809845f2039e5bb125173d20d03cecf98d417f1a696f8d8f523923c320b2f6034c1b1479b4
|
7
|
+
data.tar.gz: 74b0616c607da31c3dc02f5c47b56808161de998ddf4314a6c1792556c339bd6b95f49f6744f52b690bfce14fa05523830e8b2345e89608f51955d86a39a28ba
|
data/README.md
CHANGED
@@ -0,0 +1,75 @@
|
|
1
|
+
# AutocompleteZipcode
|
2
|
+
|
3
|
+
This gem was built to "automagically" fills an address form, for Rails version >= 3.1.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add the following gems to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'autocomplete_zipcode'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
```bash
|
17
|
+
$ bundle install
|
18
|
+
```
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
In app/assets/javascripts/application.js, you should add as follows:
|
23
|
+
|
24
|
+
```js
|
25
|
+
//= require ...
|
26
|
+
//= require autocomplete_zipcode
|
27
|
+
//= require ...
|
28
|
+
```
|
29
|
+
|
30
|
+
Basic Example:
|
31
|
+
|
32
|
+
```erb
|
33
|
+
<%= simple_form_for :example do | f | %>
|
34
|
+
...
|
35
|
+
<%= f.input :zipcode, as: :zipcode %>
|
36
|
+
<%= f.input :street, as: :street %>
|
37
|
+
<%= f.input :neighborhood, as: :neighborhood %>
|
38
|
+
<%= f.input :city, as: :city %>
|
39
|
+
<%= f.input :state, as: :state %>
|
40
|
+
...
|
41
|
+
<% end %>
|
42
|
+
```
|
43
|
+
|
44
|
+
If you are not using simple_form, then simply add the `data-provider="zipcode"` and the other fields name to the input field yourself.
|
45
|
+
|
46
|
+
```erb
|
47
|
+
<%= form_for :example do | f | %>
|
48
|
+
...
|
49
|
+
<%= f.text_field :zipcode, data: {provider: :zipcode} %>
|
50
|
+
<%= f.text_field :street, data: {provider: :street} %>
|
51
|
+
...
|
52
|
+
<% end %>
|
53
|
+
```
|
54
|
+
|
55
|
+
## Handle invalid zipcodes
|
56
|
+
|
57
|
+
Simply add an event listener callback to `zipcode.error`, for example:
|
58
|
+
|
59
|
+
```js
|
60
|
+
document.addEventListener('zipcode.error', function(e) {
|
61
|
+
alert('Invalid zipcode!!!')
|
62
|
+
});
|
63
|
+
```
|
64
|
+
|
65
|
+
## Sample projects
|
66
|
+
|
67
|
+
For an example, take a look at the `example` folder in this repository.
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
1. Fork it
|
72
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
73
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
74
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
75
|
+
5. Create new Pull Request
|
data/example/Gemfile.lock
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ..
|
3
3
|
specs:
|
4
|
-
autocomplete_zipcode (0.
|
5
|
-
rails-assets-jquery
|
4
|
+
autocomplete_zipcode (0.1.1)
|
6
5
|
|
7
6
|
GEM
|
8
7
|
remote: https://rubygems.org/
|
@@ -102,7 +101,6 @@ GEM
|
|
102
101
|
bundler (>= 1.3.0, < 2.0)
|
103
102
|
railties (= 5.0.2)
|
104
103
|
sprockets-rails (>= 2.0.0)
|
105
|
-
rails-assets-jquery (3.2.1)
|
106
104
|
rails-dom-testing (2.0.2)
|
107
105
|
activesupport (>= 4.2.0, < 6.0)
|
108
106
|
nokogiri (~> 1.6)
|
@@ -15,3 +15,12 @@
|
|
15
15
|
//= require turbolinks
|
16
16
|
//= require autocomplete_zipcode
|
17
17
|
//= require_tree .
|
18
|
+
|
19
|
+
$(document).on("turbolinks:load", function() {
|
20
|
+
document.addEventListener('zipcode.error', function(e) {
|
21
|
+
$('#error').show()
|
22
|
+
setTimeout(function() {
|
23
|
+
$('#error').fadeOut(1000);
|
24
|
+
}, 5000);
|
25
|
+
});
|
26
|
+
})
|
@@ -1,11 +1,16 @@
|
|
1
1
|
var ready = function() {
|
2
2
|
zipcode_input = $('[data-provider="zipcode"]')
|
3
3
|
zipcode_input.keyup(function(e){
|
4
|
-
var zipcode =
|
4
|
+
var zipcode = zipcode_input.val().replace(/[^0-9]/g, '');
|
5
5
|
if(zipcode.length == 8) {
|
6
6
|
$.get('http://viacep.com.br/ws/'+ zipcode +'/json/').then(function(response) {
|
7
|
+
if (response.erro) {
|
8
|
+
var event = new Event('zipcode.error');
|
9
|
+
|
10
|
+
document.dispatchEvent(event);
|
11
|
+
}
|
12
|
+
|
7
13
|
var inputs = {
|
8
|
-
// zipcode: 'cep',
|
9
14
|
street: 'logradouro',
|
10
15
|
neighborhood: 'bairro',
|
11
16
|
city: 'localidade',
|
@@ -20,5 +25,8 @@ var ready = function() {
|
|
20
25
|
})
|
21
26
|
};
|
22
27
|
|
23
|
-
|
24
|
-
$(document).
|
28
|
+
if (typeof Turbolinks == "undefined") {
|
29
|
+
$(document).ready(ready);
|
30
|
+
} else {
|
31
|
+
$(document).on("turbolinks:load", ready);
|
32
|
+
}
|