Skip to content

Commit

Permalink
Merge pull request #1223 from waldyr/sorting-on-alias
Browse files Browse the repository at this point in the history
Enable sort using alias
  • Loading branch information
scarroll32 authored Jul 6, 2021
2 parents 00d3c25 + a458c8f commit 16ce911
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 23 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,25 @@ query parameters in your URLs.
<% end %>
```

You can also use `ransack_alias` for sorting.

```ruby
class Post < ActiveRecord::Base
belongs_to :author

# Abbreviate :author_first_name to :author
ransack_alias :author, :author_first_name
end
```

Now, you can use `:author` instead of `:author_first_name` in a `sort_link`.

```erb
<%= sort_link(@q, :author) %>
```

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.

### Search Matchers

List of all possible predicates
Expand Down
4 changes: 2 additions & 2 deletions lib/ransack/nodes/sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def valid?
end

def name=(name)
@name = name
context.bind(self, name)
@name = context.ransackable_alias(name) || name
context.bind(self, @name)
end

def dir=(dir)
Expand Down
69 changes: 48 additions & 21 deletions spec/ransack/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -483,82 +483,109 @@ def remove_quotes_and_backticks(str)
expect(sort.dir).to eq 'asc'
end

it 'creates sorts based on multiple attributes/directions in array format' do
@s.sorts = ['id desc', { name: 'name', dir: 'asc' }]
it 'creates sorts based on a single alias/direction' do
@s.sorts = 'daddy desc'
expect(@s.sorts.size).to eq(1)
sort = @s.sorts.first
expect(sort).to be_a Nodes::Sort
expect(sort.name).to eq 'parent_name'
expect(sort.dir).to eq 'desc'
end

it 'creates sorts based on a single alias and uppercase direction' do
@s.sorts = 'daddy DESC'
expect(@s.sorts.size).to eq(1)
sort = @s.sorts.first
expect(sort).to be_a Nodes::Sort
expect(sort.name).to eq 'parent_name'
expect(sort.dir).to eq 'desc'
end

it 'creates sorts based on a single alias and without direction' do
@s.sorts = 'daddy'
expect(@s.sorts.size).to eq(1)
sort = @s.sorts.first
expect(sort).to be_a Nodes::Sort
expect(sort.name).to eq 'parent_name'
expect(sort.dir).to eq 'asc'
end

it 'creates sorts based on attributes, alias and directions in array format' do
@s.sorts = ['id desc', { name: 'daddy', dir: 'asc' }]
expect(@s.sorts.size).to eq(2)
sort1, sort2 = @s.sorts
expect(sort1).to be_a Nodes::Sort
expect(sort1.name).to eq 'id'
expect(sort1.dir).to eq 'desc'
expect(sort2).to be_a Nodes::Sort
expect(sort2.name).to eq 'name'
expect(sort2.name).to eq 'parent_name'
expect(sort2.dir).to eq 'asc'
end

it 'creates sorts based on multiple attributes and uppercase directions in array format' do
@s.sorts = ['id DESC', { name: 'name', dir: 'ASC' }]
it 'creates sorts based on attributes, alias and uppercase directions in array format' do
@s.sorts = ['id DESC', { name: 'daddy', dir: 'ASC' }]
expect(@s.sorts.size).to eq(2)
sort1, sort2 = @s.sorts
expect(sort1).to be_a Nodes::Sort
expect(sort1.name).to eq 'id'
expect(sort1.dir).to eq 'desc'
expect(sort2).to be_a Nodes::Sort
expect(sort2.name).to eq 'name'
expect(sort2.name).to eq 'parent_name'
expect(sort2.dir).to eq 'asc'
end

it 'creates sorts based on multiple attributes and different directions
it 'creates sorts based on attributes, alias and different directions
in array format' do
@s.sorts = ['id DESC', { name: 'name', dir: nil }]
@s.sorts = ['id DESC', { name: 'daddy', dir: nil }]
expect(@s.sorts.size).to eq(2)
sort1, sort2 = @s.sorts
expect(sort1).to be_a Nodes::Sort
expect(sort1.name).to eq 'id'
expect(sort1.dir).to eq 'desc'
expect(sort2).to be_a Nodes::Sort
expect(sort2.name).to eq 'name'
expect(sort2.name).to eq 'parent_name'
expect(sort2.dir).to eq 'asc'
end

it 'creates sorts based on multiple attributes/directions in hash format' do
it 'creates sorts based on attributes, alias and directions in hash format' do
@s.sorts = {
'0' => { name: 'id', dir: 'desc' },
'1' => { name: 'name', dir: 'asc' }
'1' => { name: 'daddy', dir: 'asc' }
}
expect(@s.sorts.size).to eq(2)
expect(@s.sorts).to be_all { |s| Nodes::Sort === s }
id_sort = @s.sorts.detect { |s| s.name == 'id' }
name_sort = @s.sorts.detect { |s| s.name == 'name' }
daddy_sort = @s.sorts.detect { |s| s.name == 'parent_name' }
expect(id_sort.dir).to eq 'desc'
expect(name_sort.dir).to eq 'asc'
expect(daddy_sort.dir).to eq 'asc'
end

it 'creates sorts based on multiple attributes and uppercase directions
it 'creates sorts based on attributes, alias and uppercase directions
in hash format' do
@s.sorts = {
'0' => { name: 'id', dir: 'DESC' },
'1' => { name: 'name', dir: 'ASC' }
'1' => { name: 'daddy', dir: 'ASC' }
}
expect(@s.sorts.size).to eq(2)
expect(@s.sorts).to be_all { |s| Nodes::Sort === s }
id_sort = @s.sorts.detect { |s| s.name == 'id' }
name_sort = @s.sorts.detect { |s| s.name == 'name' }
daddy_sort = @s.sorts.detect { |s| s.name == 'parent_name' }
expect(id_sort.dir).to eq 'desc'
expect(name_sort.dir).to eq 'asc'
expect(daddy_sort.dir).to eq 'asc'
end

it 'creates sorts based on multiple attributes and different directions
it 'creates sorts based on attributes, alias and different directions
in hash format' do
@s.sorts = {
'0' => { name: 'id', dir: 'DESC' },
'1' => { name: 'name', dir: nil }
'1' => { name: 'daddy', dir: nil }
}
expect(@s.sorts.size).to eq(2)
expect(@s.sorts).to be_all { |s| Nodes::Sort === s }
id_sort = @s.sorts.detect { |s| s.name == 'id' }
name_sort = @s.sorts.detect { |s| s.name == 'name' }
daddy_sort = @s.sorts.detect { |s| s.name == 'parent_name' }
expect(id_sort.dir).to eq 'desc'
expect(name_sort.dir).to eq 'asc'
expect(daddy_sort.dir).to eq 'asc'
end

it 'overrides existing sort' do
Expand Down

0 comments on commit 16ce911

Please sign in to comment.