record-collection 0.0.1424540975 → 0.1.1424542204
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 +4 -4
- data/lib/record_collection.rb +14 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e32711e7065692238d331a260412a7fcfe983422
|
4
|
+
data.tar.gz: 6833e8207a03b42d9c9bedff75f3d900ca35944d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82824a2a1ab8a44c0483ccd9c071775fe2d6ff3401a35522292aeca7646b3137f790175f472f1b6498ec7833bd0d3c78d88beb7cd83452b33025e4bba68f285e
|
7
|
+
data.tar.gz: 2cd606a71c2bf37da7a9a52d9ab273b9b3eac798317aad0e6afbf57d49c238a84829c38baae3cf4eaf96beb56a232829d73ff5bbb6a7416bff7849efdb78b5f4
|
data/lib/record_collection.rb
CHANGED
@@ -3,20 +3,33 @@ class RecordCollection
|
|
3
3
|
require 'set'
|
4
4
|
|
5
5
|
def initialize(base_scope)
|
6
|
-
self.base_scope = base_scope
|
6
|
+
self.base_scope = coerce_to_relation(base_scope)
|
7
7
|
self.scopes = Set.new
|
8
8
|
end
|
9
9
|
|
10
|
+
def coerce_to_relation(base_scope)
|
11
|
+
if base_scope.respond_to?(:all)
|
12
|
+
base_scope.all
|
13
|
+
else
|
14
|
+
# rails 3
|
15
|
+
base_scope.scoped
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
10
19
|
def limit_by(scope)
|
11
20
|
scopes.add(scope)
|
12
21
|
self
|
13
22
|
end
|
14
23
|
|
24
|
+
alias and limit_by
|
25
|
+
|
15
26
|
def is_limited_by?(scope)
|
16
27
|
# because ActiveRecord::Relations do not implement hashing correctly, we must do case by case equality
|
17
28
|
scopes.any? { |s| s == scope }
|
18
29
|
end
|
19
30
|
|
31
|
+
alias limited_by? is_limited_by?
|
32
|
+
|
20
33
|
def each(&block)
|
21
34
|
scopes.reduce(base_scope) { |scope, base| base.merge(scope) }.each(&block)
|
22
35
|
end
|