No commit activity in last 3 years
No release in over 3 years
Auto complete your addresses fields with Brazilian CEP (zip code).
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 1.0.0.pre1
 Project Readme

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'

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

Option Type Default Description
url string Singular name of the input. e.g. /cep The route URL that CEP will be fetched from.
fields array [: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