Skip to content

Commit

Permalink
Deprecate passing two trailing hashes to sort_link (#1289)
Browse files Browse the repository at this point in the history
Pass a single options hash instead.
  • Loading branch information
deivid-rodriguez authored Mar 24, 2022
1 parent 2e6672b commit 7ad9544
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
3 changes: 3 additions & 0 deletions lib/ransack/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ def html_options(args)
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

Expand Down
28 changes: 17 additions & 11 deletions spec/ransack/helpers/form_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -739,17 +739,23 @@ module Helpers
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
Expand Down

0 comments on commit 7ad9544

Please sign in to comment.