activerecord-sort 6.0.0 → 6.1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 721826eacd144f5e3ea8c88d1206ad82782c55d8017fc32a91f9ffbfac240c2f
4
- data.tar.gz: d58fea006a1db1e5ab6b3df864ee612a3c3383a22ff46676b1ba1d2518c089c7
3
+ metadata.gz: 5ee5bd719bed4e5e0588390935e7b237f58689dc469eab9a4940ee61117ca567
4
+ data.tar.gz: f3f6ea7df7d2d07d8f13acf20cc11bddcd6fa992bbb9572b378695ba6c18d628
5
5
  SHA512:
6
- metadata.gz: d596a620187a4b206a1f49ab8432f534b36b2c18855f5419caa4484f366c1625bd5f39e3a200aa413562ffb1bd9f26a2c60f91aadf205f739e18ab95f2cec8de
7
- data.tar.gz: 4f7fb09964241adbee89d8b5598c0c2efd824fe1d0c32645ce2e0ccd2508fb2482cf37cfa9925d95b112eb7b784185618ff9e02be394eb6782fd2b5f887ca16e
6
+ metadata.gz: f6e95a89312f348f886a61b723d0f138947520e94cb58248b6eeff25cbfdca65d6bfdb34721ec661c2a9a8c6dfb9cc1e7be1cb1f195d7e57850a2a77d1475e60
7
+ data.tar.gz: 7ffe6b076093a14bdfd1b75f6863d5ac17e4bcdcaebf60c8233137637d4f100cb8e5dbaabd2176701db10f9262d2f52bcf7e8865f60c01e0e2be057515f85988
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::sort [![Travis CI](https://travis-ci.org/malomalo/activerecord-sort.svg?branch=master)](https://travis-ci.org/malomalo/activerecord-sort)
1
+ # ActiveRecord::Sort
2
2
 
3
- `ActiveRecord::sort` provides and easy way to accept user input and order a query by the input.
3
+ `ActiveRecord::Sort` provides and easy way to accept user input and order a query by the input.
4
4
 
5
- Installtion
6
- -----------
5
+ Installation
6
+ ------------
7
7
 
8
- - Add `gem 'activerecord-sort', require: 'active_record/sort'
9
- - Run `bundle install`
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::sort` supports the following cases:
24
+ `ActiveRecord::Sort` supports the following cases:
14
25
 
15
26
  ```ruby
16
- Property.order(:id).to_sql
27
+ Property.sort(:id).to_sql
17
28
  # => "...ORDER BY properties.id ASC"
18
29
 
19
- Property.order(:id, :name).to_sql
30
+ Property.sort(:id, :name).to_sql
20
31
  # => "...ORDER BY properties.id ASC, properties.name ASC"
21
32
 
22
- Property.order(:id => :desc).to_sql
33
+ Property.sort(id: :desc).to_sql
23
34
  # => "...ORDER BY properties.id DESC"
24
35
 
25
- Property.order(:id => {:asc => :nulls_first})
36
+ Property.sort(id: {asc: :nulls_first})
26
37
  # => "...ORDER BY properties.id ASC NULLS FIRST"
27
38
 
28
- Property.order(:id => {:asc => :nulls_last})
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.order(:addresses => :id).to_sql
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.order(:addresses => {:id => :desc}).to_sql
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.order(:addresses => {:id => {:asc => :nulls_frist}}).to_sql
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
  ```
@@ -1,10 +1,126 @@
1
1
  require 'active_record'
2
+ require 'active_record/relation'
3
+ require 'arel/extensions'
2
4
 
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
- require File.expand_path(File.join(__FILE__, '../../../ext/active_record/base'))
5
+ module ActiveRecord
6
+ module Sort
9
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(self.arel_table[column_or_relation.to_s], 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
+ # TODO: probably don't need to cast to sym
47
+ def sort_for_column(column, options)
48
+ direction = (options.is_a?(Hash) || options.class.name == "ActionController::Parameters" ? 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 = Arel::Attributes::Relation.new(relation.klass.arel_table[column], relation.name)
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.left_outer_joins(relation.name)
114
+ resource = resource.order(order)
115
+ end
116
+ end
117
+ end
118
+
119
+ resource
120
+ end
121
+
122
+ end
123
+ end
124
+
125
+ ActiveRecord::QueryMethods.prepend(ActiveRecord::Sort)
10
126
  ActiveRecord::Querying.delegate :sort, to: :all
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Sort
3
- VERSION = '6.0.0'
3
+ VERSION = '6.1.0.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-sort
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.0
4
+ version: 6.1.0.2
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: 2019-05-29 00:00:00.000000000 Z
11
+ date: 2021-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,14 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 6.0.0.rc1
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: 6.0.0.rc1
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,8 +212,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
212
  - !ruby/object:Gem::Version
176
213
  version: '0'
177
214
  requirements: []
178
- rubygems_version: 3.0.3
179
- signing_key:
215
+ rubygems_version: 3.2.3
216
+ signing_key:
180
217
  specification_version: 4
181
218
  summary: A safe way to accept user parameters and order against your ActiveRecord
182
219
  Models
@@ -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
@@ -1,14 +0,0 @@
1
- module Arel
2
- module Nodes
3
- class Ascending < Ordering
4
-
5
- attr_accessor :nulls
6
-
7
- def initialize expr, nulls=nil
8
- super(expr)
9
- @nulls = nulls
10
- end
11
-
12
- end
13
- end
14
- end
@@ -1,15 +0,0 @@
1
- module Arel
2
- module Nodes
3
- class Descending < Ordering
4
-
5
- attr_accessor :nulls
6
-
7
- def initialize expr, nulls=nil
8
- super(expr)
9
- @nulls = nulls
10
- end
11
-
12
- end
13
-
14
- end
15
- end
@@ -1,7 +0,0 @@
1
- module Arel
2
- module Nodes
3
- class RandomOrdering < Arel::Nodes::Node
4
-
5
- end
6
- end
7
- end
@@ -1,14 +0,0 @@
1
- # Ordering with :nulls_last, :nulls_first options
2
- module Arel
3
- module OrderPredications
4
-
5
- def asc(nulls=nil)
6
- Nodes::Ascending.new self, nulls
7
- end
8
-
9
- def desc(nulls=nil)
10
- Nodes::Descending.new self, nulls
11
- end
12
-
13
- end
14
- end
@@ -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