Skip to content

Commit ea4d0c8

Browse files
Allow setting empty sorts (#1549)
Co-authored-by: Greg Molnar <[email protected]>
1 parent b6fa3f8 commit ea4d0c8

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

lib/ransack/nodes/sort.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Sort < Node
88

99
class << self
1010
def extract(context, str)
11-
return unless str
11+
return if str.blank?
1212
attr, direction = str.split(/\s+/, 2)
1313
self.new(context).build(name: attr, dir: direction)
1414
end

lib/ransack/search.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def sorts=(args)
6969
else
7070
sort = Nodes::Sort.extract(@context, sort)
7171
end
72-
self.sorts << sort
72+
self.sorts << sort if sort
7373
end
7474
when Hash
7575
args.each do |index, attrs|

spec/ransack/search_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,11 @@ def remove_quotes_and_backticks(str)
479479
@s = Search.new(Person)
480480
end
481481

482+
it 'doesn\'t creates sorts' do
483+
@s.sorts = ''
484+
expect(@s.sorts.size).to eq(0)
485+
end
486+
482487
it 'creates sorts based on a single attribute/direction' do
483488
@s.sorts = 'id desc'
484489
expect(@s.sorts.size).to eq(1)

0 commit comments

Comments
 (0)