yap 0.3.3 → 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/README.md +51 -16
- data/lib/yap/active_record/relation.rb +54 -0
- data/lib/yap.rb +7 -11
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb8feec012229c542653414cb4cc9600dbd07fed
|
4
|
+
data.tar.gz: 468697c601588f97ff517788dc8e874df9c1d730
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cdfbeafd15a8e8e34df20875f7216f8af7b49a6e82cc86ad3808293bd18f1320479aaeeb8d288c6acabe1a7104542e2018b21925fac2742c652b7404ff3adcd8
|
7
|
+
data.tar.gz: d98c220c7ff8a117b9b6c07de144cd0a0d74305fd46f107b703a1805fa33fa5a5d4b6e66b0aa84148c0ef8f23165b0e1a4f7ea3b97e1a09e6e82a3a3e633124b
|
data/README.md
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# yap v0.3.3
|
2
|
-
|
3
1
|
Yet another paginator for Ruby on Rails, which adds a `paginate` scope to your ActiveRecords.
|
4
2
|
|
5
3
|
## Setup
|
@@ -45,36 +43,64 @@ internal naming from users and to make sorting by associations possible (more on
|
|
45
43
|
|
46
44
|
Assuming you included `Yap` into `User`, you can now do something like this:
|
47
45
|
|
48
|
-
User.paginate
|
46
|
+
User.paginate
|
47
|
+
# => Page 1 with default order and size
|
48
|
+
|
49
49
|
User.paginate(
|
50
50
|
page: 1,
|
51
51
|
per_page: 10,
|
52
52
|
sort: 'id',
|
53
53
|
direction: 'ASC'
|
54
|
-
)
|
54
|
+
)
|
55
|
+
# => Invocation with custom options.
|
56
|
+
|
57
|
+
User.paginate(params).last_page
|
58
|
+
# => Last page as a number for the previously paginated query
|
59
|
+
User.paginate(params).range
|
60
|
+
# => E.g. { from: 1, to: 10, total: 100 }
|
61
|
+
User.paginate(params).total
|
62
|
+
# => total number of results for this filters
|
63
|
+
|
64
|
+
User.paginate(params).without_pagination do |rel|
|
65
|
+
# access rel without limit and offset; filters still apply
|
66
|
+
rel.count
|
67
|
+
end
|
68
|
+
# => total number of results
|
55
69
|
|
56
|
-
User.
|
57
|
-
|
70
|
+
User.filter('gender' => 'f')
|
71
|
+
# => All female users
|
58
72
|
|
59
|
-
User.filter('gender' => 'f') # => All female users
|
60
73
|
User.filter(
|
61
74
|
'team_id' => '1,2',
|
62
75
|
'gender' => 'm'
|
63
|
-
)
|
76
|
+
)
|
77
|
+
# => All males of teams 1 and 2
|
78
|
+
|
64
79
|
User.filter(
|
65
|
-
'
|
66
|
-
|
67
|
-
|
80
|
+
# Note that '0...3' means [0,1,2] while '0..3' means [0,1,2,3]
|
81
|
+
'date_of_birth' => '1990-01-01...1991-01-01'
|
82
|
+
)
|
83
|
+
# => All users born in 1990
|
84
|
+
|
85
|
+
User.filter('team_id' => '!null')
|
86
|
+
# => All users with any team
|
87
|
+
|
68
88
|
User.paginate(
|
69
89
|
page: 1,
|
70
90
|
filter: { 'team' => 'null' }
|
71
|
-
)
|
91
|
+
)
|
92
|
+
# => Combining filter and pagination
|
72
93
|
|
73
|
-
User.paginate(params)
|
94
|
+
User.paginate(params)
|
95
|
+
# => Passing parameters in controller (http://localhost/users?filter[gender]=f)
|
74
96
|
|
75
97
|
Yap will convert strings to symbols or numbers and vice versa where necessary. This make the last one a really powerful
|
76
98
|
method of offering the pagination API directly to the user.
|
77
99
|
|
100
|
+
### Chaining
|
101
|
+
|
102
|
+
The `paginate` scope can be chained with other `ActiveRecord` methods like `joins`, `where` etc..
|
103
|
+
|
78
104
|
### Advanced
|
79
105
|
|
80
106
|
The "team" alias defined in the column map above allows us to sort the results by the name of the team a user belongs
|
@@ -125,6 +151,15 @@ If an option cannot be parsed it will raise `Yap::PaginationError` or `Yap::Filt
|
|
125
151
|
|
126
152
|
## ToDos
|
127
153
|
|
128
|
-
*
|
129
|
-
|
130
|
-
|
154
|
+
* Make gathering total/range optional
|
155
|
+
|
156
|
+
## Changelog
|
157
|
+
|
158
|
+
### 1.0.0
|
159
|
+
|
160
|
+
* changed `last_page` to base on the actual query not only the parameters
|
161
|
+
* this now produces correct results if there are custom `where` conditions
|
162
|
+
* added `range` method which can be used like `last_page`
|
163
|
+
* provides a hash containing the limits of the latest queried page
|
164
|
+
* added `total` method to get the total number of results
|
165
|
+
* added `without_pagination` which takes a block an serves an `Activerecord::Relation` which is not paginated
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
class Relation
|
3
|
+
##
|
4
|
+
# Access the relation without pagination in a block. Limit and offset values are removed, filters still apply.
|
5
|
+
#
|
6
|
+
# @return [Object] Return value of the block
|
7
|
+
#
|
8
|
+
def without_pagination
|
9
|
+
rel = dup
|
10
|
+
rel.limit! nil
|
11
|
+
rel.offset! nil
|
12
|
+
|
13
|
+
yield rel
|
14
|
+
end
|
15
|
+
|
16
|
+
##
|
17
|
+
# Returns the total number of results without pagination. This is used for generating range and last_page values.
|
18
|
+
# The result for a relation is cached because count can be quite expensive.
|
19
|
+
#
|
20
|
+
# @return [Integer] Total number of results
|
21
|
+
#
|
22
|
+
def total
|
23
|
+
@total ||= without_pagination { |rel| rel.count }
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# Calculates the last page for paginated results.
|
28
|
+
#
|
29
|
+
# @return [Integer] Last page as a number
|
30
|
+
#
|
31
|
+
def last_page
|
32
|
+
page = (total / limit_value.to_f).ceil
|
33
|
+
|
34
|
+
page == 0 ? 1 : page
|
35
|
+
end
|
36
|
+
|
37
|
+
##
|
38
|
+
# Returns a hash defining a range with :from, :to and :total values.
|
39
|
+
#
|
40
|
+
# @return [Hash] Values defining the range of the current page.
|
41
|
+
#
|
42
|
+
def range
|
43
|
+
from = offset_value+1
|
44
|
+
to = offset_value+limit_value
|
45
|
+
to = total if total < to
|
46
|
+
|
47
|
+
{
|
48
|
+
from: from,
|
49
|
+
to: to,
|
50
|
+
total: total
|
51
|
+
}
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/yap.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'active_support/concern'
|
2
|
-
require 'yap/
|
2
|
+
require 'yap/active_record/relation'
|
3
3
|
require 'yap/column_mapper'
|
4
4
|
require 'yap/filterable'
|
5
|
+
require 'yap/exceptions'
|
5
6
|
|
6
7
|
##
|
7
8
|
# Support for Active Record pagination. All options can be safely accessed by the user through url query parameters. To
|
@@ -25,23 +26,15 @@ module Yap
|
|
25
26
|
extend ActiveSupport::Concern
|
26
27
|
include Filterable
|
27
28
|
|
28
|
-
DEFAULTS = Struct.new(:page, :per_page, :sort, :direction, :disable_warnings)
|
29
|
-
.new(1, 10, :id, :asc, false)
|
29
|
+
DEFAULTS = Struct.new(:page, :per_page, :hard_limit, :sort, :direction, :disable_warnings)
|
30
|
+
.new(1, 10, nil, :id, :asc, false)
|
30
31
|
|
31
32
|
def self.configure
|
32
33
|
raise ArgumentError, 'No block given.' unless block_given?
|
33
34
|
yield(DEFAULTS)
|
34
35
|
end
|
35
36
|
|
36
|
-
module ClassMethods
|
37
|
-
def last_page(params)
|
38
|
-
per_page = extract_number(params[:per_page], DEFAULTS.per_page)
|
39
|
-
(filter(params[:filter]).count / per_page.to_f).ceil
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
37
|
included do
|
44
|
-
extend ClassMethods
|
45
38
|
extend ColumnMapper
|
46
39
|
|
47
40
|
##
|
@@ -59,6 +52,9 @@ module Yap
|
|
59
52
|
def self.extract_pagination_params(params)
|
60
53
|
page = extract_number(params[:page], DEFAULTS.page)
|
61
54
|
per_page = extract_number(params[:per_page], DEFAULTS.per_page)
|
55
|
+
if DEFAULTS.hard_limit && per_page > DEFAULTS.hard_limit
|
56
|
+
raise PaginationError.new("Not more than #{DEFAULTS.hard_limit} items per page accepted.")
|
57
|
+
end
|
62
58
|
sort = extract_column(params[:sort])
|
63
59
|
direction = extract_direction(params[:direction])
|
64
60
|
|
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: 0.
|
4
|
+
version: 1.0.1
|
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-01
|
11
|
+
date: 2015-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 4.1
|
19
|
+
version: '4.1'
|
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: 4.1
|
26
|
+
version: '4.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: sqlite3
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -48,6 +48,7 @@ files:
|
|
48
48
|
- LICENSE
|
49
49
|
- README.md
|
50
50
|
- lib/yap.rb
|
51
|
+
- lib/yap/active_record/relation.rb
|
51
52
|
- lib/yap/column_mapper.rb
|
52
53
|
- lib/yap/exceptions.rb
|
53
54
|
- lib/yap/filter.rb
|
@@ -62,9 +63,9 @@ require_paths:
|
|
62
63
|
- lib
|
63
64
|
required_ruby_version: !ruby/object:Gem::Requirement
|
64
65
|
requirements:
|
65
|
-
- - "
|
66
|
+
- - "~>"
|
66
67
|
- !ruby/object:Gem::Version
|
67
|
-
version: 2.
|
68
|
+
version: '2.0'
|
68
69
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
70
|
requirements:
|
70
71
|
- - ">="
|