each_batched 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +88 -0
  3. data/Rakefile +55 -0
  4. data/lib/each_batched/version.rb +3 -0
  5. data/lib/each_batched.rb +65 -0
  6. data/lib/tasks/each_batched_tasks.rake +4 -0
  7. data/test/dummy/Rakefile +7 -0
  8. data/test/dummy/app/controllers/application_controller.rb +3 -0
  9. data/test/dummy/app/helpers/application_helper.rb +2 -0
  10. data/test/dummy/app/models/company.rb +7 -0
  11. data/test/dummy/app/models/customer.rb +6 -0
  12. data/test/dummy/app/models/product.rb +5 -0
  13. data/test/dummy/app/models/purchase.rb +5 -0
  14. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  15. data/test/dummy/config/application.rb +45 -0
  16. data/test/dummy/config/boot.rb +10 -0
  17. data/test/dummy/config/database.yml +25 -0
  18. data/test/dummy/config/environment.rb +5 -0
  19. data/test/dummy/config/environments/development.rb +30 -0
  20. data/test/dummy/config/environments/production.rb +60 -0
  21. data/test/dummy/config/environments/test.rb +42 -0
  22. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  23. data/test/dummy/config/initializers/inflections.rb +10 -0
  24. data/test/dummy/config/initializers/mime_types.rb +5 -0
  25. data/test/dummy/config/initializers/secret_token.rb +7 -0
  26. data/test/dummy/config/initializers/session_store.rb +8 -0
  27. data/test/dummy/config/locales/en.yml +5 -0
  28. data/test/dummy/config/routes.rb +2 -0
  29. data/test/dummy/config.ru +4 -0
  30. data/test/dummy/db/development.sqlite3 +0 -0
  31. data/test/dummy/db/migrate/0001_initialize_models.rb +36 -0
  32. data/test/dummy/db/schema.rb +48 -0
  33. data/test/dummy/db/test.sqlite3 +0 -0
  34. data/test/dummy/log/development.log +408 -0
  35. data/test/dummy/log/test.log +44659 -0
  36. data/test/dummy/public/404.html +26 -0
  37. data/test/dummy/public/422.html +26 -0
  38. data/test/dummy/public/500.html +26 -0
  39. data/test/dummy/public/favicon.ico +0 -0
  40. data/test/dummy/script/rails +6 -0
  41. data/test/each_batched_test.rb +235 -0
  42. data/test/test_helper.rb +24 -0
  43. metadata +176 -0
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ <p>We've been notified about this issue and we'll take a look at it shortly.</p>
24
+ </div>
25
+ </body>
26
+ </html>
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,235 @@
1
+ require 'test_helper'
2
+
3
+ # None of these tests test that the algorithm internals work properly/efficiently,
4
+ # just that the results give the correct rows in the correct order
5
+ # You'd have to examine log files and stuff to make sure the algorithm isn't doing anything extra that it shouldn't
6
+ class EachBatchedTest < ActiveSupport::TestCase
7
+
8
+ context 'with a simple table,' do
9
+ setup do
10
+ @c = Company
11
+ @c1 = @c.create!(:sort => 4)
12
+ @c2 = @c.create!(:sort => 3)
13
+ @c3 = @c.create!(:sort => 2)
14
+ @c4 = @c.create!(:sort => 1)
15
+ end
16
+ context "with id order," do
17
+ setup { @ord = @c.order(:id) }
18
+ context "and batch size divisible by total" do
19
+ should("br") { assert_equal [[@c1, @c2], [@c3, @c4]], [].tap { |a| @ord.batches_by_range(2) { |r| a << r } } }
20
+ should("bi") { assert_equal [[@c1, @c2], [@c3, @c4]], [].tap { |a| @ord.batches_by_ids(2) { |r| a << r } } }
21
+ should("er") { assert_equal [@c1, @c2, @c3, @c4], [].tap { |a| @ord.each_by_range(2) { |r| a << r } } }
22
+ should("ei") { assert_equal [@c1, @c2, @c3, @c4], [].tap { |a| @ord.each_by_ids(2) { |r| a << r } } }
23
+ end
24
+ context "and batch size not evenly divisible by total" do
25
+ should("br") { assert_equal [[@c1, @c2, @c3], [@c4]], [].tap { |a| @ord.batches_by_range(3) { |r| a << r } } }
26
+ should("bi") { assert_equal [[@c1, @c2, @c3], [@c4]], [].tap { |a| @ord.batches_by_ids(3) { |r| a << r } } }
27
+ should("er") { assert_equal [@c1, @c2, @c3, @c4], [].tap { |a| @ord.each_by_range(3) { |r| a << r } } }
28
+ should("ei") { assert_equal [@c1, @c2, @c3, @c4], [].tap { |a| @ord.each_by_ids(3) { |r| a << r } } }
29
+ end
30
+ context "and an offset" do
31
+ setup { @off = @ord.offset(1) }
32
+ should("br") { assert_equal [[@c2, @c3], [@c4]], [].tap { |a| @off.batches_by_range(2) { |r| a << r } } }
33
+ should("bi") { assert_equal [[@c2, @c3], [@c4]], [].tap { |a| @off.batches_by_ids(2) { |r| a << r } } }
34
+ should("er") { assert_equal [@c2, @c3, @c4], [].tap { |a| @off.each_by_range(2) { |r| a << r } } }
35
+ should("ei") { assert_equal [@c2, @c3, @c4], [].tap { |a| @off.each_by_ids(2) { |r| a << r } } }
36
+ end
37
+ context "and a limit" do
38
+ setup { @lim = @ord.limit(3) }
39
+ should("br") { assert_equal [[@c1, @c2], [@c3]], [].tap { |a| @lim.batches_by_range(2) { |r| a << r } } }
40
+ should("bi") { assert_equal [[@c1, @c2], [@c3]], [].tap { |a| @lim.batches_by_ids(2) { |r| a << r } } }
41
+ should("er") { assert_equal [@c1, @c2, @c3], [].tap { |a| @lim.each_by_range(2) { |r| a << r } } }
42
+ should("ei") { assert_equal [@c1, @c2, @c3], [].tap { |a| @lim.each_by_ids(2) { |r| a << r } } }
43
+ end
44
+ end
45
+ context "with sort order" do
46
+ setup { @ord = @c.order(:sort) }
47
+ should("br") { assert_equal [[@c4, @c3], [@c2, @c1]], [].tap { |a| @ord.batches_by_range(2) { |r| a << r } } }
48
+ should("bi") { assert_equal [[@c4, @c3], [@c2, @c1]], [].tap { |a| @ord.batches_by_ids(2) { |r| a << r } } }
49
+ should("er") { assert_equal [@c4, @c3, @c2, @c1], [].tap { |a| @ord.each_by_range(2) { |r| a << r } } }
50
+ should("ei") { assert_equal [@c4, @c3, @c2, @c1], [].tap { |a| @ord.each_by_ids(2) { |r| a << r } } }
51
+ end
52
+ context "with order set in the select" do
53
+ setup { @ord = @c.select('companies.*, companies.sort + 1 AS foo').order('foo') }
54
+ should("br") { assert_equal [[@c4, @c3], [@c2, @c1]], [].tap { |a| @ord.batches_by_range(2) { |r| a << r } } }
55
+ should("bi") { assert_equal [[@c4, @c3], [@c2, @c1]], [].tap { |a| @ord.batches_by_ids(2) { |r| a << r } } }
56
+ should("er") { assert_equal [@c4, @c3, @c2, @c1], [].tap { |a| @ord.each_by_range(2) { |r| a << r } } }
57
+ should("ei") { assert_equal [@c4, @c3, @c2, @c1], [].tap { |a| @ord.each_by_ids(2) { |r| a << r } } }
58
+ end
59
+ end
60
+
61
+ context "with a has_many association," do
62
+ setup do
63
+ com = Company.create!
64
+ @c = com.customers
65
+ @c1 = @c.create!(:sort => 4)
66
+ @c2 = @c.create!(:sort => 3)
67
+ @c3 = @c.create!(:sort => 2)
68
+ @c4 = @c.create!(:sort => 1)
69
+ end
70
+ context "with id order," do
71
+ setup { @ord = @c.order('customers.id') }
72
+ context "and batch size divisible by total" do
73
+ should("br") { assert_equal [[@c1, @c2], [@c3, @c4]], [].tap { |a| @ord.batches_by_range(2) { |r| a << r } } }
74
+ should("bi") { assert_equal [[@c1, @c2], [@c3, @c4]], [].tap { |a| @ord.batches_by_ids(2) { |r| a << r } } }
75
+ should("er") { assert_equal [@c1, @c2, @c3, @c4], [].tap { |a| @ord.each_by_range(2) { |r| a << r } } }
76
+ should("ei") { assert_equal [@c1, @c2, @c3, @c4], [].tap { |a| @ord.each_by_ids(2) { |r| a << r } } }
77
+ end
78
+ context "and batch size not evenly divisible by total" do
79
+ should("br") { assert_equal [[@c1, @c2, @c3], [@c4]], [].tap { |a| @ord.batches_by_range(3) { |r| a << r } } }
80
+ should("bi") { assert_equal [[@c1, @c2, @c3], [@c4]], [].tap { |a| @ord.batches_by_ids(3) { |r| a << r } } }
81
+ should("er") { assert_equal [@c1, @c2, @c3, @c4], [].tap { |a| @ord.each_by_range(3) { |r| a << r } } }
82
+ should("ei") { assert_equal [@c1, @c2, @c3, @c4], [].tap { |a| @ord.each_by_ids(3) { |r| a << r } } }
83
+ end
84
+ context "and an offset" do
85
+ setup { @off = @ord.offset(1) }
86
+ should("br") { assert_equal [[@c2, @c3], [@c4]], [].tap { |a| @off.batches_by_range(2) { |r| a << r } } }
87
+ should("bi") { assert_equal [[@c2, @c3], [@c4]], [].tap { |a| @off.batches_by_ids(2) { |r| a << r } } }
88
+ should("er") { assert_equal [@c2, @c3, @c4], [].tap { |a| @off.each_by_range(2) { |r| a << r } } }
89
+ should("ei") { assert_equal [@c2, @c3, @c4], [].tap { |a| @off.each_by_ids(2) { |r| a << r } } }
90
+ end
91
+ context "and a limit" do
92
+ setup { @lim = @ord.limit(3) }
93
+ should("br") { assert_equal [[@c1, @c2], [@c3]], [].tap { |a| @lim.batches_by_range(2) { |r| a << r } } }
94
+ should("bi") { assert_equal [[@c1, @c2], [@c3]], [].tap { |a| @lim.batches_by_ids(2) { |r| a << r } } }
95
+ should("er") { assert_equal [@c1, @c2, @c3], [].tap { |a| @lim.each_by_range(2) { |r| a << r } } }
96
+ should("ei") { assert_equal [@c1, @c2, @c3], [].tap { |a| @lim.each_by_ids(2) { |r| a << r } } }
97
+ end
98
+ end
99
+ context "with sort order" do
100
+ setup { @ord = @c.order('customers.sort') }
101
+ should("br") { assert_equal [[@c4, @c3], [@c2, @c1]], [].tap { |a| @ord.batches_by_range(2) { |r| a << r } } }
102
+ should("bi") { assert_equal [[@c4, @c3], [@c2, @c1]], [].tap { |a| @ord.batches_by_ids(2) { |r| a << r } } }
103
+ should("er") { assert_equal [@c4, @c3, @c2, @c1], [].tap { |a| @ord.each_by_range(2) { |r| a << r } } }
104
+ should("ei") { assert_equal [@c4, @c3, @c2, @c1], [].tap { |a| @ord.each_by_ids(2) { |r| a << r } } }
105
+ end
106
+ context "with order set in the select" do
107
+ setup { @ord = @c.select('customers.*, customers.sort + 1 AS foo').order('foo') }
108
+ should("br") { assert_equal [[@c4, @c3], [@c2, @c1]], [].tap { |a| @ord.batches_by_range(2) { |r| a << r } } }
109
+ should("bi") { assert_equal [[@c4, @c3], [@c2, @c1]], [].tap { |a| @ord.batches_by_ids(2) { |r| a << r } } }
110
+ should("er") { assert_equal [@c4, @c3, @c2, @c1], [].tap { |a| @ord.each_by_range(2) { |r| a << r } } }
111
+ should("ei") { assert_equal [@c4, @c3, @c2, @c1], [].tap { |a| @ord.each_by_ids(2) { |r| a << r } } }
112
+ end
113
+ end
114
+
115
+ context "with a has_many through has_many association," do
116
+ setup do
117
+ com = Company.create!
118
+ com.customers.create!
119
+ pro = com.products.create!
120
+ pur = com.customers[0].purchases
121
+ @c1 = pur.create!(:product => pro, :ordered_on => Date.parse('Jan 4 2011'))
122
+ @c2 = pur.create!(:product => pro, :ordered_on => Date.parse('Jan 3 2011'))
123
+ @c3 = pur.create!(:product => pro, :ordered_on => Date.parse('Jan 2 2011'))
124
+ @c4 = pur.create!(:product => pro, :ordered_on => Date.parse('Jan 1 2011'))
125
+ @c = com.customer_purchases
126
+ end
127
+ context "with id order," do
128
+ setup { @ord = @c.order('purchases.id') }
129
+ context "and batch size divisible by total" do
130
+ should("br") { assert_equal [[@c1, @c2], [@c3, @c4]], [].tap { |a| @ord.batches_by_range(2) { |r| a << r } } }
131
+ should("bi") { assert_equal [[@c1, @c2], [@c3, @c4]], [].tap { |a| @ord.batches_by_ids(2) { |r| a << r } } }
132
+ should("er") { assert_equal [@c1, @c2, @c3, @c4], [].tap { |a| @ord.each_by_range(2) { |r| a << r } } }
133
+ should("ei") { assert_equal [@c1, @c2, @c3, @c4], [].tap { |a| @ord.each_by_ids(2) { |r| a << r } } }
134
+ end
135
+ context "and batch size not evenly divisible by total" do
136
+ should("br") { assert_equal [[@c1, @c2, @c3], [@c4]], [].tap { |a| @ord.batches_by_range(3) { |r| a << r } } }
137
+ should("bi") { assert_equal [[@c1, @c2, @c3], [@c4]], [].tap { |a| @ord.batches_by_ids(3) { |r| a << r } } }
138
+ should("er") { assert_equal [@c1, @c2, @c3, @c4], [].tap { |a| @ord.each_by_range(3) { |r| a << r } } }
139
+ should("ei") { assert_equal [@c1, @c2, @c3, @c4], [].tap { |a| @ord.each_by_ids(3) { |r| a << r } } }
140
+ end
141
+ context "and an offset" do
142
+ setup { @off = @ord.offset(1) }
143
+ should("br") { assert_equal [[@c2, @c3], [@c4]], [].tap { |a| @off.batches_by_range(2) { |r| a << r } } }
144
+ should("bi") { assert_equal [[@c2, @c3], [@c4]], [].tap { |a| @off.batches_by_ids(2) { |r| a << r } } }
145
+ should("er") { assert_equal [@c2, @c3, @c4], [].tap { |a| @off.each_by_range(2) { |r| a << r } } }
146
+ should("ei") { assert_equal [@c2, @c3, @c4], [].tap { |a| @off.each_by_ids(2) { |r| a << r } } }
147
+ end
148
+ context "and a limit" do
149
+ setup { @lim = @ord.limit(3) }
150
+ should("br") { assert_equal [[@c1, @c2], [@c3]], [].tap { |a| @lim.batches_by_range(2) { |r| a << r } } }
151
+ should("bi") { assert_equal [[@c1, @c2], [@c3]], [].tap { |a| @lim.batches_by_ids(2) { |r| a << r } } }
152
+ should("er") { assert_equal [@c1, @c2, @c3], [].tap { |a| @lim.each_by_range(2) { |r| a << r } } }
153
+ should("ei") { assert_equal [@c1, @c2, @c3], [].tap { |a| @lim.each_by_ids(2) { |r| a << r } } }
154
+ end
155
+ end
156
+ context "with sort order" do
157
+ setup { @ord = @c.order('purchases.ordered_on') }
158
+ should("br") { assert_equal [[@c4, @c3], [@c2, @c1]], [].tap { |a| @ord.batches_by_range(2) { |r| a << r } } }
159
+ should("bi") { assert_equal [[@c4, @c3], [@c2, @c1]], [].tap { |a| @ord.batches_by_ids(2) { |r| a << r } } }
160
+ should("er") { assert_equal [@c4, @c3, @c2, @c1], [].tap { |a| @ord.each_by_range(2) { |r| a << r } } }
161
+ should("ei") { assert_equal [@c4, @c3, @c2, @c1], [].tap { |a| @ord.each_by_ids(2) { |r| a << r } } }
162
+ end
163
+ context "with order set in the select" do
164
+ setup { @ord = @c.select("purchases.*, DATE(purchases.ordered_on, '+1 DAY') AS foo").order('foo') }
165
+ should("br") { assert_equal [[@c4, @c3], [@c2, @c1]], [].tap { |a| @ord.batches_by_range(2) { |r| a << r } } }
166
+ should("bi") { assert_equal [[@c4, @c3], [@c2, @c1]], [].tap { |a| @ord.batches_by_ids(2) { |r| a << r } } }
167
+ should("er") { assert_equal [@c4, @c3, @c2, @c1], [].tap { |a| @ord.each_by_range(2) { |r| a << r } } }
168
+ should("ei") { assert_equal [@c4, @c3, @c2, @c1], [].tap { |a| @ord.each_by_ids(2) { |r| a << r } } }
169
+ end
170
+ end
171
+
172
+ context "with a has_many through has_many through belongs_to unique association," do
173
+ setup do
174
+ com = Company.create!
175
+ com.customers.create!
176
+ @c1 = com.products.create!(:sort => 4)
177
+ @c2 = com.products.create!(:sort => 3)
178
+ @c3 = com.products.create!(:sort => 2)
179
+ @c4 = com.products.create!(:sort => 1)
180
+ com.products.create!(:sort => 0)
181
+ pur = com.customers[0].purchases
182
+ pur.create!(:product => @c1)
183
+ pur.create!(:product => @c2)
184
+ pur.create!(:product => @c3)
185
+ pur.create!(:product => @c4)
186
+ pur.create!(:product => @c2)
187
+ @c = com.purchased_products
188
+ end
189
+ context "with id order," do
190
+ setup { @ord = @c.order('products.id') }
191
+ context "and batch size divisible by total" do
192
+ should("br") { assert_equal [[@c1, @c2], [@c3, @c4]], [].tap { |a| @ord.batches_by_range(2) { |r| a << r } } }
193
+ should("bi") { assert_equal [[@c1, @c2], [@c3, @c4]], [].tap { |a| @ord.batches_by_ids(2) { |r| a << r } } }
194
+ should("er") { assert_equal [@c1, @c2, @c3, @c4], [].tap { |a| @ord.each_by_range(2) { |r| a << r } } }
195
+ should("ei") { assert_equal [@c1, @c2, @c3, @c4], [].tap { |a| @ord.each_by_ids(2) { |r| a << r } } }
196
+ end
197
+ context "and batch size not evenly divisible by total" do
198
+ should("br") { assert_equal [[@c1, @c2, @c3], [@c4]], [].tap { |a| @ord.batches_by_range(3) { |r| a << r } } }
199
+ should("bi") { assert_equal [[@c1, @c2, @c3], [@c4]], [].tap { |a| @ord.batches_by_ids(3) { |r| a << r } } }
200
+ should("er") { assert_equal [@c1, @c2, @c3, @c4], [].tap { |a| @ord.each_by_range(3) { |r| a << r } } }
201
+ should("ei") { assert_equal [@c1, @c2, @c3, @c4], [].tap { |a| @ord.each_by_ids(3) { |r| a << r } } }
202
+ end
203
+ context "and an offset" do
204
+ setup { @off = @ord.offset(1) }
205
+ should("br") { assert_equal [[@c2, @c3], [@c4]], [].tap { |a| @off.batches_by_range(2) { |r| a << r } } }
206
+ should("bi") { assert_equal [[@c2, @c3], [@c4]], [].tap { |a| @off.batches_by_ids(2) { |r| a << r } } }
207
+ should("er") { assert_equal [@c2, @c3, @c4], [].tap { |a| @off.each_by_range(2) { |r| a << r } } }
208
+ should("ei") { assert_equal [@c2, @c3, @c4], [].tap { |a| @off.each_by_ids(2) { |r| a << r } } }
209
+ end
210
+ context "and a limit" do
211
+ setup { @lim = @ord.limit(3) }
212
+ should("br") { assert_equal [[@c1, @c2], [@c3]], [].tap { |a| @lim.batches_by_range(2) { |r| a << r } } }
213
+ should("bi") { assert_equal [[@c1, @c2], [@c3]], [].tap { |a| @lim.batches_by_ids(2) { |r| a << r } } }
214
+ should("er") { assert_equal [@c1, @c2, @c3], [].tap { |a| @lim.each_by_range(2) { |r| a << r } } }
215
+ should("ei") { assert_equal [@c1, @c2, @c3], [].tap { |a| @lim.each_by_ids(2) { |r| a << r } } }
216
+ end
217
+ end
218
+ context "with sort order" do
219
+ setup { @ord = @c.order('products.sort') }
220
+ should("br") { assert_equal [[@c4, @c3], [@c2, @c1]], [].tap { |a| @ord.batches_by_range(2) { |r| a << r } } }
221
+ should("bi") { assert_equal [[@c4, @c3], [@c2, @c1]], [].tap { |a| @ord.batches_by_ids(2) { |r| a << r } } }
222
+ should("er") { assert_equal [@c4, @c3, @c2, @c1], [].tap { |a| @ord.each_by_range(2) { |r| a << r } } }
223
+ should("ei") { assert_equal [@c4, @c3, @c2, @c1], [].tap { |a| @ord.each_by_ids(2) { |r| a << r } } }
224
+ end
225
+ context "with order set in the select" do
226
+ setup { @ord = @c.select('products.*, products.sort + 1 AS foo').order('foo') }
227
+ should("br") { assert_equal [[@c4, @c3], [@c2, @c1]], [].tap { |a| @ord.batches_by_range(2) { |r| a << r } } }
228
+ should("bi") { assert_equal [[@c4, @c3], [@c2, @c1]], [].tap { |a| @ord.batches_by_ids(2) { |r| a << r } } }
229
+ should("er") { assert_equal [@c4, @c3, @c2, @c1], [].tap { |a| @ord.each_by_range(2) { |r| a << r } } }
230
+ should("ei") { assert_equal [@c4, @c3, @c2, @c1], [].tap { |a| @ord.each_by_ids(2) { |r| a << r } } }
231
+ end
232
+ end
233
+
234
+ # TODO: any more complicated queries?
235
+ end
@@ -0,0 +1,24 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ if ENV.has_key?('USE_SIMPLECOV')
5
+ require 'simplecov'
6
+ SimpleCov.start do
7
+ add_group 'Libraries', 'lib'
8
+ add_filter 'test/dummy'
9
+ end
10
+ end
11
+
12
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
13
+ require "rails/test_help"
14
+ begin
15
+ require 'shoulda'
16
+ rescue LoadError
17
+ puts 'WARNING: missing shoulda library, cannot continue run tests'
18
+ exit
19
+ end
20
+
21
+ Rails.backtrace_cleaner.remove_silencers!
22
+
23
+ # Load support files
24
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: each_batched
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Burry
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-11 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70189324766440 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70189324766440
25
+ - !ruby/object:Gem::Dependency
26
+ name: sqlite3
27
+ requirement: &70189324765560 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70189324765560
36
+ - !ruby/object:Gem::Dependency
37
+ name: shoulda
38
+ requirement: &70189324764320 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70189324764320
47
+ - !ruby/object:Gem::Dependency
48
+ name: simplecov
49
+ requirement: &70189324763380 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70189324763380
58
+ description: ! 'ActiveRecord::Batches#find_in_batches has some gotchas. This library
59
+ provides alternate algorithms that may better suit you, in certain circumstances. Specifically:
60
+ you can order your results other than by primary key, and you can limit your batches
61
+ to just a certain range of results not only all records.'
62
+ email:
63
+ - dburry@falcon
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - lib/each_batched/version.rb
69
+ - lib/each_batched.rb
70
+ - lib/tasks/each_batched_tasks.rake
71
+ - MIT-LICENSE
72
+ - Rakefile
73
+ - README.rdoc
74
+ - test/dummy/app/controllers/application_controller.rb
75
+ - test/dummy/app/helpers/application_helper.rb
76
+ - test/dummy/app/models/company.rb
77
+ - test/dummy/app/models/customer.rb
78
+ - test/dummy/app/models/product.rb
79
+ - test/dummy/app/models/purchase.rb
80
+ - test/dummy/app/views/layouts/application.html.erb
81
+ - test/dummy/config/application.rb
82
+ - test/dummy/config/boot.rb
83
+ - test/dummy/config/database.yml
84
+ - test/dummy/config/environment.rb
85
+ - test/dummy/config/environments/development.rb
86
+ - test/dummy/config/environments/production.rb
87
+ - test/dummy/config/environments/test.rb
88
+ - test/dummy/config/initializers/backtrace_silencers.rb
89
+ - test/dummy/config/initializers/inflections.rb
90
+ - test/dummy/config/initializers/mime_types.rb
91
+ - test/dummy/config/initializers/secret_token.rb
92
+ - test/dummy/config/initializers/session_store.rb
93
+ - test/dummy/config/locales/en.yml
94
+ - test/dummy/config/routes.rb
95
+ - test/dummy/config.ru
96
+ - test/dummy/db/development.sqlite3
97
+ - test/dummy/db/migrate/0001_initialize_models.rb
98
+ - test/dummy/db/schema.rb
99
+ - test/dummy/db/test.sqlite3
100
+ - test/dummy/log/development.log
101
+ - test/dummy/log/test.log
102
+ - test/dummy/public/404.html
103
+ - test/dummy/public/422.html
104
+ - test/dummy/public/500.html
105
+ - test/dummy/public/favicon.ico
106
+ - test/dummy/Rakefile
107
+ - test/dummy/script/rails
108
+ - test/each_batched_test.rb
109
+ - test/test_helper.rb
110
+ homepage: http://github.com/dburry/each_batched
111
+ licenses: []
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ segments:
123
+ - 0
124
+ hash: 4267043599920794620
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ segments:
132
+ - 0
133
+ hash: 4267043599920794620
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 1.8.6
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: More convenient batching than Rails' ActiveRecord::Batches#find_in_batches
140
+ test_files:
141
+ - test/dummy/app/controllers/application_controller.rb
142
+ - test/dummy/app/helpers/application_helper.rb
143
+ - test/dummy/app/models/company.rb
144
+ - test/dummy/app/models/customer.rb
145
+ - test/dummy/app/models/product.rb
146
+ - test/dummy/app/models/purchase.rb
147
+ - test/dummy/app/views/layouts/application.html.erb
148
+ - test/dummy/config/application.rb
149
+ - test/dummy/config/boot.rb
150
+ - test/dummy/config/database.yml
151
+ - test/dummy/config/environment.rb
152
+ - test/dummy/config/environments/development.rb
153
+ - test/dummy/config/environments/production.rb
154
+ - test/dummy/config/environments/test.rb
155
+ - test/dummy/config/initializers/backtrace_silencers.rb
156
+ - test/dummy/config/initializers/inflections.rb
157
+ - test/dummy/config/initializers/mime_types.rb
158
+ - test/dummy/config/initializers/secret_token.rb
159
+ - test/dummy/config/initializers/session_store.rb
160
+ - test/dummy/config/locales/en.yml
161
+ - test/dummy/config/routes.rb
162
+ - test/dummy/config.ru
163
+ - test/dummy/db/development.sqlite3
164
+ - test/dummy/db/migrate/0001_initialize_models.rb
165
+ - test/dummy/db/schema.rb
166
+ - test/dummy/db/test.sqlite3
167
+ - test/dummy/log/development.log
168
+ - test/dummy/log/test.log
169
+ - test/dummy/public/404.html
170
+ - test/dummy/public/422.html
171
+ - test/dummy/public/500.html
172
+ - test/dummy/public/favicon.ico
173
+ - test/dummy/Rakefile
174
+ - test/dummy/script/rails
175
+ - test/each_batched_test.rb
176
+ - test/test_helper.rb