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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dcba0f183cd507240edbdf8339df464e59f45b72
4
- data.tar.gz: 196b2ca58e9871903acfd9c66aac2df6caa4ca0f
3
+ metadata.gz: 80a0dae6b23729364949a833b8072dbfaa75a587
4
+ data.tar.gz: 955c6d77c5efb151d998e2ec17f2b73f88d2bd29
5
5
  SHA512:
6
- metadata.gz: bb922c16004b3de26cde88f3bd3de274cb07a1eaf31debc5a8abb82728e963977effc7fa0eee7e4c6449927fcfb2c5708d6f87fd97e8a511957903b23c5f30ae
7
- data.tar.gz: d7de550a5b1ea2982e873fe6e7dfd2f0816f2e7e87fd30b364b7dd483e51cdb21d98a6e2e9d4654e2121689aff655f24d4577ce485acd5e93622eb8232ffec8d
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
@@ -1,8 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- autocomplete_zipcode (0.0.1)
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
+ })
@@ -5,6 +5,7 @@
5
5
 
6
6
  <div class="form-inputs">
7
7
  <%= f.input :zipcode, as: :zipcode %>
8
+ <small style="display: none;" id="error">Error - Invalid zipcode</small>
8
9
  <%= f.input :street, as: :street %>
9
10
  <%= f.input :neighborhood, as: :neighborhood %>
10
11
  <%= f.input :city, as: :city %>
@@ -1,3 +1,3 @@
1
1
  module AutocompleteZicode
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -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 = $('[data-provider="zipcode"]').val().replace(/[^0-9]/g, '');
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
- $(document).ready(ready);
24
- $(document).on("turbolinks:load", ready);
28
+ if (typeof Turbolinks == "undefined") {
29
+ $(document).ready(ready);
30
+ } else {
31
+ $(document).on("turbolinks:load", ready);
32
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autocomplete_zipcode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Barreto