passive_record 0.4.7 → 0.4.8
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/passive_record/core/query.rb +7 -11
- data/lib/passive_record/version.rb +1 -1
- data/spec/passive_record_spec.rb +28 -9
- data/spec/spec_helper.rb +1 -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: 7f039ca57502dbf0119be19278464cfc7cae14ab
|
4
|
+
data.tar.gz: 414f80e091cf917d897fd89fd28902c6f2563398
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24ce6d75265f9edf97ecf7aa55bb1e4d10f4deed2c7452c0f08758d5890db1bb12fd6369709e7ef06f6ac5bd66df1097ac64a7ca580bf1b5fffbba2bbd1f384b
|
7
|
+
data.tar.gz: 1f25bb2ec4160669a6332da359410a7f1f8fc0359bb175189f54fd4b84139d61565f92e74bc6e0640f220abda45b01905df3cb16296f44af73e33e92fa00099b
|
@@ -40,21 +40,19 @@ module PassiveRecord
|
|
40
40
|
if @scope
|
41
41
|
matching = @scope.method(:matching_instances)
|
42
42
|
if negated?
|
43
|
-
@klass.all.each do |instance|
|
43
|
+
@klass.all.each do |instance|
|
44
44
|
yield instance unless matching[instance]
|
45
45
|
end
|
46
46
|
else
|
47
47
|
@klass.all.each do |instance|
|
48
48
|
yield instance if matching[instance]
|
49
49
|
end
|
50
|
-
# @klass.select(&matching)
|
51
50
|
end
|
52
51
|
else
|
53
52
|
matching = method(:matching_instances)
|
54
53
|
@klass.all.each do |instance|
|
55
54
|
yield instance if matching[instance]
|
56
55
|
end
|
57
|
-
# @klass.select(&matching)
|
58
56
|
end
|
59
57
|
end
|
60
58
|
|
@@ -152,14 +150,15 @@ module PassiveRecord
|
|
152
150
|
end
|
153
151
|
|
154
152
|
class DisjoinedQuery < Query
|
155
|
-
def initialize(klass, first_query, second_query)
|
153
|
+
def initialize(klass, first_query, second_query, conditions={})
|
156
154
|
@klass = klass
|
157
155
|
@first_query = first_query
|
158
156
|
@second_query = second_query
|
157
|
+
@conditions = conditions
|
159
158
|
end
|
160
159
|
|
161
160
|
def all
|
162
|
-
(@first_query.all + @second_query.all).uniq
|
161
|
+
(@first_query.where(conditions).all + @second_query.where(conditions).all).uniq
|
163
162
|
end
|
164
163
|
|
165
164
|
def disjoined?
|
@@ -168,18 +167,15 @@ module PassiveRecord
|
|
168
167
|
end
|
169
168
|
|
170
169
|
class ConjoinedQuery < Query
|
171
|
-
def initialize(klass, first_query, second_query)
|
170
|
+
def initialize(klass, first_query, second_query, conditions={})
|
172
171
|
@klass = klass
|
173
172
|
@first_query = first_query
|
174
173
|
@second_query = second_query
|
174
|
+
@conditions = conditions
|
175
175
|
end
|
176
176
|
|
177
177
|
def all
|
178
|
-
@first_query.all & @second_query.all
|
179
|
-
end
|
180
|
-
|
181
|
-
def where(new_conditions={})
|
182
|
-
@first_query.where(new_conditions)
|
178
|
+
@first_query.where(conditions).all & @second_query.all
|
183
179
|
end
|
184
180
|
|
185
181
|
def conjoined?
|
data/spec/passive_record_spec.rb
CHANGED
@@ -19,7 +19,7 @@ end
|
|
19
19
|
describe "passive record models" do
|
20
20
|
before(:each) { PassiveRecord.drop_all }
|
21
21
|
|
22
|
-
context "
|
22
|
+
context "a simple model including PR" do
|
23
23
|
let!(:model) { SimpleModel.create(foo: value) }
|
24
24
|
let(:value) { 'foo_value' }
|
25
25
|
|
@@ -72,6 +72,7 @@ describe "passive record models" do
|
|
72
72
|
describe "#id" do
|
73
73
|
it 'should be retrievable by id' do
|
74
74
|
expect(SimpleModel.find_by(model.id)).to eq(model)
|
75
|
+
expect(SimpleModel.find(model.id)).to eq(model)
|
75
76
|
end
|
76
77
|
end
|
77
78
|
end
|
@@ -216,17 +217,35 @@ describe "passive record models" do
|
|
216
217
|
end
|
217
218
|
|
218
219
|
context 'queries with disjunctions' do
|
220
|
+
let(:poms_or_pugs) do
|
221
|
+
Family::Dog.
|
222
|
+
where(breed: 'pom').or(Family::Dog.where(breed: 'pug'))
|
223
|
+
end
|
224
|
+
|
225
|
+
let(:poms_or_small_dogs) do
|
226
|
+
Family::Dog.
|
227
|
+
where(breed: 'pom').or(Family::Dog.where(size: %w[ tiny small ]))
|
228
|
+
end
|
229
|
+
|
230
|
+
before do
|
231
|
+
@pom = Family::Dog.create(breed: 'pom', size: 'tiny')
|
232
|
+
@pug = Family::Dog.create(breed: 'pug', size: 'big')
|
233
|
+
|
234
|
+
Family::Dog.create(breed: 'mutt', size: 'medium')
|
235
|
+
Family::Dog.create(breed: 'lab', size: 'large')
|
236
|
+
|
237
|
+
@pap = Family::Dog.create(breed: 'papillon', size: 'small')
|
238
|
+
end
|
239
|
+
|
219
240
|
it 'should find where attributes match EITHER query' do
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
Family::Dog.create breed: 'lab'
|
224
|
-
Family::Dog.create breed: 'papillon'
|
241
|
+
expect(poms_or_pugs.all).to eq([@pom, @pug])
|
242
|
+
expect(poms_or_small_dogs.all).to eq([@pom, @pap])
|
243
|
+
end
|
225
244
|
|
245
|
+
it 'should be able to refine a disjunction' do
|
226
246
|
expect(
|
227
|
-
|
228
|
-
|
229
|
-
).to eq([pom, pug])
|
247
|
+
poms_or_pugs.where(size: %w[ tiny small ]).all
|
248
|
+
).to eq([@pom])
|
230
249
|
end
|
231
250
|
end
|
232
251
|
|
data/spec/spec_helper.rb
CHANGED