Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use activerecord reorder method instead of order #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

Find records in the same order of input array.

Forked from [khiav223577/find_with_order](https://github.com/khiav223577/find_with_order)
Copy link
Owner

@khiav223577 khiav223577 Jun 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes made to README.md is not suitable for this PR.


## Installation

Add this line to your application's Gemfile:

```ruby
gem 'find_with_order'
gem 'find_with_order', git: 'git://github.com/KRaikk/find_with_order.git'
```

And then execute:
Expand Down
6 changes: 3 additions & 3 deletions lib/find_with_order/mysql_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module FindWithOrder::MysqlSupport
class << self
def find_with_order(relation, ids)
relation.where(id: ids)
.order("field(#{relation.table_name}.id, #{ids.join(',')})")
.reorder("field(#{relation.table_name}.id, #{ids.join(',')})")
.to_a
end

Expand All @@ -16,8 +16,8 @@ def with_order(relation, column, ids, null_first: false)
else
column = column.to_s
end
return relation.order("field(#{column}, #{ids.map(&:inspect).join(',')})") if null_first
return relation.order("field(#{column}, #{ids.reverse.map(&:inspect).join(',')}) DESC")
return relation.reorder("field(#{column}, #{ids.map(&:inspect).join(',')})") if null_first
return relation.reorder("field(#{column}, #{ids.reverse.map(&:inspect).join(',')}) DESC")
end
end
end
6 changes: 3 additions & 3 deletions lib/find_with_order/pg_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def find_with_order(relation, ids)
# return relation.where(id: ids).order("array_position(ARRAY[#{ids.join(',')}], #{relation.table_name}.id)").to_a #array_position is only support in PG >= 9.5
return relation.where(id: ids)
.joins("JOIN (SELECT id.val, row_number() over() FROM (VALUES(#{ids.join('),(')})) AS id(val)) AS id ON (#{relation.table_name}.id = id.val)")
.order('row_number')
.reorder('row_number')
end

def where_with_order(relation, column, ids)
Expand All @@ -18,6 +18,7 @@ def with_order(relation, column, ids, null_first: false)
column = column.to_s
end
ids = ids.reverse if null_first
ids.reject!(&:blank?)
case ids.first
when Numeric
values = ids.join('),(')
Expand All @@ -29,8 +30,7 @@ def with_order(relation, column, ids, null_first: false)
else
raise "not support type: #{ids.first.class}"
end
return relation.joins("LEFT JOIN (SELECT id.val, row_number() over() FROM (VALUES(#{values})) AS id(val)) AS id ON (#{column} = id.val)")
.order(null_first ? 'row_number DESC' : 'row_number')
return relation.joins("LEFT JOIN (SELECT id.val, row_number() over() FROM (VALUES(#{values})) AS id(val)) AS id ON (#{column} = id.val)").reorder(null_first ? 'row_number DESC' : 'row_number')
end
end
end