No release in over 3 years
Low commit activity in last 3 years
Fill in your brazillian addresses automagically
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 4.4.0
~> 0.14.0
~> 13.0.3
~> 5.0.0
~> 1.12.0
~> 0.20.0
>= 5.1.0
~> 1.4.2

Runtime

>= 3.1.0
 Project Readme

AutocompleteZipcode

This gem was built to "automagically" fills an address form, for Rails version >= 3.1.

Status

Build Status Maintainability Test Coverage Ruby Style Guide RubyGems

Try it

Online

Installation

Add the following gems to your application's Gemfile:

gem 'autocomplete_zipcode'

And then execute:

bundle install

Simple Usage

In app/assets/javascripts/application.js, you should add as follows:

// app/assets/javascripts/application.js
//= require ...
//= require turbolinks
//= require jquery
//= require autocomplete_zipcode

$(document).on('ready', () => {
  AutocompleteZipcode.mount();
});

Basic Example

<%= simple_form_for :example do |f| %>
  ...
  <%= f.input :zipcode, as: :zipcode %>
  <%= f.input :street, as: :street %>
  <%= f.input :neighborhood, as: :neighborhood %>
  <%= f.input :city, as: :city %>
  <%= f.input :state, as: :state %>
  ...
<% end %>

If you are not using simple_form, then simply add the data-provider="zipcode" and the other fields name to the input field yourself.

<%= form_for :example do |f| %>
  ...
  <%= f.text_field :zipcode, data: { autocomplete_zipcode_provider: :zipcode } %>
  <%= f.text_field :street, data: { autocomplete_zipcode_provider: :street }  %>
  ...
<% end %>

Handle invalid zipcodes

Simply add an event listener callback to zipcode.error, for example:

// app/assets/javascripts/application.js

document.addEventListener('zipcode.error', () => {
  alert('Invalid zipcode!!!')
});

Advanced Usage

// app/assets/javascripts/application.js
//= require ...
//= require turbolinks
//= require jquery
//= require autocomplete_zipcode

$(document).on('ready', () => {
  AutocompleteZipcode.mount({
    onSuccess: (containerEl, zipcodeEl) => {
      console.log(containerEl, zipcodeEl);
    },
    onFail: (containerEl, zipcodeEl) => {
      console.log(containerEl, zipcodeEl);
    },
  });

  document.addEventListener('zipcode.success', () => {
    console.log('zipcode fetched successfully');
  });

  document.addEventListener('zipcode.error', () => {
    console.log('zipcode fetch failed');
  });
});

Sample projects

For an online example, take a look at this repository. You can also see it working on the dummy application (spec/dummy)

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request