From 0c5ea6223ff21198631a02726b037c6860e4a3da Mon Sep 17 00:00:00 2001 From: B Date: Tue, 25 Nov 2014 04:52:28 -0600 Subject: [PATCH] preserve params on clearfilter --- Gemfile | 1 + .../active_admin/application.js.coffee | 4 ++- features/index/filters.feature | 23 ++++++++++++++ features/step_definitions/filter_steps.rb | 30 ++++++++++++------- features/support/env.rb | 4 +++ 5 files changed, 50 insertions(+), 12 deletions(-) diff --git a/Gemfile b/Gemfile index 0ec2c485606..76ac76250fb 100644 --- a/Gemfile +++ b/Gemfile @@ -50,4 +50,5 @@ group :test do gem 'i18n-spec' gem 'shoulda-matchers' gem 'sqlite3' + gem 'poltergeist' end diff --git a/app/assets/javascripts/active_admin/application.js.coffee b/app/assets/javascripts/active_admin/application.js.coffee index 00b77647e5c..8fa2a135bd0 100644 --- a/app/assets/javascripts/active_admin/application.js.coffee +++ b/app/assets/javascripts/active_admin/application.js.coffee @@ -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() diff --git a/features/index/filters.feature b/features/index/filters.feature index dae1ef4aee5..3c80f0bebf2 100644 --- a/features/index/filters.feature +++ b/features/index/filters.feature @@ -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 diff --git a/features/step_definitions/filter_steps.rb b/features/step_definitions/filter_steps.rb index 361460d77fc..02e91f20008 100644 --- a/features/step_definitions/filter_steps.rb +++ b/features/step_definitions/filter_steps.rb @@ -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 diff --git a/features/support/env.rb b/features/support/env.rb index 5604b9ecb75..872f2836976 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -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