Skip to content

Commit a8670f2

Browse files
committed
docs(readme-and-docs): change search and nested to use association name
1 parent 6dba69e commit a8670f2

File tree

3 files changed

+69
-67
lines changed

3 files changed

+69
-67
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,13 @@ filter :created_at, as: :date_time_picker_filter
225225
You can use the ajax select input to filter values on index view like this:
226226

227227
```ruby
228-
filter :category_id, as: :search_select_filter
228+
filter :category, as: :search_select_filter
229229
```
230230

231231
<img src="./docs/images/filter-search-select.png" height="160" />
232232

233+
[Read more!](docs/slim-select_search.md#filter-usage)
234+
233235
### Themes
234236

235237
#### NO Theme

docs/slim-select_nested_select.md

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ end
3636
to enable nested select functionality, you we'll need to do the following:
3737

3838
```ruby
39-
f.input :city_id, as: :nested_select,
40-
level_1: { attribute: :country_id },
41-
level_2: { attribute: :region_id },
42-
level_3: { attribute: :city_id }
39+
f.input :city, as: :nested_select,
40+
level_1: { attribute: :country },
41+
level_2: { attribute: :region },
42+
level_3: { attribute: :city }
4343
```
4444

4545
<img src="./images/slim-select-nested-select.gif" />
@@ -48,39 +48,39 @@ By default, the nested select input uses the index action of the different level
4848
It's not mandatory to register the ActiveAdmin pages. But, if you don't, you'll need to pass the `url` attribute, on each level, to make it work.
4949

5050
```ruby
51-
f.input :city_id, as: :nested_select,
52-
level_1: {
53-
attribute: :country_id,
54-
url: '/api/countries'
55-
},
56-
level_2: {
57-
attribute: :region_id,
58-
url: '/api/regions'
59-
},
60-
level_3: {
61-
attribute: :city_id,
62-
url: '/api/cities'
63-
}
51+
f.input :city, as: :nested_select,
52+
level_1: {
53+
attribute: :country,
54+
url: '/api/countries'
55+
},
56+
level_2: {
57+
attribute: :region,
58+
url: '/api/regions'
59+
},
60+
level_3: {
61+
attribute: :city,
62+
url: '/api/cities'
63+
}
6464
```
6565

6666
> Remember: those custom endpoints need to work with Ransack filters.
6767
6868
Another option is to pass the `collection` attribute. If you set this, the `url` attribute will be ignored (no ajax request will be executed) and you will work with preloaded collections.
6969

7070
```ruby
71-
f.input :city_id, as: :nested_select,
72-
level_1: {
73-
attribute: :country_id,
74-
collection: Country.all
75-
},
76-
level_2: {
77-
attribute: :region_id,
78-
collection: Region.active
79-
},
80-
level_3: {
81-
attribute: :city_id,
82-
collection: City.all
83-
}
71+
f.input :city, as: :nested_select,
72+
level_1: {
73+
attribute: :country,
74+
collection: Country.all
75+
},
76+
level_2: {
77+
attribute: :region,
78+
collection: Region.active
79+
},
80+
level_3: {
81+
attribute: :city,
82+
collection: City.all
83+
}
8484
```
8585

8686
### Options
@@ -94,38 +94,38 @@ Nested select, allows you to customize the general behavior of the input:
9494
* `predicate`: **(optional)** You can change the default [ransack predicate](https://github.com/activerecord-hackery/ransack#search-matchers). It **defaults to** `contains`
9595

9696
```ruby
97-
f.input :city_id, as: :nested_select,
98-
fields: [:name, :id],
99-
display_name: :id,
100-
minimum_input_length: 1,
101-
level_1: { attribute: :country_id },
102-
level_2: { attribute: :region_id },
103-
level_3: { attribute: :city_id }
97+
f.input :city, as: :nested_select,
98+
fields: [:name, :id],
99+
display_name: :id,
100+
minimum_input_length: 1,
101+
level_1: { attribute: :country },
102+
level_2: { attribute: :region },
103+
level_3: { attribute: :city }
104104
```
105105

106106
<img src="./images/slim-select-nested-select-general-options.gif" />
107107

108108
Also, you can redefine general options on each level.
109109

110110
```ruby
111-
f.input :city_id, as: :nested_select,
112-
fields: [:name],
113-
display_name: :name,
114-
minimum_input_length: 0,
115-
level_1: {
116-
attribute: :country_id,
117-
minimum_input_length: 3,
118-
url: '/api/countries',
119-
response_root: 'paises'
120-
},
121-
level_2: {
122-
attribute: :region_id,
123-
display_name: :id,
124-
},
125-
level_3: {
126-
attribute: :city_id,
127-
fields: [:name, :information]
128-
}
111+
f.input :city, as: :nested_select,
112+
fields: [:name],
113+
display_name: :name,
114+
minimum_input_length: 0,
115+
level_1: {
116+
attribute: :country,
117+
minimum_input_length: 3,
118+
url: '/api/countries',
119+
response_root: 'paises'
120+
},
121+
level_2: {
122+
attribute: :region,
123+
display_name: :id,
124+
},
125+
level_3: {
126+
attribute: :city,
127+
fields: [:name, :information]
128+
}
129129
```
130130

131131
> `response_root` is not valid as general configuration. You need to define this attribute by level.
@@ -135,16 +135,16 @@ f.input :city_id, as: :nested_select,
135135
If you are using ajax search, you can define custom filters. For example, if you have:
136136

137137
```ruby
138-
f.input :city_id, as: :nested_select,
139-
level_1: { attribute: :country_id },
140-
level_2: {
141-
attribute: :region_id,
142-
filters: { name_contains: "Cuy", id_gt: 22 }
143-
},
144-
level_3: { attribute: :city_id }
138+
f.input :city, as: :nested_select,
139+
level_1: { attribute: :country },
140+
level_2: {
141+
attribute: :region,
142+
filters: { name_contains: "Cuy", id_gt: 22 }
143+
},
144+
level_3: { attribute: :city }
145145
```
146146

147-
After select a country, the regions will be filtered by:
147+
After selecting a country, the regions will be filtered by:
148148

149149
* The selected country id.
150150
* The text entered in the region's input.

docs/slim-select_search.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
To enable Slim Select ajax search functionality you need to do the following:
66

77
```ruby
8-
f.input :category_id, as: :search_select, url: admin_categories_path,
8+
f.input :category, as: :search_select, url: admin_categories_path,
99
fields: [:name, :description], display_name: 'name', minimum_input_length: 2,
1010
order_by: 'description_asc'
1111
```
@@ -18,7 +18,7 @@ To use on filters you need to add `as: :search_select_filter` with same options.
1818
If you want to use url helpers, use a `proc` like on the example
1919

2020
```ruby
21-
filter :category_id, as: :search_select_filter, url: proc { admin_categories_path },
21+
filter :category, as: :search_select_filter, url: proc { admin_categories_path },
2222
fields: [:name, :description], display_name: 'name', minimum_input_length: 2,
2323
order_by: 'description_asc'
2424
```
@@ -30,10 +30,10 @@ In case you need to filter with an attribute of another table you need to includ
3030
fields: [:name, :email], display_name: 'email', minimum_input_length: 2,
3131
order_by: 'description_asc', method_model: User
3232
```
33+
Note that in this case you need to use the `id` instead of the name of the association, just like you would do in a normal filter that uses an attribute of an association.
3334

3435
### Options
3536

36-
* `category_id`: Notice we're using the relation field name, not the relation itself, so you can't use `f.input :category`.
3737
* `url`: This is the URL where to get the results. This URL expects an activeadmin collection Url (like the index action) or anything that uses [ransack](https://github.com/activerecord-hackery/ransack) search gem.
3838
* `response_root`: **(optional)** If a request to `url` responds with root, you can indicate the name of that root with this attribute. By default, the gem will try to infer the root from url. For example: if `url` is `GET /admin/categories`, the root will be `categories`. If you have a rootless api, you don't need to worry about this attribute.
3939
* `fields`: an array of field names where to search for matches in the related model (`Category` in this example). If we give many fields, they will be searched with an OR condition.

0 commit comments

Comments
 (0)