From f5160c1b4b05d8b9f0874add91c9560ed54ff7c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Mon, 7 Mar 2022 18:45:09 +0100 Subject: [PATCH] Fix `sort_link` with `:class` option. It was being ignored (rather, added to the query string), instead of being added to the link's class option. --- CHANGELOG.md | 7 +++++++ lib/ransack/helpers/form_helper.rb | 7 ++++++- spec/ransack/helpers/form_helper_spec.rb | 24 ++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b216680..8731df2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ ## Unreleased +* Fix `:class` option to `sort_link` not being passed to the generated link + correctly when no additional options are passed. For example: + + ```ruby + sort_link(@q, :bussiness_name, class: "foo") + ``` + ## 2.6.0 - 2022-03-08 * Fix regression when joining a table with itself. diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index fa8f82a..4f4e8e1 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -135,7 +135,12 @@ module Ransack end def html_options(args) - html_options = extract_options_and_mutate_args!(args) + if args.empty? + html_options = @options + else + html_options = extract_options_and_mutate_args!(args) + end + html_options.merge( class: [['sort_link'.freeze, @current_dir], html_options[:class]] .compact.join(' '.freeze) diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index 0b1d338..82bf63f 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -726,6 +726,30 @@ module Ransack it { should match /Block label ▼/ } end + describe '#sort_link with class option' do + subject { @controller.view_context + .sort_link( + [:main_app, Person.ransack(sorts: ['name desc'])], + :name, + class: 'people', controller: 'people' + ) + } + it { should match /class="sort_link desc people"/ } + 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"/ } + end + describe '#search_form_for with default format' do subject { @controller.view_context .search_form_for(Person.ransack) {} }