Skip to content

Commit 5f2129b

Browse files
authored
Merge pull request #11 from jstumbaugh/allow-for-multi-select-filters
Allow for multi-select filters
2 parents d53b246 + 3ca195c commit 5f2129b

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ This also works for filters:
5050
end
5151
```
5252
53+
By default, you can only select one at a time for a filter. You can
54+
specify a multi-select with:
55+
56+
```ruby
57+
ActiveAdmin.register Product do
58+
filter(:category, as: :searchable_select, multiple: true)
59+
end
60+
```
61+
5362
### Fetching Options via Ajax
5463
5564
For large collections, rendering the whole set of options can be to

lib/activeadmin/searchable_select/select_input_extension.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,23 @@ def all_options_collection
5757
end
5858

5959
def selected_value_collection
60-
[selected_value_option].compact
61-
end
62-
63-
def selected_value_option
64-
option_for_record(selected_record) if selected_record
60+
selected_records.collect { |s| option_for_record(s) }
6561
end
6662

6763
def option_for_record(record)
6864
[option_collection.display_text(record), record.id]
6965
end
7066

71-
def selected_record
72-
@selected_record ||=
73-
selected_value && option_collection_scope.find_by_id(selected_value)
67+
def selected_records
68+
@selected_records ||=
69+
if selected_values
70+
option_collection_scope.where(id: selected_values)
71+
else
72+
[]
73+
end
7474
end
7575

76-
def selected_value
76+
def selected_values
7777
@object.send(input_name) if @object
7878
end
7979

spec/features/filter_input_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,54 @@
188188
text: 'Travel')
189189
end
190190
end
191+
192+
describe 'with the multiple option set to true' do
193+
before(:each) do
194+
ActiveAdminHelpers.setup do
195+
ActiveAdmin.register(Category) do
196+
searchable_select_options(scope: Category, text_attribute: :name)
197+
end
198+
199+
ActiveAdmin.register(Post) do
200+
filter(:category,
201+
as: :searchable_select,
202+
ajax: true,
203+
multiple: true)
204+
end
205+
end
206+
end
207+
208+
it 'renders select input with searchable-select-input css class and the multiple attribute' do
209+
get '/admin/posts'
210+
211+
expect(response.body).to have_selector("select.searchable-select-input[multiple='multiple']")
212+
end
213+
214+
it 'does not render options statically' do
215+
Category.create!(name: 'Travel')
216+
217+
get '/admin/posts'
218+
219+
expect(response.body).not_to have_selector('.searchable-select-input option',
220+
text: 'Travel')
221+
end
222+
223+
it 'sets data-ajax-url attribute' do
224+
get '/admin/posts'
225+
226+
expect(response.body).to have_selector('.searchable-select-input[data-ajax-url]')
227+
end
228+
229+
it 'renders the filter for multiple values selected' do
230+
category1 = Category.create!(name: 'Travel')
231+
category2 = Category.create!(name: 'Leisure')
232+
233+
get "/admin/posts?q[category_id_in][]=#{category1.id}&q[category_id_in][]=#{category2.id}"
234+
235+
expect(response.body).to have_selector('.searchable-select-input option[selected]',
236+
text: 'Travel')
237+
expect(response.body).to have_selector('.searchable-select-input option[selected]',
238+
text: 'Leisure')
239+
end
240+
end
191241
end

0 commit comments

Comments
 (0)