Skip to content

Commit 81817e3

Browse files
committed
Use zero based pagination parameter
By default, Select2 passes `page=2` to fetch the second page. This is incompatible with the ActiveAdmin's zero based pagination. Pass `data` option to change `page` param. * Add end-to-end spec for loading more options. * Make ajax spec helpers more robust against additional ajax requests. * Include CSS in dummy app, to make measuring work. * Add `sass-rails` dependency to fix Active Admin 1.0 compatibility with `select2-rails`.
1 parent f439a1f commit 81817e3

File tree

8 files changed

+52
-27
lines changed

8 files changed

+52
-27
lines changed

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
source 'https://rubygems.org'
22

3+
# Required for select2-rails to work with Active Admin 1.1
4+
# see https://github.com/activeadmin/activeadmin/issues/5226
5+
gem 'sass-rails'
6+
37
gemspec

activeadmin-searchable_select.gemspec

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# -*- encoding: utf-8 -*-
2-
31
lib = File.expand_path('../lib', __FILE__)
42
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
53
require 'activeadmin/searchable_select/version'

app/assets/javascripts/active_admin/searchable_select/init.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212
$.extend(options, {
1313
ajax: {
1414
url: url,
15-
dataType: 'json'
15+
dataType: 'json',
16+
17+
data: function (params) {
18+
return {
19+
term: params.term,
20+
page: pageParamWithBaseZero(params)
21+
};
22+
}
1623
}
1724
});
1825
}
@@ -21,6 +28,10 @@
2128
});
2229
}
2330

31+
function pageParamWithBaseZero(params) {
32+
return params.page ? params.page - 1 : undefined;
33+
}
34+
2435
$(document).on('has_many_add:after', '.has_many_container', function(e, fieldset) {
2536
initSearchableSelects(fieldset.find('.searchable-select-input'));
2637
});

gemfiles/rails_4.2_active_admin_1.0.0.pre4.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
source "https://rubygems.org"
44

5+
gem "sass-rails"
56
gem "rails", "~> 4.2"
67
gem "activeadmin", "1.0.0.pre4"
78
gem "jquery-ui-rails", "~> 5.0"

gemfiles/rails_5.1_active_admin_1.0.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
source "https://rubygems.org"
44

5+
gem "sass-rails"
56
gem "rails", "~> 5.1"
67
gem "activeadmin", "~> 1.0"
78

gemfiles/rails_5.1_active_admin_1.1.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
source "https://rubygems.org"
44

5+
gem "sass-rails"
56
gem "rails", "~> 5.1"
67
gem "activeadmin", "~> 1.1"
78

spec/features/end_to_end_spec.rb

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,42 @@
2424
end
2525

2626
describe 'index page with searchable select filter' do
27-
before(:each) do
28-
music_category = Category.create(name: 'Music')
29-
travel_category = Category.create(name: 'Travel')
30-
31-
Post.create(title: 'Best songs',
32-
category: music_category)
33-
Post.create(title: 'Best places',
34-
category: travel_category)
35-
end
36-
3727
it 'loads filter input options' do
28+
Category.create(name: 'Music')
29+
Category.create(name: 'Travel')
30+
3831
visit '/admin/posts'
3932

4033
expand_select_box
34+
wait_for_ajax
4135

4236
expect(select_box_items).to eq(%w(Music Travel))
4337
end
4438

4539
it 'allows filtering options by term' do
40+
Category.create(name: 'Music')
41+
Category.create(name: 'Travel')
42+
4643
visit '/admin/posts'
4744

4845
expand_select_box
49-
50-
wait_for_ajax do
51-
enter_search_term('T')
52-
end
46+
enter_search_term('T')
47+
wait_for_ajax
5348

5449
expect(select_box_items).to eq(%w(Travel))
5550
end
51+
52+
it 'loads more items when scrolling down' do
53+
15.times { |i| Category.create(name: "Category #{i}") }
54+
visit '/admin/posts'
55+
56+
expand_select_box
57+
wait_for_ajax
58+
scroll_select_box_list
59+
wait_for_ajax
60+
61+
expect(select_box_items.size).to eq(15)
62+
end
5663
end
5764

5865
def expand_select_box
@@ -63,21 +70,21 @@ def enter_search_term(term)
6370
find('.select2-dropdown input').send_keys(term)
6471
end
6572

73+
def scroll_select_box_list
74+
page.execute_script '$(".select2-container ul").scrollTop(1000)'
75+
end
76+
6677
def select_box_items
6778
all('.select2-dropdown li').map(&:text)
6879
end
6980

70-
def wait_for_ajax(count = 1)
71-
page.execute_script 'window._ajaxCalls = 0'
72-
page.execute_script 'window._ajaxCompleteCounter = function() { window._ajaxCalls += 1; }'
73-
page.execute_script '$(document).ajaxComplete(window._ajaxCompleteCounter)'
74-
75-
yield
76-
77-
sleep(0.5) until finished_all_ajax_requests?(count)
81+
def wait_for_ajax
82+
Timeout.timeout(Capybara.default_max_wait_time) do
83+
loop until finished_all_ajax_requests?
84+
end
7885
end
7986

80-
def finished_all_ajax_requests?(count)
81-
page.evaluate_script('window._ajaxCalls') == count
87+
def finished_all_ajax_requests?
88+
page.evaluate_script('jQuery.active').zero?
8289
end
8390
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
@import "active_admin/mixins";
22
@import "active_admin/base";
3+
4+
@import "active_admin/searchable_select";

0 commit comments

Comments
 (0)