From 7ad95448dd518f5a96a0e18c25623f3f90d612b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Thu, 24 Mar 2022 22:21:10 +0100 Subject: [PATCH] Deprecate passing two trailing hashes to `sort_link` (#1289) Pass a single options hash instead. --- CHANGELOG.md | 8 +++++++ lib/ransack/helpers/form_helper.rb | 3 +++ spec/ransack/helpers/form_helper_spec.rb | 28 ++++++++++++++---------- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8731df2..cf9dbcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## Unreleased +* Deprecate passing two trailing hashes to `sort_link`, for example: + + ```ruby + sort_link(@q, :bussiness_name, "bussines_name", {}, class: "foo") + ``` + + Pass a single hash with all options instead. + * Fix `:class` option to `sort_link` not being passed to the generated link correctly when no additional options are passed. For example: diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index a251b5f..e91cb38 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -138,6 +138,9 @@ module Ransack if args.empty? html_options = @options else + deprecation_message = "Passing two trailing hashes to `sort_link` is deprecated, merge the trailing hashes into a single one." + caller_location = caller_locations(2, 2).first + warn "#{deprecation_message} (called at #{caller_location.path}:#{caller_location.lineno})" html_options = extract_options_and_mutate_args!(args) end diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index 79f3418..d794328 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -739,17 +739,23 @@ module Ransack end describe '#sort_link with class option workaround' do - subject { @controller.view_context - .sort_link( - [:main_app, Person.ransack(sorts: ['name desc'])], - :name, - 'name', - { controller: 'people' }, - class: 'people' - ) - } - it { should match /class="sort_link desc people"/ } - it { should_not match /people\?class=people/ } + it "generates a correct link and prints a deprecation" do + expect do + link = @controller.view_context + .sort_link( + [:main_app, Person.ransack(sorts: ['name desc'])], + :name, + 'name', + { controller: 'people' }, + class: 'people' + ) + + expect(link).to match(/class="sort_link desc people"/) + expect(link).not_to match(/people\?class=people/) + end.to output( + /Passing two trailing hashes to `sort_link` is deprecated, merge the trailing hashes into a single one\. \(called at #{Regexp.escape(__FILE__)}:/ + ).to_stderr + end end describe '#search_form_for with default format' do