yap 1.1 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 977481c7c3322da6e497d1d853a316cfa58e8322
4
- data.tar.gz: e5aee06760c0f49d79539c3f1105eb30625291d1
3
+ metadata.gz: 7c06ad590d341b79057d52f99af8c20400b8ac9e
4
+ data.tar.gz: f6b3102bf46259bf4a60fc1178a864e357236b39
5
5
  SHA512:
6
- metadata.gz: 34b7c4a7a2b88477e0bb1578c807f94fbf2743ce7c582d6d12eb48acdc1e6401954b66c92fa217982d014f902f61b648f3f365639a021f08d6a2fcbb217cfe68
7
- data.tar.gz: 522e1d53ba3134c86ce946b743a91e7abd3529b69f98094755dc6d2e237625d9ed809d800d4307474c968e614b31403070b4b30486c65d115f9a6882226bdefa
6
+ metadata.gz: 76221f22ab11ecc78f5c9f9f7b27b576d9be29621f2fa62a02ceef6d24158a7ef7311fec1872af0b62960fa2816fd44c316a0b9c3ee74ffc153e35e28259fc75
7
+ data.tar.gz: 25a75e83e8d189a70d24b0d71682840816236743541d1230ee98969bfe254b82009df3b483e1311b2de48da237ebd85b2132105aebd5b8d9ebe465b608dc8654
data/README.md CHANGED
@@ -67,6 +67,17 @@ Assuming you included `Yap` into `User`, you can now do something like this:
67
67
  end
68
68
  # => total number of results
69
69
 
70
+ User.paginate(
71
+ sort: {
72
+ 'gender' => 'desc',
73
+ 'date_of_birth' => 'asc'
74
+ }
75
+ )
76
+ # => Sort by gender and date_of_birth (method 1)
77
+
78
+ User.paginate(sort: 'gender,date_of_birth', direction: 'desc,asc')
79
+ # => Sort by gender and date_of_birth (method 2)
80
+
70
81
  User.filter('gender' => 'f')
71
82
  # => All female users
72
83
 
@@ -149,11 +160,14 @@ If an option cannot be parsed it will raise `Yap::PaginationError` or `Yap::Filt
149
160
  end
150
161
  end
151
162
 
152
- ## ToDos
163
+ ## Changelog
153
164
 
154
- * Make gathering total/range optional
165
+ ### 1.2
155
166
 
156
- ## Changelog
167
+ * added sorting by multiple elements
168
+ * method 1: ?sort[team]=desc&sort[date_of_birth]=asc
169
+ * method 2: ?sort=team,date_of_birth&direction=desc,asc
170
+ * missing directions will fall back to default
157
171
 
158
172
  ### 1.1
159
173
 
data/lib/yap.rb CHANGED
@@ -55,10 +55,9 @@ module Yap
55
55
  if DEFAULTS.hard_limit && per_page > DEFAULTS.hard_limit
56
56
  raise PaginationError.new("Not more than #{DEFAULTS.hard_limit} items per page accepted.")
57
57
  end
58
- sort = extract_column(params[:sort])
59
- direction = extract_direction(params[:direction])
58
+ sort = extract_order(params[:sort], params[:direction])
60
59
 
61
- return page, per_page, (sort =~ /\./ ? "#{sort} #{direction}" : { sort => direction })
60
+ return page, per_page, sort
62
61
  end
63
62
 
64
63
  def self.extract_number(number, default)
@@ -73,6 +72,36 @@ module Yap
73
72
  number
74
73
  end
75
74
 
75
+ def self.extract_order(sort, direction)
76
+ sort = sort.split(',') if sort.is_a?(String) && sort =~ /,/
77
+ direction = direction.split(',') if direction.is_a?(String) && direction =~ /,/
78
+
79
+ case sort
80
+ when Array
81
+ order = []
82
+ direction = Array.wrap direction
83
+ sort.each_with_index do |s, i|
84
+ order << build_order_by(s, direction[i] || DEFAULTS.direction)
85
+ end
86
+
87
+ order
88
+ when Hash
89
+ sort.map do |s, d|
90
+ build_order_by(s, d)
91
+ end
92
+ else
93
+ build_order_by sort, direction
94
+ end
95
+ end
96
+
97
+ def self.build_order_by(sort, direction)
98
+ sort = extract_column(sort)
99
+ direction = extract_direction(direction)
100
+
101
+ (sort =~ /\./ ? "#{sort} #{direction}" : { sort => direction })
102
+ end
103
+ private_class_method :build_order_by
104
+
76
105
  def self.extract_column(sort)
77
106
  sort ||= DEFAULTS.sort
78
107
  column = map_column(sort.to_s.downcase)
data/lib/yap/filter.rb CHANGED
@@ -21,13 +21,13 @@ module Yap
21
21
  when /(.+)\.{2}(.+)/
22
22
  value = $1..$2
23
23
  else
24
+ # Convert null to ruby nil to use 'IS NULL' in SQL.
24
25
  value = value.downcase == 'null' ? nil : value
25
26
  end
26
27
 
27
28
  # Ensure filter contains an array to append to.
28
29
  self[match][column] ||= []
29
30
 
30
- # Convert null to ruby nil to use 'IS NULL' in SQL.
31
31
  self[match][column] << value
32
32
  end
33
33
  end
@@ -0,0 +1,3 @@
1
+ module Yap
2
+ VERSION = '1.2.2'
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yap
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.1'
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Finn Glöe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-07 00:00:00.000000000 Z
11
+ date: 2015-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -53,7 +53,8 @@ files:
53
53
  - lib/yap/exceptions.rb
54
54
  - lib/yap/filter.rb
55
55
  - lib/yap/filterable.rb
56
- homepage: http://rubygems.org/gems/yap
56
+ - lib/yap/version.rb
57
+ homepage: https://github.com/1and1/yap
57
58
  licenses:
58
59
  - GPL v2
59
60
  metadata: {}
@@ -73,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
74
  version: '0'
74
75
  requirements: []
75
76
  rubyforge_project:
76
- rubygems_version: 2.4.5
77
+ rubygems_version: 2.4.6
77
78
  signing_key:
78
79
  specification_version: 4
79
80
  summary: Yet another paginator for Ruby on Rails