embark-journey 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/journey/resource/where_multiple.rb +23 -0
- data/lib/journey/version.rb +1 -1
- data/spec/models/journey/resource/where_multiple_spec.rb +31 -10
- 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: be545fab222f3f8b7ba132b398f1939493884720
|
4
|
+
data.tar.gz: adeec5bc879a5ace33102a3306c1c3fa964d14bc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cdc71dea35635846aa89f5020c51eecb6571885fdeb3c190208a609b577809183fb62b4407a881a7a7f60aa437721cab115815f509626b1deba98a3a3a7729f4
|
7
|
+
data.tar.gz: a2421076bf07010f27daa861efd57d852aef0b9b61d461586ff798d3dc680d40766cb441b5447753382ad5d666e5d9d376440141add2fc4d8b849bbded3632f3
|
@@ -24,6 +24,29 @@ module Journey::Resource::WhereMultiple
|
|
24
24
|
where(clauses.merge(query: query))
|
25
25
|
end
|
26
26
|
end
|
27
|
+
|
28
|
+
def self.count_multiple(clauses)
|
29
|
+
# TODO refactor me to re-use all the same recursive query logic in `where_multiple`
|
30
|
+
|
31
|
+
query = clauses.delete(:query)
|
32
|
+
query_keys_with_array_values = query.map do |key, value|
|
33
|
+
key if value.is_a?(Array)
|
34
|
+
end.compact
|
35
|
+
|
36
|
+
if branch_key = query_keys_with_array_values.first
|
37
|
+
consistent_query = query.except(branch_key)
|
38
|
+
value_branches = query[branch_key]
|
39
|
+
|
40
|
+
value_branches.map do |value|
|
41
|
+
branch_query = consistent_query.merge(branch_key => value)
|
42
|
+
count_multiple(clauses.merge(query: branch_query))
|
43
|
+
end.sum
|
44
|
+
|
45
|
+
else
|
46
|
+
count(clauses.merge(query: query))
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
27
50
|
end
|
28
51
|
|
29
52
|
end
|
data/lib/journey/version.rb
CHANGED
@@ -16,8 +16,14 @@ describe Journey::Resource::WhereMultiple do
|
|
16
16
|
before { klass.destroy_all }
|
17
17
|
|
18
18
|
let!(:candidates) { matchables + unmatchables }
|
19
|
+
let(:collection) { klass.where_multiple(clauses) }
|
20
|
+
let(:count) { klass.count_multiple(clauses) }
|
19
21
|
|
20
22
|
context "when query doesn't contain any key having an array-like value" do
|
23
|
+
let(:clauses) do
|
24
|
+
{ query: { number: 'A' } }
|
25
|
+
end
|
26
|
+
|
21
27
|
let(:matchables) do
|
22
28
|
[ klass.create(number: 'A') ]
|
23
29
|
end
|
@@ -25,7 +31,6 @@ describe Journey::Resource::WhereMultiple do
|
|
25
31
|
[ klass.create(number: 'X') ]
|
26
32
|
end
|
27
33
|
|
28
|
-
let!(:collection) { klass.where_multiple(query: { number: 'A' }) }
|
29
34
|
|
30
35
|
it 'returns correct results' do
|
31
36
|
expect(matchables).to be_all do |matchable|
|
@@ -36,11 +41,17 @@ describe Journey::Resource::WhereMultiple do
|
|
36
41
|
end
|
37
42
|
end
|
38
43
|
|
44
|
+
it 'counts correctly' do
|
45
|
+
expect(count).to eq 1
|
46
|
+
end
|
47
|
+
|
39
48
|
pending 'performs 1 query'
|
40
49
|
end
|
41
50
|
|
42
51
|
context "when query contains a key with the value of an array containing a single item" do
|
43
|
-
|
52
|
+
let(:clauses) do
|
53
|
+
{ query: { number: ['A'] }, sort: { number: :desc } }
|
54
|
+
end
|
44
55
|
let(:matchables) do
|
45
56
|
[
|
46
57
|
klass.create(number: 'A')
|
@@ -52,10 +63,7 @@ describe Journey::Resource::WhereMultiple do
|
|
52
63
|
]
|
53
64
|
end
|
54
65
|
|
55
|
-
let!(:collection) { klass.where_multiple(query: { number: ['A'] }, sort: { number: :desc }) }
|
56
|
-
|
57
66
|
it 'returns correct results' do
|
58
|
-
collection
|
59
67
|
expect(matchables).to be_all do |matchable|
|
60
68
|
collection.include?(matchable)
|
61
69
|
end
|
@@ -64,12 +72,18 @@ describe Journey::Resource::WhereMultiple do
|
|
64
72
|
end
|
65
73
|
end
|
66
74
|
|
75
|
+
it 'counts correctly' do
|
76
|
+
expect(count).to eq 1
|
77
|
+
end
|
78
|
+
|
67
79
|
pending 'performs n queries'
|
68
80
|
|
69
81
|
end
|
70
82
|
|
71
83
|
context "when query contains one key having an array-like value" do
|
72
|
-
|
84
|
+
let(:clauses) do
|
85
|
+
{ query: { number: ['A', 'B'] } }
|
86
|
+
end
|
73
87
|
let(:matchables) do
|
74
88
|
[
|
75
89
|
klass.create(number: 'A'),
|
@@ -83,8 +97,6 @@ describe Journey::Resource::WhereMultiple do
|
|
83
97
|
]
|
84
98
|
end
|
85
99
|
|
86
|
-
let!(:collection) { klass.where_multiple(query: { number: ['A', 'B'] }) }
|
87
|
-
|
88
100
|
it 'returns correct results' do
|
89
101
|
expect(matchables).to be_all do |matchable|
|
90
102
|
collection.include?(matchable)
|
@@ -94,11 +106,18 @@ describe Journey::Resource::WhereMultiple do
|
|
94
106
|
end
|
95
107
|
end
|
96
108
|
|
109
|
+
it 'counts correctly' do
|
110
|
+
expect(count).to eq 2
|
111
|
+
end
|
112
|
+
|
97
113
|
pending 'performs n queries'
|
98
114
|
end
|
99
115
|
|
100
116
|
|
101
117
|
context "when query contains two keys having array-like values" do
|
118
|
+
let(:clauses) do
|
119
|
+
{ query: { number: ['A', 'B'], flash_number: ['1', '2'] } }
|
120
|
+
end
|
102
121
|
let(:matchables) do
|
103
122
|
[
|
104
123
|
klass.create(number: 'A', flash_number: '1'),
|
@@ -117,8 +136,6 @@ describe Journey::Resource::WhereMultiple do
|
|
117
136
|
]
|
118
137
|
end
|
119
138
|
|
120
|
-
let!(:collection) { klass.where_multiple(query: { number: ['A', 'B'], flash_number: ['1', '2'] }) }
|
121
|
-
|
122
139
|
it 'returns correct results' do
|
123
140
|
expect(matchables).to be_all do |matchable|
|
124
141
|
collection.include?(matchable)
|
@@ -128,6 +145,10 @@ describe Journey::Resource::WhereMultiple do
|
|
128
145
|
end
|
129
146
|
end
|
130
147
|
|
148
|
+
it 'counts correctly' do
|
149
|
+
expect(count).to eq 4
|
150
|
+
end
|
151
|
+
|
131
152
|
pending 'performs m * n queries'
|
132
153
|
end
|
133
154
|
|