Skip to content

Commit 2d6a55d

Browse files
author
James McKinney
committed
Merge pull request activeadmin#1611 from tinynumbers/index-filter-page-fix
Added fix for issue activeadmin#1610 - filtering the index when not on the first page should reset pagination to show the first page of results.
2 parents 1a20a16 + a33bf04 commit 2d6a55d

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

features/index/filters.feature

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ Feature: Index Filtering
3838
And I should not see pagination
3939
And I should see "No Posts found"
4040

41+
Scenario: Filtering posts while not on the first page
42+
Given 9 posts exist
43+
And an index configuration of:
44+
"""
45+
ActiveAdmin.register Post do
46+
config.per_page = 5
47+
end
48+
"""
49+
When I follow "2"
50+
Then I should see "Displaying Posts 6 - 9 of 9 in total"
51+
52+
When I fill in "Search Title" with "Hello World 2"
53+
And I press "Filter"
54+
And I should see 1 posts in the table
55+
And I should see "Hello World 2" within ".index_table"
56+
4157
Scenario: Checkboxes - Filtering posts written by anyone
4258
Given 1 post exists
4359
And a post with the title "Hello World" written by "Jane Doe" exists

lib/active_admin/filters/forms.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def active_admin_filters_form_for(search, filters, options = {})
8080
buttons = content_tag :div, :class => "buttons" do
8181
f.submit(I18n.t('active_admin.filter')) +
8282
clear_link +
83-
hidden_field_tags_for(params, :except => :q)
83+
hidden_field_tags_for(params, :except => [:q, :page])
8484
end
8585

8686
f.form_buffers.last + buttons

lib/active_admin/view_helpers/fields_for.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ module FormHelper
1515
#
1616
def fields_for_params(params, options = {})
1717
namespace = options[:namespace]
18-
except = options[:except]
18+
except = options[:except].is_a?(Array) ? options[:except] : [options[:except]]
1919

2020
params.map do |k, v|
2121
next if namespace.nil? && %w(controller action commit utf8).include?(k.to_s)
22-
next if except && k.to_s == except.to_s
22+
next if except.map(&:to_s).include?(k.to_s)
2323

2424
if namespace
2525
k = "#{namespace}[#{k}]"

spec/unit/view_helpers/fields_for_spec.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
should == [ { :scope => "All" } ]
1515
end
1616

17+
it "should allow an array for the except" do
18+
fields_for_params({:scope => "All", :name => "Greg", :age => "12"}, :except => [:name, :age]).
19+
should == [ { :scope => "All" } ]
20+
end
21+
1722
it "should work with hashes" do
1823
params = fields_for_params(:filters => { :name => "John", :age => "12" })
1924

@@ -29,8 +34,8 @@
2934

3035
it "should work with arrays" do
3136
fields_for_params(:people => ["greg", "emily", "philippe"]).
32-
should == [ { "people[]" => "greg" },
33-
{ "people[]" => "emily" },
37+
should == [ { "people[]" => "greg" },
38+
{ "people[]" => "emily" },
3439
{ "people[]" => "philippe" } ]
3540
end
3641
end

0 commit comments

Comments
 (0)