activerecord-collections 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_record/collection.rb +2 -2
- data/lib/active_record/collections/batching.rb +51 -61
- data/lib/active_record/collections/collectable.rb +1 -0
- data/lib/active_record/collections/pagination.rb +81 -0
- data/lib/active_record/collections/records.rb +1 -5
- data/lib/active_record/collections/relation.rb +19 -27
- data/lib/active_record/collections/serialization.rb +10 -10
- data/lib/active_record/collections/version.rb +1 -1
- data/lib/activerecord-collections.rb +2 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30bfecdfccd0daf5210318a0a0b0ba38552b78d1
|
4
|
+
data.tar.gz: 1fc500acc90fa0467dc711b6991a79d5d5408a66
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e29769f0a1122a146425847b98ba2aff294a3a40d2a9c84959d12a0a0fbcf977431b209cb6692345f9b472c16252d60be0d176c8697aba6ef20de874bf7635a
|
7
|
+
data.tar.gz: e74a2a642cf76911383b0cd20760716d4968a9f852d1656c38a49ea43355693a9f0018adf5c285f2234cb4636319883dd114a560ff8ff6d43e6b5763acdb1ab4
|
@@ -5,6 +5,7 @@ module ActiveRecord
|
|
5
5
|
include ActiveRecord::Collections::Batching
|
6
6
|
include ActiveRecord::Collections::Delegation
|
7
7
|
include ActiveRecord::Collections::Serialization
|
8
|
+
include ActiveRecord::Collections::Pagination
|
8
9
|
attr_reader :relation, :options
|
9
10
|
|
10
11
|
class << self
|
@@ -79,8 +80,7 @@ module ActiveRecord
|
|
79
80
|
@collectable = old.collectable
|
80
81
|
@options = old.options.dup
|
81
82
|
@records = @relation = old.relation.dup
|
82
|
-
@
|
83
|
-
batch!(batch: old.current_batch, batch_size: old.batch_size) if old.is_batch? || old.batched?(false)
|
83
|
+
@total_count = old.instance_variable_get(:@total_count)
|
84
84
|
is_batch! if old.is_batch?
|
85
85
|
end
|
86
86
|
end
|
@@ -8,12 +8,12 @@ module ActiveRecord
|
|
8
8
|
module ClassMethods
|
9
9
|
def default_batch_size(size=nil)
|
10
10
|
@default_batch_size = size unless size.nil?
|
11
|
-
@default_batch_size ||=
|
11
|
+
@default_batch_size ||= 500
|
12
12
|
end
|
13
13
|
|
14
14
|
def batching_threshold(threshold=nil)
|
15
15
|
@batching_threshold = threshold unless threshold.nil?
|
16
|
-
@batching_threshold ||=
|
16
|
+
@batching_threshold ||= 0
|
17
17
|
end
|
18
18
|
|
19
19
|
def batch_by_default!
|
@@ -44,15 +44,53 @@ module ActiveRecord
|
|
44
44
|
def batch_by_default?
|
45
45
|
self.class.batch_by_default? ||
|
46
46
|
( batching_threshold > 0 &&
|
47
|
-
|
47
|
+
total_count >= batching_threshold )
|
48
48
|
end
|
49
49
|
|
50
|
-
def should_batch?
|
50
|
+
def should_batch?
|
51
51
|
return false if is_batch?
|
52
|
-
return false if check_if_batched && batched?
|
53
52
|
batch_by_default?
|
54
53
|
end
|
55
54
|
|
55
|
+
def is_batched?
|
56
|
+
@is_batched || false
|
57
|
+
end
|
58
|
+
alias_method :batched?, :is_batched?
|
59
|
+
|
60
|
+
def batch(btch=1)
|
61
|
+
dup.batch!(btch)
|
62
|
+
end
|
63
|
+
|
64
|
+
def batch!(btch=1)
|
65
|
+
batchify!(btch, default_batch_size)
|
66
|
+
end
|
67
|
+
|
68
|
+
def per_batch(bs=nil)
|
69
|
+
dup.per_batch!(bs)
|
70
|
+
end
|
71
|
+
|
72
|
+
def per_batch!(bs=nil)
|
73
|
+
batchify!(current_batch, (bs || default_batch_size))
|
74
|
+
end
|
75
|
+
|
76
|
+
def batchify!(btch, bs)
|
77
|
+
@is_batched = true
|
78
|
+
limit!(bs)
|
79
|
+
offset!((btch - 1) * bs)
|
80
|
+
end
|
81
|
+
|
82
|
+
def total_batches
|
83
|
+
(total_count.to_f / (relation.limit_value || total_count).to_f).ceil
|
84
|
+
end
|
85
|
+
|
86
|
+
def current_batch
|
87
|
+
(relation.offset_value.to_i / (relation.limit_value || 1)) + 1
|
88
|
+
end
|
89
|
+
|
90
|
+
def batch_size
|
91
|
+
limit_value || total_count
|
92
|
+
end
|
93
|
+
|
56
94
|
def is_batch!
|
57
95
|
@is_batch = true
|
58
96
|
self
|
@@ -76,7 +114,7 @@ module ActiveRecord
|
|
76
114
|
batched = dup.batch!
|
77
115
|
batches = [batched.first_batch!.as_batch]
|
78
116
|
while batched.next_batch? do
|
79
|
-
batches << batched.
|
117
|
+
batches << batched.as_next_batch
|
80
118
|
end
|
81
119
|
batches
|
82
120
|
end
|
@@ -87,7 +125,7 @@ module ActiveRecord
|
|
87
125
|
batches = [batched.first_batch!.as_batch]
|
88
126
|
yield batches.first if block_given?
|
89
127
|
while batched.next_batch? do
|
90
|
-
b = batched.
|
128
|
+
b = batched.as_next_batch
|
91
129
|
yield b if block_given?
|
92
130
|
batches << b
|
93
131
|
end
|
@@ -95,56 +133,8 @@ module ActiveRecord
|
|
95
133
|
end
|
96
134
|
alias_method :in_batches, :as_batches
|
97
135
|
|
98
|
-
def batch(batch: 1, batch_size: nil)
|
99
|
-
dup.batch!(batch: batch, batch_size: batch_size)
|
100
|
-
end
|
101
|
-
|
102
|
-
def batch!(batch: 1, batch_size: nil)
|
103
|
-
reset!(false, false)
|
104
|
-
@current_batch = batch
|
105
|
-
@batch_size = batch_size unless batch_size.nil?
|
106
|
-
@batch_size ||= default_batch_size
|
107
|
-
@relation = relation.limit(@batch_size).offset((@current_batch - 1) * @batch_size)
|
108
|
-
self
|
109
|
-
end
|
110
|
-
|
111
|
-
def per_batch(num=nil)
|
112
|
-
dup.per_batch!(num)
|
113
|
-
end
|
114
|
-
|
115
|
-
def per_batch!(num=nil)
|
116
|
-
reset!(false, false)
|
117
|
-
@current_batch ||= 1
|
118
|
-
@batch_size = num || default_batch_size
|
119
|
-
@relation = relation.limit(@batch_size).offset((@current_batch - 1) * @batch_size)
|
120
|
-
self
|
121
|
-
end
|
122
|
-
|
123
|
-
def batched?(check_if_should=false)
|
124
|
-
return true if !(@current_batch.nil? && @batch_size.nil?)
|
125
|
-
if check_if_should && should_batch?(false)
|
126
|
-
batch!
|
127
|
-
true
|
128
|
-
else
|
129
|
-
false
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
def current_batch
|
134
|
-
@current_batch || 1
|
135
|
-
end
|
136
|
-
|
137
|
-
def batch_size
|
138
|
-
@batch_size || total_count
|
139
|
-
end
|
140
|
-
|
141
|
-
def total_batches
|
142
|
-
return 1 if is_batch?
|
143
|
-
(total_count.to_f / batch_size.to_f).ceil
|
144
|
-
end
|
145
|
-
|
146
136
|
def each_batch(&block)
|
147
|
-
batch!
|
137
|
+
batch!
|
148
138
|
|
149
139
|
if total_batches <= 1
|
150
140
|
yield to_a if block_given?
|
@@ -163,7 +153,7 @@ module ActiveRecord
|
|
163
153
|
end
|
164
154
|
|
165
155
|
def batch_map(&block)
|
166
|
-
batch!
|
156
|
+
batch!
|
167
157
|
|
168
158
|
if total_batches <= 1
|
169
159
|
return (block_given? ? yield(to_a) : to_a)
|
@@ -188,7 +178,7 @@ module ActiveRecord
|
|
188
178
|
end
|
189
179
|
|
190
180
|
def first_batch!
|
191
|
-
batch!(
|
181
|
+
batch!(1)
|
192
182
|
end
|
193
183
|
|
194
184
|
def next_batch?
|
@@ -200,7 +190,7 @@ module ActiveRecord
|
|
200
190
|
end
|
201
191
|
|
202
192
|
def next_batch!
|
203
|
-
batch!(
|
193
|
+
batch!(current_batch + 1) if next_batch?
|
204
194
|
end
|
205
195
|
|
206
196
|
def prev_batch?
|
@@ -212,7 +202,7 @@ module ActiveRecord
|
|
212
202
|
end
|
213
203
|
|
214
204
|
def prev_batch!
|
215
|
-
batch!(
|
205
|
+
batch!(current_batch - 1) if prev_batch?
|
216
206
|
end
|
217
207
|
|
218
208
|
def last_batch
|
@@ -220,7 +210,7 @@ module ActiveRecord
|
|
220
210
|
end
|
221
211
|
|
222
212
|
def last_batch!
|
223
|
-
batch!(
|
213
|
+
batch!(total_batches)
|
224
214
|
end
|
225
215
|
end
|
226
216
|
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Collections
|
3
|
+
module Pagination
|
4
|
+
def self.included(base)
|
5
|
+
base.send :extend, ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def default_per_page(pp=nil)
|
10
|
+
@default_per_page = pp unless pp.nil?
|
11
|
+
@default_per_page ||= 25
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def default_per_page
|
16
|
+
self.class.default_per_page
|
17
|
+
end
|
18
|
+
|
19
|
+
def page(pg=1)
|
20
|
+
dup.page!(pg)
|
21
|
+
end
|
22
|
+
|
23
|
+
def page!(pg=1)
|
24
|
+
paginate!(pg, default_per_page)
|
25
|
+
end
|
26
|
+
|
27
|
+
def per(pp=nil)
|
28
|
+
dup.per!(pp)
|
29
|
+
end
|
30
|
+
|
31
|
+
def per!(pp=nil)
|
32
|
+
paginate!(current_page, (pp || default_per_page))
|
33
|
+
end
|
34
|
+
|
35
|
+
def paginate!(pg, pp)
|
36
|
+
limit!(pp)
|
37
|
+
offset!((pg - 1) * pp)
|
38
|
+
end
|
39
|
+
|
40
|
+
def total_pages
|
41
|
+
(total_count.to_f / (relation.limit_value || total_count).to_f).ceil
|
42
|
+
end
|
43
|
+
|
44
|
+
def current_page
|
45
|
+
(relation.offset_value.to_i / (relation.limit_value || 1)) + 1
|
46
|
+
end
|
47
|
+
|
48
|
+
def per_page
|
49
|
+
limit_value || total_count
|
50
|
+
end
|
51
|
+
|
52
|
+
def next_page
|
53
|
+
current_page + 1 unless last_page?
|
54
|
+
end
|
55
|
+
|
56
|
+
def prev_page
|
57
|
+
current_page - 1 unless first_page?
|
58
|
+
end
|
59
|
+
|
60
|
+
def next_page?
|
61
|
+
!last_page?
|
62
|
+
end
|
63
|
+
|
64
|
+
def prev_page?
|
65
|
+
!first_page?
|
66
|
+
end
|
67
|
+
|
68
|
+
def first_page?
|
69
|
+
current_page == 1
|
70
|
+
end
|
71
|
+
|
72
|
+
def last_page?
|
73
|
+
current_page == total_pages
|
74
|
+
end
|
75
|
+
|
76
|
+
def out_of_range?
|
77
|
+
current_page > total_pages
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -18,12 +18,8 @@ module ActiveRecord
|
|
18
18
|
end
|
19
19
|
alias_method :to_a, :to_ary
|
20
20
|
|
21
|
-
def total_records
|
22
|
-
@total_records ||= relation.limit(nil).count
|
23
|
-
end
|
24
|
-
|
25
21
|
def total_count
|
26
|
-
|
22
|
+
@total_count ||= relation.dup.limit(nil).offset(nil).count
|
27
23
|
end
|
28
24
|
alias_method :total, :total_count
|
29
25
|
alias_method :count, :total_count
|
@@ -53,7 +53,7 @@ module ActiveRecord
|
|
53
53
|
end
|
54
54
|
|
55
55
|
def select!(*args)
|
56
|
-
reset!
|
56
|
+
#reset!
|
57
57
|
@relation = relation.select(*args)
|
58
58
|
self
|
59
59
|
end
|
@@ -63,7 +63,7 @@ module ActiveRecord
|
|
63
63
|
end
|
64
64
|
|
65
65
|
def distinct!(bool=true)
|
66
|
-
reset!
|
66
|
+
#reset!
|
67
67
|
@relation = relation.distinct(bool)
|
68
68
|
self
|
69
69
|
end
|
@@ -73,7 +73,7 @@ module ActiveRecord
|
|
73
73
|
end
|
74
74
|
|
75
75
|
def where!(*args, &block)
|
76
|
-
reset!
|
76
|
+
#reset!
|
77
77
|
relation.where!(*args, &block)
|
78
78
|
self
|
79
79
|
end
|
@@ -83,7 +83,7 @@ module ActiveRecord
|
|
83
83
|
end
|
84
84
|
|
85
85
|
def not!(*args, &block)
|
86
|
-
reset!
|
86
|
+
#reset!
|
87
87
|
@relation = relation.where.not(*args, &block)
|
88
88
|
self
|
89
89
|
end
|
@@ -93,7 +93,7 @@ module ActiveRecord
|
|
93
93
|
end
|
94
94
|
|
95
95
|
def or!(*args, &block)
|
96
|
-
reset!
|
96
|
+
#reset!
|
97
97
|
@relation = relation.or.where(*args, &block)
|
98
98
|
self
|
99
99
|
end
|
@@ -103,7 +103,7 @@ module ActiveRecord
|
|
103
103
|
end
|
104
104
|
|
105
105
|
def order!(*args, &block)
|
106
|
-
reset!(false)
|
106
|
+
#reset!(false)
|
107
107
|
relation.order!(*args, &block)
|
108
108
|
self
|
109
109
|
end
|
@@ -113,7 +113,7 @@ module ActiveRecord
|
|
113
113
|
end
|
114
114
|
|
115
115
|
def limit!(*args, &block)
|
116
|
-
reset!
|
116
|
+
#reset!
|
117
117
|
relation.limit!(*args, &block)
|
118
118
|
self
|
119
119
|
end
|
@@ -123,31 +123,17 @@ module ActiveRecord
|
|
123
123
|
end
|
124
124
|
|
125
125
|
def offset!(*args, &block)
|
126
|
-
reset!
|
126
|
+
#reset!
|
127
127
|
relation.offset!(*args, &block)
|
128
128
|
self
|
129
129
|
end
|
130
130
|
|
131
|
-
# TODO make this not dependent on kaminari
|
132
|
-
def page!(*args)
|
133
|
-
@relation = relation.page(*args)
|
134
|
-
self
|
135
|
-
end
|
136
|
-
alias_method :page, :page!
|
137
|
-
|
138
|
-
def per!(*args)
|
139
|
-
@relation = relation.page((relation.offset_value / relation.limit_value) + 1).per(*args)
|
140
|
-
self
|
141
|
-
end
|
142
|
-
alias_method :per, :per!
|
143
|
-
# END kaminari
|
144
|
-
|
145
131
|
def joins(*args)
|
146
132
|
dup.joins!(*args)
|
147
133
|
end
|
148
134
|
|
149
135
|
def joins!(*args)
|
150
|
-
reset!
|
136
|
+
#reset!
|
151
137
|
relation.joins!(*args)
|
152
138
|
self
|
153
139
|
end
|
@@ -157,7 +143,7 @@ module ActiveRecord
|
|
157
143
|
end
|
158
144
|
|
159
145
|
def includes!(*args)
|
160
|
-
reset!
|
146
|
+
#reset!
|
161
147
|
relation.includes!(*args)
|
162
148
|
self
|
163
149
|
end
|
@@ -167,21 +153,27 @@ module ActiveRecord
|
|
167
153
|
end
|
168
154
|
|
169
155
|
def references!(*table_names)
|
170
|
-
reset!
|
156
|
+
#reset!
|
171
157
|
relation.references!(*table_names)
|
172
158
|
self
|
173
159
|
end
|
174
160
|
|
161
|
+
%i(bind_values select_values distinct_value joins_values includes_values references_values where_values order_values limit_value offset_value).each do |meth|
|
162
|
+
define_method meth do
|
163
|
+
relation.send(meth)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
175
167
|
def reset(clear_total=true, clear_batches=true)
|
176
168
|
dup.reset!(clear_total, clear_batches)
|
177
169
|
end
|
178
170
|
|
179
171
|
def reset!(clear_total=true, clear_batches=true)
|
180
172
|
@records = @record_ids = @size = nil
|
181
|
-
@
|
173
|
+
@total_count = nil if clear_total
|
182
174
|
relation.reset
|
183
175
|
if clear_batches
|
184
|
-
@
|
176
|
+
@is_batched = false
|
185
177
|
relation.limit!(nil).offset!(nil)
|
186
178
|
end
|
187
179
|
self
|
@@ -32,18 +32,18 @@ module ActiveRecord
|
|
32
32
|
|
33
33
|
def to_hash(include_limit=false)
|
34
34
|
h = {
|
35
|
-
select:
|
36
|
-
distinct:
|
37
|
-
joins:
|
38
|
-
references:
|
39
|
-
includes:
|
40
|
-
where:
|
41
|
-
order:
|
42
|
-
bind:
|
35
|
+
select: select_values,
|
36
|
+
distinct: distinct_value,
|
37
|
+
joins: joins_values,
|
38
|
+
references: references_values,
|
39
|
+
includes: includes_values,
|
40
|
+
where: where_values.map { |v| v.is_a?(String) ? v : v.to_sql },
|
41
|
+
order: order_values.map { |v| v.is_a?(String) ? v : v.to_sql },
|
42
|
+
bind: bind_values.map { |b| {name: b.first.name, value: b.last} }
|
43
43
|
}
|
44
44
|
if include_limit || try(:is_batch?)
|
45
|
-
h[:limit] =
|
46
|
-
h[:offset] =
|
45
|
+
h[:limit] = limit_value
|
46
|
+
h[:offset] = offset_value
|
47
47
|
end
|
48
48
|
h
|
49
49
|
end
|
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'active_record/collections/batching'
|
2
2
|
require 'active_record/collections/delegation'
|
3
|
-
require 'active_record/collections/
|
3
|
+
require 'active_record/collections/pagination'
|
4
4
|
require 'active_record/collections/records'
|
5
|
+
require 'active_record/collections/relation'
|
5
6
|
require 'active_record/collections/serialization'
|
6
7
|
require 'active_record/collection'
|
7
8
|
require 'active_record/collections/collectable'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-collections
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Rebec
|
@@ -107,6 +107,7 @@ files:
|
|
107
107
|
- lib/active_record/collections/batching.rb
|
108
108
|
- lib/active_record/collections/collectable.rb
|
109
109
|
- lib/active_record/collections/delegation.rb
|
110
|
+
- lib/active_record/collections/pagination.rb
|
110
111
|
- lib/active_record/collections/records.rb
|
111
112
|
- lib/active_record/collections/relation.rb
|
112
113
|
- lib/active_record/collections/serialization.rb
|