Skip to content

Commit 3570bce

Browse files
committed
Append method PostgreSQLCursor::Cursor#size for ActiveRecord collection rendering
1 parent 14735ca commit 3570bce

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ Product.each_row.map {|r| r["id"].to_i } #=> [1, 2, 3, ...]
8888
Product.each_instance.map {|r| r.id }.each {|id| p id } #=> [1, 2, 3, ...]
8989
Product.each_instance.lazy.inject(0) {|sum,r| sum + r.quantity } #=> 499500
9090
```
91+
92+
### PostgreSQLCursor and collection rendering
93+
94+
You can render cursor collection, using enumeration as collection attribute.
95+
96+
```ruby
97+
render partial: "some_partial", collection: Product.each_instance
98+
render partial: "some_partial", collection: Product.each_row
99+
render partial: "some_partial", collection: Product.each_hash
100+
```
101+
91102
### Hashes vs. Instances
92103

93104
The each_row method returns the Hash of strings for speed (as this allows you to process a lot of rows).

lib/postgresql_cursor/cursor.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ def iterate_batched(batched = true)
7171
self
7272
end
7373

74+
# ActiveRecord call #size when rendering a collection
75+
# Define it and return some dummy value
76+
def size
77+
-1
78+
end
79+
7480
# Public: Yields each row of the result set to the passed block
7581
#
7682
# Yields the row to the block. The row is a hash with symbolized keys.

test/test_postgresql_cursor.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,13 @@ def test_relation_association_is_not_loaded
225225
cursor = Product.first.prices.each_instance
226226
refute cursor.instance_variable_get(:@type).loaded?
227227
end
228+
229+
def test_size
230+
r = Product.each_instance
231+
assert_equal -1, r.size
232+
r = Product.each_hash
233+
assert_equal -1, r.size
234+
r = Product.each_row
235+
assert_equal -1, r.size
236+
end
228237
end

0 commit comments

Comments
 (0)