Skip to content

Commit

Permalink
preserve params on clearfilter
Browse files Browse the repository at this point in the history
  • Loading branch information
b264 committed Dec 5, 2014
1 parent 1b74fb7 commit 0c5ea62
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ group :test do
gem 'i18n-spec'
gem 'shoulda-matchers'
gem 'sqlite3'
gem 'poltergeist'
end
4 changes: 3 additions & 1 deletion app/assets/javascripts/active_admin/application.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ $ ->

# Clear Filters button
$('.clear_filters_btn').click ->
window.location.search = ''
params = window.location.search.split('&')
regex = /^(q\[|q%5B|q%5b|page|commit)/
window.location.search = (param for param in params when not param.match(regex)).join('&')

# Batch Actions dropdown
$('.dropdown_button').popover()
Expand Down
23 changes: 23 additions & 0 deletions features/index/filters.feature
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,29 @@ Feature: Index Filtering
And I should see "Non-Fiction" within ".index_table"
And I should not see "Mystery" within ".index_table"

@javascript
Scenario: Clearing filter preserves custom parameters
Given a category named "Mystery" exists
And 1 post with the title "Hello World" written by "Jane Doe" in category "Non-Fiction" exists
And 1 post with the title "Lorem Ipsum" written by "Joe Smith" in category "Mystery" exists
And an index configuration of:
"""
ActiveAdmin.register Category
ActiveAdmin.application.favicon = false
"""
Then I should see "Displaying all 2 Categories"
When I add parameter "scope" with value "all" to the URL
And I add parameter "foo" with value "bar" to the URL
And I select "Hello World" from "Posts"
And I press "Filter"
Then I should see "Non-Fiction"
And I should not see "Mystery"
When I click "Clear Filters"
Then I should see "Non-Fiction"
And I should see "Mystery"
And I should have parameter "foo" with value "bar"
And I should have parameter "scope" with value "all"

Scenario: Checkboxes - Filtering categories via posts written by anyone
Given a category named "Mystery" exists
And a post with the title "Hello World" written by "Jane Doe" in category "Non-Fiction" exists
Expand Down
30 changes: 19 additions & 11 deletions features/step_definitions/filter_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@
end
end

Then(/^I should( not)? see parameter "([^"]*)" with value "([^"]*)"$/) do |negative, key, value|
uri_with_params= page.current_url.split('?')
params_string= (uri_with_params.length == 2) ? uri_with_params[1]: nil
expect(params_string).to_not be_nil
params= Hash.new
params_string.split('&').each do |pair|
params[pair.split('=')[0]]= pair.split('=')[1]
end
if params != nil
negative ? (expect(params[key]).to be_falsey)
: (expect(params[key]).to be_truthy) && (expect(params[key]).to eq(value))
Given(/^I add parameter "([^"]*)" with value "([^"]*)" to the URL$/) do |key, value|
url = page.current_url
separator = url.include?('?') ? '&' : '?'
visit url + separator + key.to_s + '=' + value.to_s
end

Then(/^I should( not)? have parameter "([^"]*)"( with value "([^"]*)")?$/) do |negative, key, compare_val, value|
query = URI(page.current_url).query
if query.nil?
expect(negative).to be_truthy
else
params = Rack::Utils.parse_query query
if compare_val
expect(params[key]).to_not eq value if negative
expect(params[key]).to eq value unless negative
else
expect(params[key]).to be_nil if negative
expect(params[key]).to be_present unless negative
end
end
end
4 changes: 4 additions & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
require 'capybara/rails'
require 'capybara/cucumber'
require 'capybara/session'
require 'capybara/poltergeist'

Capybara.javascript_driver = :poltergeist

# Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
# order to ease the transition to Capybara we set the default here. If you'd
# prefer to use XPath just remove this line and adjust any selectors in your
Expand Down

0 comments on commit 0c5ea62

Please sign in to comment.