Skip to content

Commit 16ce911

Browse files
authored
Merge pull request #1223 from waldyr/sorting-on-alias
Enable sort using alias
2 parents 00d3c25 + a458c8f commit 16ce911

File tree

3 files changed

+69
-23
lines changed

3 files changed

+69
-23
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,25 @@ query parameters in your URLs.
475475
<% end %>
476476
```
477477

478+
You can also use `ransack_alias` for sorting.
479+
480+
```ruby
481+
class Post < ActiveRecord::Base
482+
belongs_to :author
483+
484+
# Abbreviate :author_first_name to :author
485+
ransack_alias :author, :author_first_name
486+
end
487+
```
488+
489+
Now, you can use `:author` instead of `:author_first_name` in a `sort_link`.
490+
491+
```erb
492+
<%= sort_link(@q, :author) %>
493+
```
494+
495+
Note that using `:author_first_name_or_author_last_name_cont` would produce an invalid sql query. In those cases, Ransack ignores the sorting clause.
496+
478497
### Search Matchers
479498

480499
List of all possible predicates

lib/ransack/nodes/sort.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def valid?
3131
end
3232

3333
def name=(name)
34-
@name = name
35-
context.bind(self, name)
34+
@name = context.ransackable_alias(name) || name
35+
context.bind(self, @name)
3636
end
3737

3838
def dir=(dir)

spec/ransack/search_spec.rb

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -483,82 +483,109 @@ def remove_quotes_and_backticks(str)
483483
expect(sort.dir).to eq 'asc'
484484
end
485485

486-
it 'creates sorts based on multiple attributes/directions in array format' do
487-
@s.sorts = ['id desc', { name: 'name', dir: 'asc' }]
486+
it 'creates sorts based on a single alias/direction' do
487+
@s.sorts = 'daddy desc'
488+
expect(@s.sorts.size).to eq(1)
489+
sort = @s.sorts.first
490+
expect(sort).to be_a Nodes::Sort
491+
expect(sort.name).to eq 'parent_name'
492+
expect(sort.dir).to eq 'desc'
493+
end
494+
495+
it 'creates sorts based on a single alias and uppercase direction' do
496+
@s.sorts = 'daddy DESC'
497+
expect(@s.sorts.size).to eq(1)
498+
sort = @s.sorts.first
499+
expect(sort).to be_a Nodes::Sort
500+
expect(sort.name).to eq 'parent_name'
501+
expect(sort.dir).to eq 'desc'
502+
end
503+
504+
it 'creates sorts based on a single alias and without direction' do
505+
@s.sorts = 'daddy'
506+
expect(@s.sorts.size).to eq(1)
507+
sort = @s.sorts.first
508+
expect(sort).to be_a Nodes::Sort
509+
expect(sort.name).to eq 'parent_name'
510+
expect(sort.dir).to eq 'asc'
511+
end
512+
513+
it 'creates sorts based on attributes, alias and directions in array format' do
514+
@s.sorts = ['id desc', { name: 'daddy', dir: 'asc' }]
488515
expect(@s.sorts.size).to eq(2)
489516
sort1, sort2 = @s.sorts
490517
expect(sort1).to be_a Nodes::Sort
491518
expect(sort1.name).to eq 'id'
492519
expect(sort1.dir).to eq 'desc'
493520
expect(sort2).to be_a Nodes::Sort
494-
expect(sort2.name).to eq 'name'
521+
expect(sort2.name).to eq 'parent_name'
495522
expect(sort2.dir).to eq 'asc'
496523
end
497524

498-
it 'creates sorts based on multiple attributes and uppercase directions in array format' do
499-
@s.sorts = ['id DESC', { name: 'name', dir: 'ASC' }]
525+
it 'creates sorts based on attributes, alias and uppercase directions in array format' do
526+
@s.sorts = ['id DESC', { name: 'daddy', dir: 'ASC' }]
500527
expect(@s.sorts.size).to eq(2)
501528
sort1, sort2 = @s.sorts
502529
expect(sort1).to be_a Nodes::Sort
503530
expect(sort1.name).to eq 'id'
504531
expect(sort1.dir).to eq 'desc'
505532
expect(sort2).to be_a Nodes::Sort
506-
expect(sort2.name).to eq 'name'
533+
expect(sort2.name).to eq 'parent_name'
507534
expect(sort2.dir).to eq 'asc'
508535
end
509536

510-
it 'creates sorts based on multiple attributes and different directions
537+
it 'creates sorts based on attributes, alias and different directions
511538
in array format' do
512-
@s.sorts = ['id DESC', { name: 'name', dir: nil }]
539+
@s.sorts = ['id DESC', { name: 'daddy', dir: nil }]
513540
expect(@s.sorts.size).to eq(2)
514541
sort1, sort2 = @s.sorts
515542
expect(sort1).to be_a Nodes::Sort
516543
expect(sort1.name).to eq 'id'
517544
expect(sort1.dir).to eq 'desc'
518545
expect(sort2).to be_a Nodes::Sort
519-
expect(sort2.name).to eq 'name'
546+
expect(sort2.name).to eq 'parent_name'
520547
expect(sort2.dir).to eq 'asc'
521548
end
522549

523-
it 'creates sorts based on multiple attributes/directions in hash format' do
550+
it 'creates sorts based on attributes, alias and directions in hash format' do
524551
@s.sorts = {
525552
'0' => { name: 'id', dir: 'desc' },
526-
'1' => { name: 'name', dir: 'asc' }
553+
'1' => { name: 'daddy', dir: 'asc' }
527554
}
528555
expect(@s.sorts.size).to eq(2)
529556
expect(@s.sorts).to be_all { |s| Nodes::Sort === s }
530557
id_sort = @s.sorts.detect { |s| s.name == 'id' }
531-
name_sort = @s.sorts.detect { |s| s.name == 'name' }
558+
daddy_sort = @s.sorts.detect { |s| s.name == 'parent_name' }
532559
expect(id_sort.dir).to eq 'desc'
533-
expect(name_sort.dir).to eq 'asc'
560+
expect(daddy_sort.dir).to eq 'asc'
534561
end
535562

536-
it 'creates sorts based on multiple attributes and uppercase directions
563+
it 'creates sorts based on attributes, alias and uppercase directions
537564
in hash format' do
538565
@s.sorts = {
539566
'0' => { name: 'id', dir: 'DESC' },
540-
'1' => { name: 'name', dir: 'ASC' }
567+
'1' => { name: 'daddy', dir: 'ASC' }
541568
}
542569
expect(@s.sorts.size).to eq(2)
543570
expect(@s.sorts).to be_all { |s| Nodes::Sort === s }
544571
id_sort = @s.sorts.detect { |s| s.name == 'id' }
545-
name_sort = @s.sorts.detect { |s| s.name == 'name' }
572+
daddy_sort = @s.sorts.detect { |s| s.name == 'parent_name' }
546573
expect(id_sort.dir).to eq 'desc'
547-
expect(name_sort.dir).to eq 'asc'
574+
expect(daddy_sort.dir).to eq 'asc'
548575
end
549576

550-
it 'creates sorts based on multiple attributes and different directions
577+
it 'creates sorts based on attributes, alias and different directions
551578
in hash format' do
552579
@s.sorts = {
553580
'0' => { name: 'id', dir: 'DESC' },
554-
'1' => { name: 'name', dir: nil }
581+
'1' => { name: 'daddy', dir: nil }
555582
}
556583
expect(@s.sorts.size).to eq(2)
557584
expect(@s.sorts).to be_all { |s| Nodes::Sort === s }
558585
id_sort = @s.sorts.detect { |s| s.name == 'id' }
559-
name_sort = @s.sorts.detect { |s| s.name == 'name' }
586+
daddy_sort = @s.sorts.detect { |s| s.name == 'parent_name' }
560587
expect(id_sort.dir).to eq 'desc'
561-
expect(name_sort.dir).to eq 'asc'
588+
expect(daddy_sort.dir).to eq 'asc'
562589
end
563590

564591
it 'overrides existing sort' do

0 commit comments

Comments
 (0)