passive_record 0.3.18 → 0.3.19
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -9
- data/lib/passive_record/associations/has_many.rb +18 -0
- data/lib/passive_record/version.rb +1 -1
- data/spec/passive_record_spec.rb +10 -0
- data/spec/spec_helper.rb +1 -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: 308e4dbc28c56bee86bcd17bfac0e2d88e6e4134
|
4
|
+
data.tar.gz: 26a64acb50009c2ccc9aced0c5aefef4b46560e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06b0384b55def7cab63cd01a7f6b7e142719251885f9c263e845a2ae0bdc503f7300d887fa1149298170aa29477f27fca46de09892c5f32db54146ba440283be
|
7
|
+
data.tar.gz: 6428e64b00eefb4043e8d21925c7ca39b75e83bd2ab58f86c461f87641f7b852e471ef81e548d93bf746a5d941dcf8f99abf703cc754a0ea7fec39fd62255c6b
|
data/README.md
CHANGED
@@ -162,15 +162,23 @@ PassiveRecord may be right for you!
|
|
162
162
|
|
163
163
|
### Queries
|
164
164
|
|
165
|
-
`Core::Query` objects
|
166
|
-
|
167
|
-
- `where(conditions).all`
|
168
|
-
- `where(conditions).each` enumerates over `where(conditions).all`, so we have `where(conditions).count`, `where(conditions).first`, etc.
|
169
|
-
- `where(conditions).create(attrs)`
|
170
|
-
- `where(conditions).first_or_create(attrs)`
|
171
|
-
- `where(conditions).where(further_conditions)` (chaining)
|
172
|
-
- `where.not(conditions)` (negation)
|
173
|
-
- `where.
|
165
|
+
You can acquire `Core::Query` objects through the class method `where`. These are chainable, accept nested conditions that traverse relationships, and understand scopes defined as class methods. The query object will have the following public methods:
|
166
|
+
|
167
|
+
- `Post.where(conditions).all`
|
168
|
+
- `Post.where(conditions).each` enumerates over `where(conditions).all`, so we have `where(conditions).count`, `where(conditions).first`, etc.
|
169
|
+
- `Post.where(conditions).create(attrs)`
|
170
|
+
- `Post.where(conditions).first_or_create(attrs)`
|
171
|
+
- `Post.where(conditions).where(further_conditions)` (chaining)
|
172
|
+
- `Post.where.not(conditions)` (negation)
|
173
|
+
- `Post.where(conditions).or(Post.where(conditions))` (disjunction)
|
174
|
+
- `Post.active.recent` (scoping with class methods that return queries; supports chaining)
|
175
|
+
|
176
|
+
`conditions` here is expected to be a hash of attribute values. Note that there is special behavior for certain kinds of values.
|
177
|
+
|
178
|
+
- Ranges select models with an attribute covered by the range (behaving like `BETWEEN`)
|
179
|
+
- Arrays select models with an attribute whose value is in the array (behaving like `IN`)
|
180
|
+
- Hash values (subhashes) select models with related models who attributes match the inner hash. So `Doctor.where(appointments: { patient: patient })` finds doctors whose appointments include an appointment with `patient`.
|
181
|
+
|
174
182
|
|
175
183
|
## Hooks
|
176
184
|
|
@@ -40,6 +40,24 @@ module PassiveRecord
|
|
40
40
|
all
|
41
41
|
end
|
42
42
|
|
43
|
+
def pluck(attr)
|
44
|
+
all.map(&attr)
|
45
|
+
end
|
46
|
+
|
47
|
+
def sum(attr)
|
48
|
+
pluck(attr).inject(&:+)
|
49
|
+
end
|
50
|
+
|
51
|
+
def average(attr)
|
52
|
+
sum(attr) / count
|
53
|
+
end
|
54
|
+
|
55
|
+
def mode(attr)
|
56
|
+
arr = pluck(attr)
|
57
|
+
freq = arr.inject(Hash.new(0)) { |h,v| h[v] += 1; h }
|
58
|
+
arr.max_by { |v| freq[v] }
|
59
|
+
end
|
60
|
+
|
43
61
|
def singular?
|
44
62
|
false
|
45
63
|
end
|
data/spec/passive_record_spec.rb
CHANGED
@@ -340,6 +340,16 @@ describe "passive record models" do
|
|
340
340
|
expect(parent.children.all).to eq([child, another_child])
|
341
341
|
expect(parent.child_ids).to eq([child.id, another_child.id])
|
342
342
|
end
|
343
|
+
|
344
|
+
it 'should provide arithmetic helpers' do
|
345
|
+
parent.create_child(age: 10)
|
346
|
+
parent.create_child(age: 10)
|
347
|
+
parent.create_child(age: 40)
|
348
|
+
expect(parent.children.pluck(:age)).to eq([10,10,40])
|
349
|
+
expect(parent.children.sum(:age)).to eq(60)
|
350
|
+
expect(parent.children.average(:age)).to eq(20)
|
351
|
+
expect(parent.children.mode(:age)).to eq(10)
|
352
|
+
end
|
343
353
|
end
|
344
354
|
|
345
355
|
context 'one-to-many through relationships' do
|
data/spec/spec_helper.rb
CHANGED