passive_record 0.3.4 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/passive_record/class_methods.rb +1 -1
- data/lib/passive_record/core/query.rb +30 -0
- data/lib/passive_record/version.rb +1 -1
- data/spec/passive_record_spec.rb +9 -0
- 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: 291605bad255c42e5aeb6a91e58738cb4d0ac646
|
4
|
+
data.tar.gz: dbb92d140be813f5374c219d5eb37731d112f1e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24297308bdad6a014e87126f7dcf7334d24ba87e0638bc6c97bf6189114f580918fb0bb47f6af8941f974ed07058562c5276940db40d8301c1c2f9bf5864ffe6
|
7
|
+
data.tar.gz: 23e3adc6f921481527447089a6bbb0af921bd5cde00ec051fd272595570e916a124bc184f80c02c972e65a35ae2e1f5c42e5ee11da2afaa87731e50d3daa1036
|
@@ -9,6 +9,11 @@ module PassiveRecord
|
|
9
9
|
def initialize(klass,conditions={})
|
10
10
|
@klass = klass
|
11
11
|
@conditions = conditions
|
12
|
+
@subqueries = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def not(conditions={})
|
16
|
+
NegatedQuery.new(@klass, conditions)
|
12
17
|
end
|
13
18
|
|
14
19
|
def all
|
@@ -45,6 +50,10 @@ module PassiveRecord
|
|
45
50
|
end
|
46
51
|
|
47
52
|
protected
|
53
|
+
def negated?
|
54
|
+
false
|
55
|
+
end
|
56
|
+
|
48
57
|
def evaluate_nested_conditions(instance, field, value)
|
49
58
|
association = instance.send(field)
|
50
59
|
association && value.all? do |(association_field,val)|
|
@@ -56,5 +65,26 @@ module PassiveRecord
|
|
56
65
|
end
|
57
66
|
end
|
58
67
|
end
|
68
|
+
|
69
|
+
class NegatedQuery < Query
|
70
|
+
def all
|
71
|
+
return [] unless conditions
|
72
|
+
klass.select do |instance|
|
73
|
+
conditions.none? do |(field,value)|
|
74
|
+
if value.is_a?(Hash)
|
75
|
+
evaluate_nested_conditions(instance, field, value)
|
76
|
+
elsif value.is_a?(Range) || value.is_a?(Array)
|
77
|
+
value.include?(instance.send(field))
|
78
|
+
else
|
79
|
+
instance.send(field) == value
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def negated?
|
86
|
+
true
|
87
|
+
end
|
88
|
+
end
|
59
89
|
end
|
60
90
|
end
|
data/spec/passive_record_spec.rb
CHANGED
@@ -176,7 +176,16 @@ describe "passive record models" do
|
|
176
176
|
Model.create(id: 12)
|
177
177
|
expect(Model.find_all_by(id: [10,11])).to eq([model_a, model_b])
|
178
178
|
end
|
179
|
+
end
|
180
|
+
|
181
|
+
context 'queries with negations' do
|
182
|
+
it 'should find where attribute value is NOT equal' do
|
183
|
+
model_a = Model.create(id: 'alpha')
|
184
|
+
model_b = Model.create(id: 'beta')
|
179
185
|
|
186
|
+
expect(Model.where.not(id: 'alpha').first).to eq(model_b)
|
187
|
+
expect(Model.where.not(id: 'beta').first).to eq(model_a)
|
188
|
+
end
|
180
189
|
end
|
181
190
|
end
|
182
191
|
end
|