Skip to content

Commit dd5c512

Browse files
authored
Merge pull request #2 from tf/pagination-base
Use zero based pagination parameter
2 parents 647bf36 + 81817e3 commit dd5c512

File tree

11 files changed

+89
-58
lines changed

11 files changed

+89
-58
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'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//= require select2
2+
3+
//= require_tree ./searchable_select

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

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
(function() {
2+
function initSearchableSelects(inputs, extra) {
3+
inputs.each(function() {
4+
var item = $(this);
5+
6+
// reading from data allows <input data-searchable_select='{"tags": ['some']}'>
7+
// to be passed to select2
8+
var options = $.extend(extra || {}, item.data('searchableSelect'));
9+
var url = item.data('ajaxUrl');
10+
11+
if (url) {
12+
$.extend(options, {
13+
ajax: {
14+
url: url,
15+
dataType: 'json',
16+
17+
data: function (params) {
18+
return {
19+
term: params.term,
20+
page: pageParamWithBaseZero(params)
21+
};
22+
}
23+
}
24+
});
25+
}
26+
27+
item.select2(options);
28+
});
29+
}
30+
31+
function pageParamWithBaseZero(params) {
32+
return params.page ? params.page - 1 : undefined;
33+
}
34+
35+
$(document).on('has_many_add:after', '.has_many_container', function(e, fieldset) {
36+
initSearchableSelects(fieldset.find('.searchable-select-input'));
37+
});
38+
39+
$(document).on('page:load turbolinks:load', function() {
40+
initSearchableSelects($(".searchable-select-input"), {placeholder: ""});
41+
});
42+
43+
$(function() {
44+
initSearchableSelects($(".searchable-select-input"));
45+
});
46+
}());

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

Lines changed: 0 additions & 29 deletions
This file was deleted.

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

0 commit comments

Comments
 (0)