activerecord-sort 1.0.0 → 1.0.1

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
  SHA1:
3
- metadata.gz: 9ad6ad083d32b5354b090af30d25fb20c5b43c74
4
- data.tar.gz: 9cbb97bae8eefbae937cf0ebc82154fbae1020e8
3
+ metadata.gz: 64d26616c5be5b175e976284d70e5dcc6b79990b
4
+ data.tar.gz: 7ffa19e030b2e7de432f52187d4dec524a68b96b
5
5
  SHA512:
6
- metadata.gz: 589a24220b64270fc59cb5e75f286fb51858150a451f1f4d464a3c45137c163c0d4e02f64d6946181b77fc41fce2a46cda96db965fb379349c5aaf09c17147bf
7
- data.tar.gz: 63024dc5a854cf4f18ddadc8d5e13e6816ce671c2fcf9a7cdef34ad636b18ed63642330447401bcb750b81885b989bcf0828f6b63dc226aa0e193c6f08c9a4e7
6
+ metadata.gz: 1da137f286a3a2d0005cef33b91534cda3caab256e1cf9dbb105aed2c851f01a81ccb0e2b4104b2daabad1f5ac8726c56efa8b975dc166428c5299807f3ca503
7
+ data.tar.gz: 3ee7ff9be612e95a2906f4a559d3661aba387c238c25409334cc1f8a6290122b2f890103dcf74360a448875afec2a637aced2d448e11fff9f6f635d5e1991885
data/README.md CHANGED
@@ -1,2 +1,46 @@
1
- # activerecord-sort
2
- A safe way to accept user parameters and order against your ActiveRecord Models
1
+ # ActiveRecord::sort
2
+
3
+ `ActiveRecord::sort` provides and easy way to accept user input and order a query by the input.
4
+
5
+ Installtion
6
+ -----------
7
+
8
+ - Add `gem 'activerecord-sort', require: 'active_record/sort'
9
+ - Run `bundle install`
10
+
11
+ Examples
12
+ --------
13
+ `ActiveRecord::sort` supports the following cases:
14
+
15
+ ```ruby
16
+ Property.order(:id).to_sql
17
+ # => "...ORDER BY properties.id ASC"
18
+
19
+ Property.order(:id, :name).to_sql
20
+ # => "...ORDER BY properties.id ASC, properties.name ASC"
21
+
22
+ Property.order(:id => :desc).to_sql
23
+ # => "...ORDER BY properties.id DESC"
24
+
25
+ Property.order(:id => {:asc => :nulls_first})
26
+ # => "...ORDER BY properties.id ASC NULLS FIRST"
27
+
28
+ Property.order(:id => {:asc => :nulls_last})
29
+ # => "...ORDER BY properties.id ASC NULLS LAST"
30
+ ```
31
+
32
+ It can also sort on relations:
33
+
34
+ ```ruby
35
+ Property.order(:addresses => :id).to_sql
36
+ # => "...INNER JOIN addresses ON addresses.property_id = properties.id
37
+ # => " ORDER BY addresses.id ASC"
38
+
39
+ Property.order(:addresses => {:id => :desc}).to_sql
40
+ # => "...INNER JOIN addresses ON addresses.property_id = properties.id
41
+ # => " ORDER BY addresses.id DESC"
42
+
43
+ Property.order(:addresses => {:id => {:asc => :nulls_frist}}).to_sql
44
+ # => "...INNER JOIN addresses ON addresses.property_id = properties.id
45
+ # => " ORDER BY addresses.id ASC NULLS FIRST"
46
+ ```
@@ -15,7 +15,7 @@ class ActiveRecord::Base
15
15
  return resource if ordering.size == 0
16
16
 
17
17
  ordering.each do |order|
18
- order = sort_order_to_column_and_options(order)
18
+ order = Array(order)
19
19
 
20
20
  order.each do |column_or_relation, options|
21
21
  if self.column_names.include?(column_or_relation.to_s)
@@ -54,7 +54,7 @@ class ActiveRecord::Base
54
54
  options = [options] if !options.is_a?(Array)
55
55
 
56
56
  options.each do |order|
57
- order = sort_order_to_column_and_options(order)
57
+ order = Array(order)
58
58
  order.each do |column, options|
59
59
  column = relation.klass.arel_table[column]
60
60
  direction = (options.is_a?(Hash) ? options.keys.first.to_sym : options.to_s.downcase.to_sym)
@@ -87,7 +87,7 @@ class ActiveRecord::Base
87
87
  options = [options] if !options.is_a?(Array)
88
88
 
89
89
  options.each do |order|
90
- order = sort_order_to_column_and_options(order)
90
+ order = Array(order)
91
91
  order.each do |column, options|
92
92
  column = relation.klass.arel_table[column]
93
93
  direction = (options.is_a?(Hash) ? options.keys.first.to_sym : options.to_s.downcase.to_sym)
@@ -108,13 +108,5 @@ class ActiveRecord::Base
108
108
  resource
109
109
  end
110
110
 
111
- def sort_order_to_column_and_options(column)
112
- if column.is_a?(Symbol)
113
- [column]
114
- else
115
- column
116
- end
117
- end
118
-
119
111
  end
120
112
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Sort
3
- VERSION = '1.0.0'
3
+ VERSION = '1.0.1'
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: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Bracy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-30 00:00:00.000000000 Z
11
+ date: 2015-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg