Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dhyegocalota/active_admin-cep_auto_complete
Auto complete your addresses fields with Brazilian CEP (zip code) with your ActiveAdmin. :earth_americas:
https://github.com/dhyegocalota/active_admin-cep_auto_complete
activeadmin activeadmin-plugin brazil cep rails ruby
Last synced: 2 days ago
JSON representation
Auto complete your addresses fields with Brazilian CEP (zip code) with your ActiveAdmin. :earth_americas:
- Host: GitHub
- URL: https://github.com/dhyegocalota/active_admin-cep_auto_complete
- Owner: dhyegocalota
- License: mit
- Created: 2017-10-10T15:34:20.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-16T21:12:38.000Z (over 6 years ago)
- Last Synced: 2024-03-19T02:56:05.282Z (8 months ago)
- Topics: activeadmin, activeadmin-plugin, brazil, cep, rails, ruby
- Language: Ruby
- Homepage:
- Size: 58.6 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# ActiveAdmin CEP Auto Complete (brazilian postal code)
Auto complete your addresses fields with Brazilian CEP (zip code).## Installation
Include to your Gemfile
```ruby
gem 'active_admin-cep_auto_complete'
```## Usage
**1. Create a custom page in `app/admin/cep.rb`**
```ruby
ActiveAdmin.register_page 'CEP' do
setup_cep_auto_complete
end
```**2. Add a CEP Input**
```ruby
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](https://github.com/dhyegofernando/active_admin-cep_auto_complete#options).*
**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**
```ruby
f.input :cep, as: :cep, fields: [:state_id]
f.input :state_id
```**2. Add the render method to the controller**
```ruby
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:**
```javascript
$('#address_state_id').val(cep.state_id);
```*p.s. it just a pseudo-code*
**If you want support a different plugin (like [select2](https://github.com/select2/select2)) or any other javascript render method, you can do:**
```javascript
$('#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](https://github.com/dhyegofernando)