activerecord-sort 5.2.0 → 6.1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +22 -0
- data/README.md +26 -15
- data/lib/active_record/sort.rb +1 -5
- data/lib/active_record/sort/version.rb +1 -1
- metadata +53 -17
- data/ext/active_record/base.rb +0 -123
- data/ext/arel/nodes/ascending.rb +0 -14
- data/ext/arel/nodes/descending.rb +0 -15
- data/ext/arel/nodes/random.rb +0 -7
- data/ext/arel/order_predications.rb +0 -14
- data/ext/arel/visitors/postgresql.rb +0 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24b933e7b9456dfe06da3a8da7a20ad8bf7b03043c81667021dbdafc8f6b9cf4
|
4
|
+
data.tar.gz: 77347b9a5f294de607d485598b2c41305e66b2932debec47da84ded5cca9772d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41524ce760cfbf7f8bdb93bd5688c149e553a277b615d45c240939d8c51541c6d34660ccf8ad681ce78aeececcf86995b62ab20fd10797f5440115b88767acd6
|
7
|
+
data.tar.gz: f2098644c9201456b82899f1b621f8348a140bee47d6db7e4780ca4273b2ee47061771c535905d727a70195d4b20fc3da2c02181396bfcf84c1e645a739ef73c
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Jon Bracy
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
CHANGED
@@ -1,46 +1,57 @@
|
|
1
|
-
# ActiveRecord::
|
1
|
+
# ActiveRecord::Sort
|
2
2
|
|
3
|
-
`ActiveRecord::
|
3
|
+
`ActiveRecord::Sort` provides and easy way to accept user input and order a query by the input.
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
Installation
|
6
|
+
------------
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
Add `sunstone` to your Gemfile and run `bundle`:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'activerecord-sort', require: 'active_record/sort'
|
12
|
+
```
|
13
|
+
|
14
|
+
Or install the gem and require it:
|
15
|
+
|
16
|
+
```sh
|
17
|
+
gem install activerecord-sort
|
18
|
+
irb
|
19
|
+
# => require('active_record/sort')
|
20
|
+
```
|
10
21
|
|
11
22
|
Examples
|
12
23
|
--------
|
13
|
-
`ActiveRecord::
|
24
|
+
`ActiveRecord::Sort` supports the following cases:
|
14
25
|
|
15
26
|
```ruby
|
16
|
-
Property.
|
27
|
+
Property.sort(:id).to_sql
|
17
28
|
# => "...ORDER BY properties.id ASC"
|
18
29
|
|
19
|
-
Property.
|
30
|
+
Property.sort(:id, :name).to_sql
|
20
31
|
# => "...ORDER BY properties.id ASC, properties.name ASC"
|
21
32
|
|
22
|
-
Property.
|
33
|
+
Property.sort(id: :desc).to_sql
|
23
34
|
# => "...ORDER BY properties.id DESC"
|
24
35
|
|
25
|
-
Property.
|
36
|
+
Property.sort(id: {asc: :nulls_first})
|
26
37
|
# => "...ORDER BY properties.id ASC NULLS FIRST"
|
27
38
|
|
28
|
-
Property.
|
39
|
+
Property.sort(id: {asc: :nulls_last})
|
29
40
|
# => "...ORDER BY properties.id ASC NULLS LAST"
|
30
41
|
```
|
31
42
|
|
32
43
|
It can also sort on relations:
|
33
44
|
|
34
45
|
```ruby
|
35
|
-
Property.
|
46
|
+
Property.sort(addresses: :id).to_sql
|
36
47
|
# => "...INNER JOIN addresses ON addresses.property_id = properties.id
|
37
48
|
# => " ORDER BY addresses.id ASC"
|
38
49
|
|
39
|
-
Property.
|
50
|
+
Property.sort(addresses: {id: :desc}).to_sql
|
40
51
|
# => "...INNER JOIN addresses ON addresses.property_id = properties.id
|
41
52
|
# => " ORDER BY addresses.id DESC"
|
42
53
|
|
43
|
-
Property.
|
54
|
+
Property.sort(addresses: {id: {asc: :nulls_frist}}).to_sql
|
44
55
|
# => "...INNER JOIN addresses ON addresses.property_id = properties.id
|
45
56
|
# => " ORDER BY addresses.id ASC NULLS FIRST"
|
46
57
|
```
|
data/lib/active_record/sort.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
1
|
require 'active_record'
|
2
|
+
require 'arel/extensions'
|
2
3
|
|
3
|
-
require File.expand_path(File.join(__FILE__, '../../../ext/arel/order_predications'))
|
4
|
-
require File.expand_path(File.join(__FILE__, '../../../ext/arel/nodes/ascending'))
|
5
|
-
require File.expand_path(File.join(__FILE__, '../../../ext/arel/nodes/descending'))
|
6
|
-
require File.expand_path(File.join(__FILE__, '../../../ext/arel/nodes/random'))
|
7
|
-
require File.expand_path(File.join(__FILE__, '../../../ext/arel/visitors/postgresql'))
|
8
4
|
require File.expand_path(File.join(__FILE__, '../../../ext/active_record/base'))
|
9
5
|
|
10
6
|
ActiveRecord::Querying.delegate :sort, to: :all
|
metadata
CHANGED
@@ -1,29 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-sort
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 6.1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jon Bracy
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 6.1.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 6.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: arel-extensions
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 6.1.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 6.1.0
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: pg
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -136,6 +150,34 @@ dependencies:
|
|
136
150
|
- - ">="
|
137
151
|
- !ruby/object:Gem::Version
|
138
152
|
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: sunstone
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: 6.1.0.2
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: 6.1.0.2
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: webmock
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
139
181
|
description: A safe way to accept user parameters and order against your ActiveRecord
|
140
182
|
Models
|
141
183
|
email:
|
@@ -145,20 +187,15 @@ extensions: []
|
|
145
187
|
extra_rdoc_files:
|
146
188
|
- README.md
|
147
189
|
files:
|
190
|
+
- LICENSE
|
148
191
|
- README.md
|
149
|
-
- ext/active_record/base.rb
|
150
|
-
- ext/arel/nodes/ascending.rb
|
151
|
-
- ext/arel/nodes/descending.rb
|
152
|
-
- ext/arel/nodes/random.rb
|
153
|
-
- ext/arel/order_predications.rb
|
154
|
-
- ext/arel/visitors/postgresql.rb
|
155
192
|
- lib/active_record/sort.rb
|
156
193
|
- lib/active_record/sort/version.rb
|
157
194
|
homepage: https://github.com/malomalo/activerecord-sort
|
158
195
|
licenses:
|
159
196
|
- MIT
|
160
197
|
metadata: {}
|
161
|
-
post_install_message:
|
198
|
+
post_install_message:
|
162
199
|
rdoc_options:
|
163
200
|
- "--main"
|
164
201
|
- README.md
|
@@ -175,9 +212,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
175
212
|
- !ruby/object:Gem::Version
|
176
213
|
version: '0'
|
177
214
|
requirements: []
|
178
|
-
|
179
|
-
|
180
|
-
signing_key:
|
215
|
+
rubygems_version: 3.2.3
|
216
|
+
signing_key:
|
181
217
|
specification_version: 4
|
182
218
|
summary: A safe way to accept user parameters and order against your ActiveRecord
|
183
219
|
Models
|
data/ext/active_record/base.rb
DELETED
@@ -1,123 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
require 'active_record/relation'
|
3
|
-
|
4
|
-
module ActiveRecord
|
5
|
-
module QueryMethods
|
6
|
-
# class << self
|
7
|
-
|
8
|
-
# ordering:
|
9
|
-
# :id
|
10
|
-
# :name, :id
|
11
|
-
# :id => :desc
|
12
|
-
# :id => {:desc => :nulls_last}
|
13
|
-
# :listings => :id
|
14
|
-
# :listings => {:id => {:asc => :nulls_first}}
|
15
|
-
# :random
|
16
|
-
def sort(*ordering)
|
17
|
-
resource = all
|
18
|
-
ordering.compact!
|
19
|
-
ordering.flatten!
|
20
|
-
return resource if ordering.size == 0
|
21
|
-
|
22
|
-
ordering.each do |order|
|
23
|
-
order = Array(order)
|
24
|
-
|
25
|
-
order.each do |column_or_relation, options|
|
26
|
-
if column_or_relation.to_sym == :random
|
27
|
-
resource = resource.random_sort
|
28
|
-
elsif self.column_names.include?(column_or_relation.to_s)
|
29
|
-
resource = resource.sort_for_column(column_or_relation, options)
|
30
|
-
elsif reflect_on_association(column_or_relation.to_sym)
|
31
|
-
resource = resource.select(resource.klass.arel_table[Arel::Nodes::SqlLiteral.new('*')])
|
32
|
-
resource = resource.sort_for_relation(column_or_relation.to_sym, options)
|
33
|
-
else
|
34
|
-
raise ActiveRecord::StatementInvalid.new("Unkown column #{column_or_relation}")
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
resource
|
40
|
-
end
|
41
|
-
|
42
|
-
def random_sort
|
43
|
-
self.order(Arel::Nodes::RandomOrdering.new)
|
44
|
-
end
|
45
|
-
|
46
|
-
def sort_for_column(column, options)
|
47
|
-
column = self.arel_table[column.to_s.underscore]
|
48
|
-
direction = (options.is_a?(Hash) ? options.keys.first.to_sym : options.to_s.downcase.to_sym)
|
49
|
-
|
50
|
-
nulls = (options.is_a?(Hash) ? options.values.first.to_sym : nil)
|
51
|
-
if direction == :desc
|
52
|
-
self.order(Arel::Nodes::Descending.new(column, nulls))
|
53
|
-
elsif direction == :asc || direction == :''
|
54
|
-
self.order(Arel::Nodes::Ascending.new(column, nulls))
|
55
|
-
else
|
56
|
-
raise ActiveRecord::StatementInvalid.new("Unkown ordering #{direction}")
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def sort_for_relation(relation, options)
|
61
|
-
resource = self
|
62
|
-
relation = reflect_on_association(relation)
|
63
|
-
|
64
|
-
if relation.macro == :has_many
|
65
|
-
options = [options] if !options.is_a?(Array)
|
66
|
-
|
67
|
-
options.each do |order|
|
68
|
-
order = Array(order)
|
69
|
-
order.each do |column, options|
|
70
|
-
column = relation.klass.arel_table[column]
|
71
|
-
direction = (options.is_a?(Hash) ? options.keys.first.to_sym : options.to_s.downcase.to_sym)
|
72
|
-
|
73
|
-
nulls = (options.is_a?(Hash) ? options.values.first.to_sym : nil)
|
74
|
-
if direction == :desc
|
75
|
-
# aggregation = Arel::Nodes::Max.new([column], "max_#{relation.name}_#{column.name}")
|
76
|
-
# order = Arel::Nodes::Descending.new(Arel::Nodes::SqlLiteral.new("max_#{relation.name}_#{column.name}"), nulls)
|
77
|
-
|
78
|
-
if relation.options[:through]
|
79
|
-
resource = resource.joins(relation.options[:through] => relation.source_reflection_name)
|
80
|
-
else
|
81
|
-
resource = resource.joins(relation.name)
|
82
|
-
end
|
83
|
-
# resource = resource.select(aggregation)
|
84
|
-
# resource = resource.order(order)
|
85
|
-
resource = resource.order(Arel::Nodes::Descending.new(column, nulls))
|
86
|
-
else
|
87
|
-
# aggregation = Arel::Nodes::Min.new([column], "min_#{relation.name}_#{column.name}")
|
88
|
-
order = Arel::Nodes::Ascending.new(Arel::Nodes::SqlLiteral.new("min_#{relation.name}_#{column.name}"), nulls)
|
89
|
-
|
90
|
-
resource = resource.joins(relation.name)
|
91
|
-
# resource = resource.select(aggregation)
|
92
|
-
# resource = resource.order(order)
|
93
|
-
resource = resource.order(Arel::Nodes::Ascending.new(column, nulls))
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
elsif relation.macro == :belongs_to || relation.macro == :has_one
|
98
|
-
options = [options] if !options.is_a?(Array)
|
99
|
-
|
100
|
-
options.each do |order|
|
101
|
-
order = Array(order)
|
102
|
-
order.each do |column, options|
|
103
|
-
column = relation.klass.arel_table[column]
|
104
|
-
direction = (options.is_a?(Hash) ? options.keys.first.to_sym : options.to_s.downcase.to_sym)
|
105
|
-
|
106
|
-
nulls = (options.is_a?(Hash) ? options.values.first.to_sym : nil)
|
107
|
-
if direction == :asc
|
108
|
-
order = Arel::Nodes::Ascending.new(column, nulls)
|
109
|
-
else
|
110
|
-
order = Arel::Nodes::Descending.new(column, nulls)
|
111
|
-
end
|
112
|
-
|
113
|
-
resource = resource.joins(relation.name)
|
114
|
-
resource = resource.order(order)
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
resource
|
120
|
-
end
|
121
|
-
|
122
|
-
end
|
123
|
-
end
|
data/ext/arel/nodes/ascending.rb
DELETED
data/ext/arel/nodes/random.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
module Arel
|
2
|
-
module Visitors
|
3
|
-
class PostgreSQL
|
4
|
-
private
|
5
|
-
|
6
|
-
def visit_Arel_Nodes_Ascending o, collector
|
7
|
-
case o.nulls
|
8
|
-
when :nulls_first then visit(o.expr, collector) << ' ASC NULLS FIRST'
|
9
|
-
when :nulls_last then visit(o.expr, collector) << ' ASC NULLS LAST'
|
10
|
-
else visit(o.expr, collector) << ' ASC'
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def visit_Arel_Nodes_Descending o, collector
|
15
|
-
case o.nulls
|
16
|
-
when :nulls_first then visit(o.expr, collector) << ' DESC NULLS FIRST'
|
17
|
-
when :nulls_last then visit(o.expr, collector) << ' DESC NULLS LAST'
|
18
|
-
else visit(o.expr, collector) << ' DESC'
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def visit_Arel_Nodes_RandomOrdering o, collector
|
23
|
-
collector << "RANDOM()"
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|