Home

Awesome

ActiveAdmin CEP Auto Complete (brazilian postal code)

Auto complete your addresses fields with Brazilian CEP (zip code).

Installation

Include to your Gemfile

gem 'active_admin-cep_auto_complete'
<img src="./demo.gif"/>

Usage

1. Create a custom page in app/admin/cep.rb

ActiveAdmin.register_page 'CEP' do
  setup_cep_auto_complete
end

2. Add a CEP Input

f.input :cep, as: :cep
f.input :street
f.input :state
f.input :city
f.input :neighborhood

p.s. if you have a different input name, you'll have to change your URL option.

3. It works!!

Options

Input options

OptionTypeDefaultDescription
urlstringSingular name of the input. e.g. /cepThe route URL that CEP will be fetched from.
fieldsarray[:street, :state, :city, :neighborhood]The inputs names which will be auto completed.

Custom fields

If you want to add any custom field to be autocompleted, just do the follow:

1. Add to the CEP input options

f.input :cep, as: :cep, fields: [:state_id]
f.input :state_id

2. Add the render method to the controller

ActiveAdmin.register_page 'CEP' do
  setup_cep_auto_complete do
    # New field
    field :state_id do |cep|
      state = State.where("title LIKE '%?%'", cep.state).take
      
      if state.any?
        state.id
      end
    end
    
    # Another new field that uses a result from other one
    field :some_other_field do |cep|
      "State number #{cep.state_id}"
    end
    
    # Override an original field
    field :street do |cep|
      "Street #{cep.street}"
    end
  end
end

3. Now, the javascript will auto trigger the field with something like this:

$('#address_state_id').val(cep.state_id);

p.s. it just a pseudo-code

If you want support a different plugin (like select2) or any other javascript render method, you can do:

$('#address_state_id').on('active_admin:cep_auto_complete', function(e, value, cep, input) {
  if (value) {
    $(this).val(value);
    $(this).trigger('change');
  }
});

Maintainer

Dhyego Fernando